 All Problems
Flood Fill
easy
graph
depth-first search
breadth-first search
matrix
amazon
google
microsoft
facebook

An image is represented by an m x n integer grid image where image[i][j] represents the pixel value of the image.

You are also given three integers sr, sc, and color. Perform a flood fill starting from pixel image[sr][sc]: change the color of the pixel and all connected pixels of the same original color to the new color. Return the modified image.

Example 1:

Input:
3 3
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

Example 2:

Input:
2 2
0 0
0 0
sr=0 sc=0 color=0
Output:
0 0
0 0

Constraints:

  • m == image.length, n == image[i].length
  • 1 ≤ m, n ≤ 50
  • 0 ≤ image[i][j], color < 2¹⁶
  • 0 ≤ sr < m, 0 ≤ sc < n

Input format: First line: m n. Then m rows. Last line: sr sc color.

Output format: Modified grid.

Run to check your code against the sample cases, or submit to run every case