Browse curriculum

Number of Islands (LeetCode 200)

Number of Islands (LeetCode 200): count the number of connected groups of 1s in a grid.

Examples

Input:grid = [['1','1','0'], ['1','1','0'], ['0','0','1']]
Output:2
Explain:

Island 1 (Top Left), Island 2 (Bottom Right).

Problem Understanding

An m x n grid marks land with '1' and water with '0'. Count the islands: groups of land cells joined through up, down, left or right neighbours, with water or the grid's edge all around them.

Algorithm Strategy

We need to find Connected Components.

  1. Iterate: Loop through every cell (r, c) of the grid.
  2. Trigger: If we find a land cell '1', we found a NEW island!
    • Increment count.
    • Sink the Island: Launch DFS from this cell to visit all connected lands and mark them as visited (e.g., change '1' to '0').
  3. Continue: If we see a '0' (or visited cell), skip it.

Visualization

1 = LAND · 0 = WATER · COLOURED 0 = SUNK WITH ITS ISLAND

NEW ISLAND

islands

1

Answer

:

call stack

No dfs running

islands found

#1: 0 cells

 

(0, 0) is '1': land no DFS has reached: islands = 1. Sink it

Next

dfs(0, 0): land, sink it to '0' as part of island #1

Free preview: the first 24 of 80 steps

grid

Scan the grid row by row. When the scan finds land, the island counter goes up and DFS sinks every cell of that island to '0', calling down, up, right, then left. Each island keeps its own colour after it is sunk, so when the scan later passes over those cells you can see they were already counted.

Dry Run

Grid:

1 1 0
1 0 0
0 0 1
Step Where Action Islands
1 scan (0,0) '1': new island. islands = 1. Call dfs(0,0). 1
2 dfs(0,0) Sink (0,0). Call down. 1
3 dfs(1,0) Sink (1,0). Down (2,0) is water, up (0,0) is already sunk, right (1,1) is water, left (1,-1) is outside: all return. 1
4 back in dfs(0,0) Up (-1,0) is outside: returns. Call right. 1
5 dfs(0,1) Sink (0,1). Down (1,1) is water, up (-1,1) is outside, right (0,2) is water, left (0,0) is already sunk: all return. 1
6 back in dfs(0,0) Left (0,-1) is outside: returns. The first island is fully sunk. 1
7 scan (0,1) to (2,1) (0,1) and (1,0) are now '0'; (0,2), (1,1), (1,2), (2,0) and (2,1) are water: all skipped. 1
8 scan (2,2) '1': new island. islands = 2. Call dfs(2,2). 2
9 dfs(2,2) Sink (2,2). Down and right are outside; up (1,2) and left (2,1) are water: all return. 2
End - Every cell scanned. Return 2. 2

Edge Cases & Common Mistakes

  • Out of Bounds: Always check r < 0, c < 0, r >= rows, c >= cols first in DFS.
  • Cyclic Dependency: Without marking visited (or changing '1' to '0'), DFS will run infinitely in a loop of lands.
  • All Water: No cell is 1, so no DFS ever starts and the answer is 0. (LeetCode guarantees at least one row and one column.)
  • Diagonal Connection: The problem usually specifies horizontal/vertical connections only (4-way). Don't include diagonals unless specified.

Solution (Sinking Method)

Modifying the grid in-place to save space.

Number of Islands Code
1var numIslands = function(grid) {
2 const rows = grid.length, cols = grid[0].length;
3
4 function dfs(r, c) {
5 if (r < 0 || r >= rows || c < 0 || c >= cols || grid[r][c] !== '1') {
6 return;
7 }
8 grid[r][c] = '0';
9 dfs(r + 1, c);
10 dfs(r - 1, c);
11 dfs(r, c + 1);
12 dfs(r, c - 1);
13 }
14
15 let islands = 0;
16 for (let r = 0; r < rows; r++) {
17 for (let c = 0; c < cols; c++) {
18 if (grid[r][c] === '1') {
19 islands++;
20 dfs(r, c);
21 }
22 }
23 }
24 return islands;
25};

Number of Islands in Python, Java, C++, TypeScript and C

Number of Islands in Python
class Solution:
    def numIslands(self, grid: List[List[str]]) -> int:
        rows, cols = len(grid), len(grid[0])

        def dfs(r: int, c: int) -> None:
            if r < 0 or r >= rows or c < 0 or c >= cols or grid[r][c] != '1':
                return
            grid[r][c] = '0'
            dfs(r + 1, c)
            dfs(r - 1, c)
            dfs(r, c + 1)
            dfs(r, c - 1)

        islands = 0
        for r in range(rows):
            for c in range(cols):
                if grid[r][c] == '1':
                    islands += 1
                    dfs(r, c)
        return islands
