Code
Loading editor...
Tap Analyze to see visualization
Click Analyze to visualize
See step-by-step execution, variables, and output
Variables
Run code to see variables
Output
Console output will appear here
Click Analyze to visualize
See step-by-step execution, variables, and output
Run code to see variables
Console output will appear here
Visit every cell. When land is found, run DFS to mark every connected land cell as water. Increment a counter only for a new starting land component.
Every island is a connected region. Once DFS marks all cells in one island as visited, remaining unvisited land must belong to a new island.
Updated Feb 2026