Max Area of Island is the classic grid flood-fill problem. It teaches the move you reach for on any 2D grid: walk the grid, and whenever you find an unvisited region, flood-fill it with DFS to measure how big it is.
Problem. Given an m x n binary grid where 1 is land and 0 is water, an island is a group of
1s connected up/down/left/right. Return the area (number of cells) of the largest island. If there
are no islands, return 0.
Example: the 4×4 grid below has a 4-cell island in the top-left and a lone 1-cell island, so the answer is 4.
The slow way first
You might try to be clever and reason about shapes, but there is no shortcut around actually visiting the land. The real question is: when I stand on a land cell, how do I measure the whole island it belongs to without counting any cell twice?
If you do not mark cells as visited, a naive walk will revisit the same land over and over and either loop forever or massively overcount. The fix is simple and is the heart of the technique: mark each land cell the moment you count it.
The idea: flood-fill each island with DFS
Scan every cell. When you land on a 1 you have not seen, start a DFS from it. The DFS counts the current cell, marks it visited (overwrite it to 0), then recurses into its four neighbors, summing whatever area they return. Water and out-of-bounds cells return 0. The total is that island area; compare it against the running max.
The key insight: overwriting a counted cell to 0 doubles as the visited marker, so the same DFS naturally stops at the island edges and never recounts a cell.
Walk through it
Step through the animation. The scan hits land at (0,0) and launches a DFS. Watch each visited cell flip and the area counter climb to 4 as the flood-fill expands down and right. When the neighbors run out, the DFS returns 4 and max updates. The scan then finds a separate 1-cell island, but max stays 4.
Pseudocode
best = 0
for each cell (r, c) in the grid:
if grid[r][c] is land (1):
best = max(best, dfs(r, c)) # measure this island
return best
dfs(r, c):
if (r, c) is off-grid or water (0):
return 0
grid[r][c] = 0 # mark visited so we never recount
area = 1 # count this cell
for each of the 4 neighbors:
area += dfs(neighbor) # add the connected land
return areaThe Python solution
def max_area_of_island(grid):
best = 0
for r in range(len(grid)):
for c in range(len(grid[0])):
if grid[r][c] == 1:
best = max(best, dfs(grid, r, c))
return best
def dfs(grid, r, c):
if not in_bounds(grid, r, c) or grid[r][c] == 0:
return 0
grid[r][c] = 0
area = 1
for nr, nc in neighbors(r, c):
area += dfs(grid, nr, nc)
return area- The double loop scans every cell; we only start a DFS when we hit a land cell.
best = max(best, dfs(...))keeps the largest island area seen so far.- In
dfs, the firstifis the base case: off-grid or water returns0, ending that branch. grid[r][c] = 0marks the cell visited and counts as overwriting it, so it is never visited again.area = 1counts the current cell; we then add the area returned by each of the four neighbor DFS calls.
Complexity
| Case | Time | Notes |
|---|---|---|
| Scan + flood-fill | O(m·n) (moderate) | each cell is visited at most once |
| Recursion depth | O(m·n) (moderate) | worst case the whole grid is one island |
O(m·n) (moderate)Every cell is touched a constant number of times, so the whole thing is O(m·n). The extra space is the recursion stack, which in the worst case (one giant snaking island) can reach O(m·n).
When this pattern shows up
Any grid problem about connected regions — number of islands, max area, flood fill, surrounded regions, rotting oranges — is the same move: scan the grid, and DFS or BFS to explore each region while marking cells visited. Overwriting the grid in place is the cheapest visited-set there is.
Do not forget to mark cells visited before recursing into neighbors. If you mark too late, two neighbors can each recurse back into the other and you loop forever or overcount. Also remember the bounds check is the base case that stops the DFS at the grid edge.
Practice
In the 4×4 example, the DFS from (0,0) visits cells (0,0), (0,1), (1,0), and (2,0). What area does it return, and what is max afterward?
1. Why do we overwrite a land cell to 0 during the DFS?
2. What does the dfs function return for a cell that is off-grid or water?
3. How is the area of one island computed?
4. What is the time complexity of this solution on an m×n grid?