373. Flood Fill
This is the paint bucket in an image editor.
A grid of single digits is given, together with a starting cell and a new colour. Recolour the starting cell and every cell reachable from it through cells of the SAME original colour, moving up, down, left and right only. Then print the whole grid.
Cells of a different colour block the spread, and diagonals do not connect.
There is one case that separates a working solution from one that hangs: when the new colour equals the colour already there. A fill that repaints a cell to the colour it is looking for will keep finding that cell "fillable" forever. The sixth test is exactly this, and it must print the grid unchanged.
Rows and columns are numbered from 0 here, matching how a grid is indexed in every language, not from 1 like the vertices in the rest of this topic.
Constraints - `1 ≤ r ≤ 1000` - `1 ≤ c ≤ 1000` - `0 ≤ sr < r` - `0 ≤ sc < c` - `0 ≤ colour ≤ 9` - Every grid character is a digit `0` to `9`.
Input
The first line contains five integers r, c, sr, sc and colour: the
number of rows, the number of columns, the starting row, the starting column,
and the new colour. Rows are numbered 0 to r - 1 and columns 0 to c - 1.
Each of the next r lines contains c digit characters with no spaces.
Output
Print the grid after the fill, r lines of c characters.
Hints
Four rungs, in order. The last two open once you have submitted an attempt. A wrong one counts.