Skip to content
AI360Xpert
Beta
Difficulty: MediumGraphs

Max Area of Island

Problem in Plain English

Given a binary grid of land and water, find the cell count of the largest landmass connected horizontally or vertically. Grids without land score zero.

Problem Statement

You are given an m x n binary matrix grid where 1 is land and 0 is water.

An island is a group of 1s connected horizontally or vertically, and its area is its cell count. Return the largest island area, or 0 when there is no land.

Constraints

  • m == grid.length
  • n == grid[i].length
  • 1 <= m, n <= 50
  • grid[i][j] is 0 or 1.

Examples

Example 1

Input
grid = [[1,1,0],[0,1,0],[1,0,1]]
Output
3

Cells (0,0), (0,1) and (1,1) connect through shared sides. The bottom corners touch that group only diagonally, so each has area 1 and the maximum is 3.

Example 2

Input
grid = [[0,0,0,0,0,0,0,0]]
Output
0

No land cell exists, so the maximum area is zero.

Example 3

Input
grid = [[1]]
Output
1

The only cell is land, so it forms one island of area 1 even though every neighbor lies outside the grid.

Example 4

Input
grid = [[0,0,1,0,0,0,0,1,0,0,0,0,0],[0,0,0,0,0,0,0,1,1,1,0,0,0],[0,1,1,0,1,0,0,0,0,0,0,0,0],[0,1,0,0,1,1,0,0,1,0,1,0,0],[0,1,0,0,1,1,0,0,1,1,1,0,0],[0,0,0,0,0,0,0,0,0,0,1,0,0],[0,0,0,0,0,0,0,1,1,1,0,0,0],[0,0,0,0,0,0,0,1,1,0,0,0,0]]
Output
6

The land around rows 3-5 near column 8 links six cells, beating the islands of size 5, 4 and 1.

Intuition

An island is a connected component of land cells: starting at any of its cells and following only horizontal or vertical land neighbors reaches exactly that island. In Example 1, (0,0), (0,1) and (1,1) make a component of area 3, while (2,0) and (2,2) each stand alone. Count cells during a flood fill and keep the largest count. Remembering discoveries across the outer scan prevents measuring the same island again.

Approaches

Flood Per Cell

Solution Details

Reveal Flood Per Cell: intuition, complexity, and code

Hints

Hint 1
Walk the whole grid and start measuring only at land cells you have not settled yet.
Hint 2
Count with a flood that moves up, down, left and right from each start cell.
Hint 3
Mark flooded land as visited, by sinking it to water or a second grid, so no island is ever measured twice.

Edge Cases

  • All water returns 0; no flood begins. A one-cell land grid returns 1.
  • An all-land 50 by 50 grid returns 2500. The explicit stack avoids a recursive call chain beyond Python's default recursion limit.
  • Diagonal contact does not connect islands: [[1,0],[0,1]] returns 1.
  • A one-row or one-column island is counted normally, with out-of-range neighbors ignored.
  • The single-pass version leaves all land as water. Reusing that same grid for another call returns 0; clone rows first if the caller needs the original.

Common Mistakes and Interview Tips

  • Following diagonal neighbors merges distinct islands. Use exactly the four side directions.
  • Marking only when popping allows several parents to push one cell; counting each pop then overestimates area.
  • Returning the number of fills instead of the largest area solves Number of Islands, a different problem. Reset area for each fill and keep best across fills.
  • Calling recursive flood fill on a 2500-cell winding island can exceed a language recursion limit; the published code uses an explicit stack.
  • Claiming constant auxiliary space because the grid is mutated ignores the stack. Also disclose input mutation in an interview.

Key Takeaway

Grid flood problems reward settling each cell once; sinking land or a global visited set turns repeated island walks into a single linear sweep.