You are given an image represented by an m x n grid of integers image, where image[i][j] represents the pixel value of the image. You are also given three integers sr, sc, and color. Your task is to perform a flood fill on the image starting from the pixel image[sr][sc].
To perform a flood fill:
color.Return the modified image after performing the flood fill.
Example 1:
Input: image = [[1,1,1],[1,1,0],[1,0,1]], sr = 1, sc = 1, color = 2
Output: [[2,2,2],[2,2,0],[2,0,1]]
Explanation:

From the center of the image with position (sr, sc) = (1, 1) (i.e., the red pixel), all pixels connected by a path of the same color as the starting pixel (i.e., the blue pixels) are colored with the new color.
Note the bottom corner is not colored 2, because it is not horizontally or vertically connected to the starting pixel.
Example 2:
Input: image = [[0,0,0],[0,0,0]], sr = 0, sc = 0, color = 0
Output: [[0,0,0],[0,0,0]]
Explanation:
The starting pixel is already colored with 0, which is the same as the target color. Therefore, no changes are made to the image.
Constraints:
m == image.lengthn == image[i].length1 <= m, n <= 500 <= image[i][j], color < 2160 <= sr < m0 <= sc < nWhen you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
Imagine looking at a grid of colored tiles and wanting to repaint a connected patch of tiles with a new color. The brute force approach involves blindly checking every single tile in the entire grid to see if it is connected to your starting point and shares the original color.
Here's how the algorithm would work step-by-step:
def flood_fill(image_grid, start_row, start_column, new_color): original_color = image_grid[start_row][start_column] # Stop immediately if the starting color already matches the target to prevent infinite recursion if original_color == new_color: return image_grid total_rows = len(image_grid) total_columns = len(image_grid[0]) def recursive_fill(current_row, current_column): # Boundary check ensures we do not attempt to inspect indices lying outside the grid dimensions if current_row < 0 or current_row >= total_rows or current_column < 0 or current_column >= total_columns: return # Halt traversal on this path if the current tile does not match the original color we are replacing if image_grid[current_row][current_column] != original_color: return image_grid[current_row][current_column] = new_color recursive_fill(current_row - 1, current_column) recursive_fill(current_row + 1, current_column) recursive_fill(current_row, current_column - 1) recursive_fill(current_row, current_column + 1) recursive_fill(start_row, start_column) return image_gridThe optimal strategy is like spreading paint on a canvas, where you start at your chosen spot and let the new color flow outward to all touching neighbors of the same original color. By tackling connected areas step by step and keeping track of where you have already been, you change the entire region efficiently without getting stuck in loops.
Here's how the algorithm would work step-by-step:
class Solution:
def floodFill(self, image_grid: list[list[int]], start_row: int, start_column: int, target_color: int) -> list[list[int]]:
original_color = image_grid[start_row][start_column]
# Prevent infinite loops and redundant work if the color is already target.
if original_color == target_color:
return image_grid
total_rows = len(image_grid)
total_columns = len(image_grid[0])
def recursive_paint(current_row: int, current_column: int):
# Stop recursion if we drift outside grid boundaries or hit a different color.
if current_row < 0 or current_row >= total_rows or current_column < 0 or current_column >= total_columns:
return
if image_grid[current_row][current_column] != original_color:
return
# Update current cell to target color before visiting its neighbors.
image_grid[current_row][current_column] = target_color
recursive_paint(current_row - 1, current_column)
recursive_paint(current_row + 1, current_column)
recursive_paint(current_row, current_column - 1)
recursive_paint(current_row, current_column + 1)
recursive_paint(start_row, start_column)
# Return the modified grid containing the completed flood fill.
return image_grid| Case | How to Handle |
|---|---|
| Starting pixel already has the target color | To prevent infinite recursion, immediately check if the original color equals the new color and return the grid unchanged. |
| Grid is empty, null, or has dimensions of zero | Validate the input dimensions at the start and return the grid immediately if it is empty. |
| Starting coordinates are out of the grid bounds | Check boundary conditions before accessing the grid to prevent index out of bounds exceptions. |
| Minimum-sized 1x1 grid | The algorithm will update the single pixel's color and safely return the grid without triggering out of bounds recursive calls. |
| Extremely large grid exceeding recursion stack limits | Use an iterative approach with a queue or stack instead of pure recursion to avoid stack overflow errors on massive inputs. |
| Grid where all pixels already match the starting color | The algorithm will traverse and update every single cell in the grid efficiently within time complexity limits. |
| Pixels contain negative numbers, zeros, or extreme color values | Since the algorithm only checks for equality against the original color, arbitrary integer values are handled transparently. |