Fill connected pixels with a new color using BFS traversal
Flood fill is a connected component traversal. BFS explores cells in "wavefront" order—all cells at distance d from source are processed before distance d+1. The visited check (or immediate color change) ensures each cell enters the queue exactly once.
DFS and BFS have identical time complexity here. BFS may be preferable for very large connected regions to avoid stack overflow.
The queue size at any moment equals the "frontier"—cells discovered but not yet processed. For BFS, this is bounded by the largest wavefront, which can approach O(m × n) for certain shapes. In practice, the frontier is often O(min(m, n)).
Changing the color in-place serves as the visited marker—no separate visited array needed, but the original image is modified.