Each island is a connected group of 1s in a binary grid. A DFS flood fill from every unvisited land cell measures its size in O(1) extra work per cell — the max of those sizes is the answer.
Given an m × n binary grid of 0s (water) and 1s (land), return the area of the largest island — a 4-directionally connected group of 1s. For the grid:[[1,1,0,0,0],[1,1,0,0,0],[0,0,0,1,1],[0,0,0,1,1]]
there are two islands, each of area 4, so the answer is 4. If the grid is all water, return 0.
1. Running max over all fill sizes gives the answer in one pass.r then c. Skip water and already-visited cells — O(m·n) work total, not per island.dfs(r, c). This is the seed of a new island.grid[r][c] = 0) so it's never revisited. Recurse in all 4 directions; each out-of-bounds or water call returns 0.return 1 + dfs(up) + dfs(down) + dfs(left) + dfs(right) — each live cell contributes exactly 1.maxArea and keep the larger.dfs, before any recursive calls.Replace the recursive DFS with an iterative BFS using a queue. Identical complexity; avoids call-stack overflow on very large grids (though LeetCode constraints make this academic here). Union-Find also works but adds code without benefit for this problem.
1▶function maxAreaOfIsland(grid: number[][]): number {2▶ const rows = grid.length;3▶ const cols = grid[0].length;4▶ let maxArea = 0;56 function dfs(r: number, c: number): number {7 if (r < 0 || r >= rows || c < 0 || c >= cols) return 0;8 if (grid[r][c] !== 1) return 0; // water or already visited9 grid[r][c] = 0; // mark visited by zeroing in-place10 return (11 1 +12 dfs(r + 1, c) +13 dfs(r - 1, c) +14 dfs(r, c + 1) +15 dfs(r, c - 1)16 );17 }1819 for (let r = 0; r < rows; r++) {20 for (let c = 0; c < cols; c++) {21 if (grid[r][c] === 1) {22 maxArea = Math.max(maxArea, dfs(r, c));23 }24 }25 }2627 return maxArea;28}
function maxAreaOfIsland(grid: number[][]): number {
const rows = grid.length;
const cols = grid[0].length;
let maxArea = 0;
function dfs(r: number, c: number): number {
if (r < 0 || r >= rows || c < 0 || c >= cols) return 0;
if (grid[r][c] !== 1) return 0; // water or already visited
grid[r][c] = 0; // mark visited by zeroing in-place
return (
1 +
dfs(r + 1, c) +
dfs(r - 1, c) +
dfs(r, c + 1) +
dfs(r, c - 1)
);
}
for (let r = 0; r < rows; r++) {
for (let c = 0; c < cols; c++) {
if (grid[r][c] === 1) {
maxArea = Math.max(maxArea, dfs(r, c));
}
}
}
return maxArea;
}rows and cols once. maxArea starts at 0; if the grid is all water it stays 0 and is returned as-is.0. A 0 cell (water or already visited) also returns 0. These two checks are the "wall" that stops the flood fill from spilling.grid[r][c] = 0 before recursing prevents any neighbor from queuing this cell again. This is the in-place visited trick — no extra boolean[][] needed. Caveat: it mutates the input; restore it if the caller expects the grid unchanged.1(this cell) plus the sum of the four recursive calls. Each call either returns 0 immediately or expands deeper into the island. The total sum bubbles up as the island's area.dfs zeroes every cell it visits, subsequent passes over already-explored land are instant 0-returns — the total work across all DFS calls is still O(m·n).boolean[][] visited array, or clone the grid before running.[r, c] pairs during the DFS and store the best set alongsidemaxArea.dirs array — the rest of the code is unchanged.| "largest / max connected group of 1s" | DFS flood fill returning area |
| binary grid, 4-directional neighbors | recursive DFS with in-place visited mark |
| "count islands" or "number of components" | flood fill from each unvisited seed |
| grid traversal, avoid revisit | zero-out / visited[][] before recursing |
function maxAreaOfIsland(grid: number[][]): number {
const rows = grid.length, cols = grid[0].length;
let maxArea = 0;
function dfs(r: number, c: number): number {
if (r < 0 || r >= rows || c < 0 || c >= cols) return 0;
if (grid[r][c] !== 1) return 0;
grid[r][c] = 0; // mark visited
return 1 + dfs(r+1,c) + dfs(r-1,c) + dfs(r,c+1) + dfs(r,c-1);
}
for (let r = 0; r < rows; r++)
for (let c = 0; c < cols; c++)
if (grid[r][c] === 1)
maxArea = Math.max(maxArea, dfs(r, c));
return maxArea;
}grid[r][c] = 0 before recursing?[[1,1,0],[0,1,0],[0,0,1]], what does the algorithm return?