AlgoFrames

Max area of island

Standard · Write code · about 12 min

A grid holds 1 for land and 0 for water. An island is a group of land cells joined up, down, left or right. Its area is the number of cells it has.

Return the largest area of any island, or 0 if there is no land.

Example 1

Input:

[[[1,1,0,0],[0,1,0,0],[0,0,1,1],[0,0,1,1]]]

Output:

4

The islands have areas 3 and 4.

Example 2

Input:

[[[0,0,0],[0,0,0]]]

Output:

0

No land at all.

Constraints

  • 1 ≤ rows, cols ≤ 50
  • grid[r][c] is 0 or 1

Learn the idea first: Connected components and flood fill, a visual lesson.

More Connected components and flood fill problems