Flood Fill

EasyGraphs
Next
GraphMatrixDFSBFS

An image is a grid of colour numbers. Starting from the pixel at (sr, sc), recolour it and every pixel connected to it through up, down, left and right moves that shares its original colour, using the new colour. Print the resulting image.

Input

Line 1: r and c. Next r lines: c integers each (the colours). Last line: sr, sc and the new colour.

Output

Print the final image, one row per line.

Constraints

1 <= r, c <= 50 0 <= colour <= 65535

Example 1
Input
3 3
1 1 1
1 1 0
1 0 1
1 1 2
Output
2 2 2
2 2 0
2 0 1

The connected group of 1s around (1, 1) becomes 2; the 0s and the corner 1 are untouched.

Hints

Flood Fill - Easy Graphs Problem | Skill Shiksha