Number of Islands, solved three ways

Try:
CALL
1 / 1

Code

The highlighted line runs next. The pill shows the live values in the running call.

Recursion tree

not called yet running now paused, waiting on a child call land call, finished (colour = island) ✕ returned at once (out / water / sunk)

Call stack

Grid

Rows on the left, columns on top; the outer ring is off the grid

1land 0water 0sunk (colour = island) current dfs call outoutside the grid

Keyboard: ← → step, Space play or pause. Grids up to 6×6; switching solution keeps the grid. All three are O(m·n) time (DSU with path compression is effectively linear). DFS and BFS sink cells in place; DSU leaves the grid untouched and is the one to reach for when land arrives over time (LeetCode 305).