← Visualizers
Graphs · Flood fill on a grid LeetCode 200

Number of Islands / visualized

The graph is implicit — no adjacency list exists, neighbours are computed by (r±1, c) and (r, c±1). Scan every cell; each time you land on un-sunk land, that is one new island, so sink the whole component before the scan moves on.

Execution

idle
Press Run to begin.
0 / 0
Speed

Java · running line

scan cursor current cell sinking now island finished water
O(m·n) time · O(m·n) worst-case recursion depth  ·  the cell is sunk before the four calls, not after — mark first, or the neighbours call straight back and the recursion never ends.