DFS Island Counter - Advanced Visualizer

5x5 grid with real-time code execution tracking

Step: 0 / 0
Water (0)
Land (1)
Visited
Current (analyzing)
Click "Next" or "Play" to start the step-by-step execution.
Grid State (5x5)
Code Execution
def numIslands(grid):
count = 0
rows = len(grid)
cols = len(grid[0])
for i in range(rows):
for j in range(cols):
if grid[i][j] == '1': # Found island
count += 1
dfs(i, j) # Mark entire island as visited
return count
# --- DFS Helper Function ---
def dfs(row, col):
if row < 0 or row >= rows or col < 0 or col >= cols or grid[row][col] == '0':
return
grid[row][col] = '0' # Mark as visited
dfs(row + 1, col) # Down
dfs(row - 1, col) # Up
dfs(row, col + 1) # Right
dfs(row, col - 1) # Left
Call Stack
Execution Log