Max Area of Island
Find the maximum area of a connected island in a 2D binary grid using DFS.
01The problem
You are given an m x n binary matrix grid. An island is a group of 1's (representing land) connected 4-directionally (horizontal or vertical.) You may assume all four edges of the grid are surrounded by water.
The area of an island is the number of cells with a value 1 in the island.
Return the maximum area of an island in grid. If there is no island, return 0.
| Input | Output | Why |
|---|---|---|
grid = [[0,0,1,0,0],[0,1,1,1,0],[0,0,1,0,0],[1,1,0,0,0]] | 5 | The cross-shaped island in rows 0-2 has area 5 (1+3+1). Second island has area 2. |
Constraints
m == grid.lengthn == grid[i].length1 <= m, n <= 50grid[i][j]is either0or1.
02How to spot it
- "Max area of an island": Extension of Number of Islands (#200). Instead of simply counting components, the DFS sums the area of each component:
1 + area(up) + area(down) + area(left) + area(right).
03Pseudocode
area(r, c):
if outside or cell != 1: return 0
cell = 0
return 1 + area of the 4 neighbours
best = max of area(r, c) over all cells04Watch it work
grid
max area
0
Find max island area: compute area of each island with DFS, zeroing visited land.
Pseudocode
- area(r, c):
- if outside or cell != 1: return 0
- cell = 0
- return 1 + area of the 4 neighbours
- best = max of area(r, c) over all cells
Variables
- best
- 0
05Step-by-step dry run
We trace on grid = [[0,0,1,0,0],[0,1,1,1,0],[0,0,1,0,0],[1,1,0,0,0]]:
| Scan cell | Land? | DFS area sum | best updated |
|---|---|---|---|
(0, 0) to (0, 1) | 0 | - | 0 |
(0, 2) | 1 | 1 + (1 + 1 + 1) + 1 = 5 | max(0, 5) = 5 |
(3, 0) | 1 | 1 + 1 = 2 | max(5, 2) = 5 |
Output: 5.
06Java solution
public class Solution {
public static int maxAreaOfIsland(int[][] grid) {
int maxArea = 0;
int m = grid.length;
int n = grid[0].length;
for (int r = 0; r < m; r++) {
for (int c = 0; c < n; c++) {
if (grid[r][c] == 1) {
maxArea = Math.max(maxArea, area(grid, r, c));
}
}
}
return maxArea;
}
private static int area(int[][] grid, int r, int c) {
if (r < 0 || r >= grid.length || c < 0 || c >= grid[0].length || grid[r][c] != 1) {
return 0;
}
grid[r][c] = 0;
return 1 + area(grid, r + 1, c)
+ area(grid, r - 1, c)
+ area(grid, r, c + 1)
+ area(grid, r, c - 1);
}
public static void main(String[] args) {
int[][] grid = {
{0, 0, 1, 0, 0},
{0, 1, 1, 1, 0},
{0, 0, 1, 0, 0},
{1, 1, 0, 0, 0}
};
System.out.println(maxAreaOfIsland(grid));
}
}5
07Why it works
The recurrence 1 + sum(neighborAreas) naturally sums the size of the connected component. Sinking each cell grid[r][c] = 0 immediately before recursing ensures no cell is counted more than once.
08Time and space
- Time complexity:
O(m * n). Each cell is checked by the loop and visited by DFS at most once. - Space complexity:
O(m * n)recursion stack memory for the deepest connected component.
09Edge cases
- All zeros: Returns 0.
- All ones: Returns
m * n.
10Common mistakes
- Forgetting to sink the cell: Calling
areawithout changinggrid[r][c] = 0causes immediate infinite recursion between adjacent cells.
11Other approaches
- BFS with Queue: Keep a running count of cells dequeued in the current island.
12In the interview
- Opening line: "I will scan each cell. When I find
1, I run a DFS that sinks the cell to0and returns1 + area of 4 neighbors. I track the maximum area seen."
13Quiz
What is the base case return value when a neighbor is water (0) or outside the grid?
- (Recommended) 0
- 1
- -1
- null
Answer: 0. Water and out-of-bounds cells contribute 0 area to the island.
14Solve it yourself
Open LeetCode #695 and submit the solution.