Number of Islands
BFS/DFS Flood Fill on 2D Grid
LeetCode 200 • Medium • Graphs
Input: grid = [["1","1","0","0","0"],["1","1","0","0","0"],["0","0","1","0","0"],["0","0","0","1","1"]] → Output: 3
For each unvisited '1' cell, run BFS to mark the entire island. Count how many BFS calls = islands.
Time
O(R×C)
visit each cell once
Space
O(R×C)
visited set + queue
Visited: 0/7
Queue: 0
Islands: 0
Processing
In Queue
Island 1
Island 2
Island 3
Unvisited land
Water
Queue
empty
Visited
{ }
Count
0
Ready
Press Play to watch BFS flood fill discover islands, or Step to advance one operation at a time.
Each unvisited '1' cell triggers a new BFS, marking one complete island.
Each unvisited '1' cell triggers a new BFS, marking one complete island.