Number of Islands
Difficulty: Medium
You're given a grid where each cell is "1" (land) or "0" (water). An island is a group of land cells connected horizontally or vertically, not diagonally. Land cells that touch only at a corner are separate islands.
Count the islands in the grid.
Examples
Input: grid = [["1","1","0","0","0"],["1","1","0","0","0"],["0","0","1","0","0"],["0","0","0","1","1"]]
Output: 3
The top-left block of four 1s is one island, the single 1 in the middle is a second island, and the two 1s in the bottom-right corner (connected to each other) form a third.
Input: grid = [["1","1","1"],["0","1","0"],["1","1","1"]]
Output: 1
Every land cell is reachable from every other land cell through horizontal/vertical neighbors, so it's all one island.
Input: grid = [["0","0"],["0","0"]]
Output: 0
There's no land at all.
Constraints
1 <= rows, cols <= 300
grid[i][j] is either "0" or "1"
Approach
The key idea is traversal: start from one cell and visit every cell connected to it. It's like flood-fill in a paint program: click one land cell, and the fill reaches every land cell connected by up, down, left or right steps.
Scan the grid in order. Each unvisited "1" is a new island: add one to the count, then spread out and mark every connected land cell so it's never counted again. Then keep scanning.
You can spread in two ways, with the same result:
- Depth-first search (DFS): go as far as possible in one direction (recursively) before backtracking.
- Breadth-first search (BFS): visit all direct neighbors first, using a queue, before moving further out.
They differ only in visiting order and in using the call stack (DFS) or an explicit queue (BFS).
Solutions
DFS - Recursive Flood Fill
Scan every cell. An unvisited "1" is a new island: count it, then recursively visit every connected land cell, turning each into water ("0") so it's never revisited. The recursion stops at water, grid edges and visited cells.
function numIslands(grid) {
const rows = grid.length;
const cols = grid[0].length;
let count = 0;
function dfs(r, c) {
if (r < 0 || r >= rows || c < 0 || c >= cols || grid[r][c] !== "1") {
return;
}
grid[r][c] = "0"; // mark this land cell as visited
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") {
count++;
dfs(r, c);
}
}
}
return count;
}Time: O(rows × cols) · Space: O(rows × cols)
BFS - Iterative Flood Fill
Same idea with a queue instead of recursion. For a new island, mark its cell visited and queue it. Then pop cells and mark and queue their unvisited land neighbors until the queue is empty. This avoids deep recursion on very large grids.
function numIslands(grid) {
const rows = grid.length;
const cols = grid[0].length;
let count = 0;
for (let r = 0; r < rows; r++) {
for (let c = 0; c < cols; c++) {
if (grid[r][c] !== "1") continue;
count++;
grid[r][c] = "0";
const queue = [[r, c]];
while (queue.length > 0) {
const [row, col] = queue.shift();
const neighbors = [[row + 1, col], [row - 1, col], [row, col + 1], [row, col - 1]];
for (const [nr, nc] of neighbors) {
if (nr >= 0 && nr < rows && nc >= 0 && nc < cols && grid[nr][nc] === "1") {
grid[nr][nc] = "0";
queue.push([nr, nc]);
}
}
}
}
}
return count;
}Time: O(rows × cols) · Space: O(rows × cols)