Number of Islands in Java
class Solution {
    public int numIslands(char[][] grid) {
        int islands = 0;
        for (int r = 0; r < grid.length; r++) {
            for (int c = 0; c < grid[0].length; c++) {
                if (grid[r][c] == '1') {
                    islands++;
                    dfs(grid, r, c);
                }
            }
        }
        return islands;
    }

    private void dfs(char[][] grid, int r, int c) {
        if (r < 0 || r >= grid.length || c < 0 || c >= grid[0].length || grid[r][c] != '1') {
            return;
        }
        grid[r][c] = '0';
        dfs(grid, r + 1, c);
        dfs(grid, r - 1, c);
        dfs(grid, r, c + 1);
        dfs(grid, r, c - 1);
    }
}
Number of Islands in C++
class Solution {
public:
    int numIslands(vector<vector<char>>& grid) {
        int islands = 0;
        for (int r = 0; r < (int)grid.size(); r++) {
            for (int c = 0; c < (int)grid[0].size(); c++) {
                if (grid[r][c] == '1') {
                    islands++;
                    dfs(grid, r, c);
                }
            }
        }
        return islands;
    }

private:
    void dfs(vector<vector<char>>& grid, int r, int c) {
        if (r < 0 || r >= (int)grid.size() || c < 0 || c >= (int)grid[0].size() || grid[r][c] != '1') {
            return;
        }
        grid[r][c] = '0';
        dfs(grid, r + 1, c);
        dfs(grid, r - 1, c);
        dfs(grid, r, c + 1);
        dfs(grid, r, c - 1);
    }
};
Number of Islands in TypeScript
function numIslands(grid: string[][]): number {
    const rows = grid.length, cols = grid[0].length;

    function dfs(r: number, c: number): void {
        if (r < 0 || r >= rows || c < 0 || c >= cols || grid[r][c] !== '1') {
            return;
        }
        grid[r][c] = '0';
        dfs(r + 1, c);
        dfs(r - 1, c);
        dfs(r, c + 1);
        dfs(r, c - 1);
    }

    let islands = 0;
    for (let r = 0; r < rows; r++) {
        for (let c = 0; c < cols; c++) {
            if (grid[r][c] === '1') {
                islands++;
                dfs(r, c);
            }
        }
    }
    return islands;
}
Number of Islands in C
static void dfs(char** grid, int rows, int cols, int r, int c) {
    if (r < 0 || r >= rows || c < 0 || c >= cols || grid[r][c] != '1') {
        return;
    }
    grid[r][c] = '0';
    dfs(grid, rows, cols, r + 1, c);
    dfs(grid, rows, cols, r - 1, c);
    dfs(grid, rows, cols, r, c + 1);
    dfs(grid, rows, cols, r, c - 1);
}

int numIslands(char** grid, int gridSize, int* gridColSize) {
    int islands = 0;
    for (int r = 0; r < gridSize; r++) {
        for (int c = 0; c < gridColSize[r]; c++) {
            if (grid[r][c] == '1') {
                islands++;
                dfs(grid, gridSize, gridColSize[0], r, c);
            }
        }
    }
    return islands;
}

More practice: DSA interview questions with answers · patterns cheat sheet

The approach, step by step

  1. Scan every cell

    Loop over the grid row by row.

  2. Count new land

    When a cell is '1', add one to the island count and call dfs on it.

  3. Sink the island

    dfs sets the cell to '0' and recurses down, up, right and left, stopping at the edges and at water.

Frequently asked questions

Why sink the island instead of keeping a visited set?

Setting land to 0 marks it as seen without any extra memory, and the scan then skips it like water. If the grid must not change, use a visited grid instead.


Why are diagonal cells not part of the same island?

The problem connects land only horizontally and vertically, so DFS looks at the four side neighbours. Land that touches only at a corner is a separate island.


What is the time complexity of Number of Islands?

O(m·n). The scan visits every cell once, and each land cell is sunk once, so every DFS call after that returns immediately.

New to the technique behind this problem? Watch it run step by step:

Depth First Search Visualization

Applying for software engineering roles? Free, no sign-up:

AI resume checker: match your resume to the job description