DSA Visualizer
GRID

Number of Islands

Count the patches of land in a grid of 1s and 0s. Click cells to redraw the map and watch each island get its own colour.

Count the connected patches of land in a grid of 1s and 0s, sinking each island as you find it.

0
1
2
3
4
0
1
2
3
Call stack
Empty
Variablescount=0
Count starts at 0. Scan the grid row by row, looking for land (1) that has not been visited.
0
IslandsIslands
0
VisitedLand visited
7
LandLand total
1 / 23

Pseudocode

Number of islands
1count = 0
2for each cell (r, c) in row-major order:
3 if grid[r][c] is land and not visited:
4 count += 1
5 sink(r, c) # DFS marks the whole island visited
6return count

Legend

Visiting nowLandIsland (one colour each)

Grid

Click a cell to turn land into water or water into land.

Want exact values? Edit this grid in the 2D Array Visualizer →

Tip: Space play/pause · ← → step

Learn number of islands

Number of islands counts the connected patches of land in a grid of 1s (land) and 0s (water). Scan every cell; each time you find land nobody has visited, that is a new island, and a DFS sinks the whole island so it is never counted again.

All three are the same idea in different clothes: a grid is a graph where every cell is joined to its four neighbours, and each algorithm walks that graph while remembering which cells it has already seen. Compare with flood fill and bfs shortest path.

Each cell is visited at most once and looks at four neighbours, so every one of them runs in O(rows · cols) time. The queue or call stack can grow to that size too.

How number of islands works

Look at a map where 1 is land and 0 is water and count the islands. Your eyes do it in one glance. A program cannot glance, so it does something more careful: it walks the map cell by cell, and each time it steps onto land it has never seen, it says “new island”, then explores that whole island so it will not be counted again.

The exploring step is exactly flood fill. From the new land cell, visit every connected land cell through up, down, left and right moves, and mark each one. The classic trick is to mark by sinking: turn the 1 into a 0. Then the outer scan, which only reacts to 1s, walks straight past the rest of that island.

In the visualizer every island gets its own colour and its number as a badge, and the strip shows the call stack of the recursive sink function as it explores.

The loop shape

Two nested loops walk the grid in row-major order. Inside, one if: is this cell land that has not been visited? If so, count += 1 and call sink(r, c).

sink returns immediately for anything outside the grid, any water, and any land already visited. Otherwise it marks the cell and calls itself on the four neighbours. The order of those four calls changes the visit order but never the count.

A worked example: three islands

This is the second example from LeetCode 200. The scan meets land at row 0, column 0 and sinks a square of four cells: island 1. It passes the rest of that square without stopping, meets a lone 1 at row 2, column 2: island 2. The two 1s in the bottom-right corner become island 3. Seven land cells, three islands.

Notice that island 2 touches island 1 only at a corner. Corners do not connect, so they stay separate.

Where you'll meet it

  • Connected components. The same scan-and-fill counts blobs in an image, rooms in a floor plan, or clusters of pixels above a threshold.
  • Games. Groups of stones in Go, territories on a strategy map, and matching blocks in tile games.
  • Interviews. LeetCode 200 is one of the most asked grid questions, with variants that ask for the largest island (695), the number of enclosed regions, or counting as land is added (305).

Mistakes beginners make

  • Counting every land cell. The count goes up only when the scan finds land that has not been visited. The flood fill is what stops the rest of the island from counting.
  • Including diagonals. Unless the problem says so, only the four side neighbours connect. Diagonal moves merge islands that should be separate.
  • Forgetting that sinking destroys the input. Fine in an interview, surprising in real code. Copy the grid or keep a visited set.
  • Deep recursion on a big island. A 1000 × 1000 grid of all land is one island a million cells deep. Use BFS or an explicit stack there.

Next, search a grid for the fewest steps instead of the most cells: BFS shortest path →

Number of islands questions

What counts as one island?

A group of land cells (1s) where you can walk from any cell to any other by moving up, down, left or right through land. Two land cells that touch only at a corner are in different islands, because the standard problem is 4-connected. If your problem allows diagonal moves, add the four diagonal neighbours to the search.

Why does the DFS change the grid?

It turns each visited 1 into a 0, sinking the island, so the outer scan never counts the same island twice. It is a compact way to remember what has been visited, but it destroys the input. If you need the grid afterwards, keep a separate visited set or copy the grid first. The visualizer keeps the grid and uses a visited set.

Is it faster with BFS, DFS or union-find?

All three are O(rows × cols): every cell is looked at a constant number of times. DFS is the shortest code, BFS avoids deep recursion on large islands, and union-find shines when land is added over time and the count has to be updated after each change, as in LeetCode 305.

How is this different from flood fill?

It is flood fill in a loop. Flood fill spreads from one cell you choose. Number of islands scans every cell in row-major order and, whenever it meets land nobody has visited, runs a flood fill from there and adds one to the count.