We will represent the drawing area of MS Paint as a rectangular grid of unit squares divided into
rows
and
columns. Each square of the grid represents a single pixel that can be colored in one of the
different colors. When the user applies the so called bucket tool with color
on a pixel
which is
colored in the color
, then all pixels in the monochrome neighborhood of pixel
change their color to
. Monochrome neighborhood of a pixel
is a set of pixels that are reachable by walking from
in the four general directions (up, down, left and right) without changing the color of the pixel along the
way. Note that the pixel
is itself a part of its monochrome neighborhood.

You are given a starting image drawn in MS Paint along with
instructions that should be executed
in the given order. Each instruction tells you on which pixel should you apply the bucket tool and with
what color. Your task is to how the image looks like after all instructions are executed.
Input Specification
The first line contains integers
and
from the task description.
Each of the next
lines contains
non-negative integers less than
that represent the starting
image drawn in MS Paint. More precisely, the
-th number in the
-th row of the image represents the
color of the pixel
.
The next line contains an integer
from the task description.
The
-th of the next
lines contains integers
,
, and
,
which represent the
-th instruction that tells you to use the bucket tool with color
on the pixel
.
Output Specification
You should output the final state of the image in the same format as it was given in the input.
Constraints
Subtask | Points | Constraints |
1 | 8 |  |
2 | 9 |  |
3 | 31 |  Each pixel will in every moment be colored either in color or color . |
4 | 52 |  |
Sample Input 1
Copy
12 11
1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 0 0 0 1 1 1 1
1 1 1 0 0 0 0 0 1 1 1
1 1 0 0 0 0 0 0 0 1 1
1 0 0 0 2 2 2 0 0 0 1
1 0 0 0 2 2 2 0 0 0 1
1 0 0 0 2 2 2 0 0 0 1
1 0 0 0 0 0 0 0 0 0 1
1 1 0 0 0 2 0 0 0 1 1
0 1 1 0 0 2 0 0 1 1 0
0 0 1 1 0 0 0 1 1 0 0
0 0 0 1 1 1 1 1 0 0 0
2
5 5 3
6 2 4
Sample Output 1
Copy
1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 4 4 4 1 1 1 1
1 1 1 4 4 4 4 4 1 1 1
1 1 4 4 4 4 4 4 4 1 1
1 4 4 4 3 3 3 4 4 4 1
1 4 4 4 3 3 3 4 4 4 1
1 4 4 4 3 3 3 4 4 4 1
1 4 4 4 4 4 4 4 4 4 1
1 1 4 4 4 2 4 4 4 1 1
0 1 1 4 4 2 4 4 1 1 0
0 0 1 1 4 4 4 1 1 0 0
0 0 0 1 1 1 1 1 0 0 0
Explanation for Sample Output 1
The figure from the task description corresponds to the input of
the first example. White color corresponds to number
, red color corresponds to number
, blue color
corresponds to number
, green color corresponds to number
and yellow color corresponds to number
.
Sample Input 2
Copy
4 4
1 0 1 3
1 3 2 2
3 3 1 2
2 2 1 3
3
1 2 3
3 2 1
4 2 3
Sample Output 2
Copy
1 1 1 3
1 1 2 2
1 1 1 2
3 3 1 3
Sample Input 3
Copy
6 6
1 2 1 2 2 2
3 1 2 1 3 1
3 3 2 3 2 2
2 3 1 3 3 2
3 3 3 3 3 3
2 3 2 2 2 1
4
6 2 2
3 5 2
3 2 3
1 2 3
Sample Output 3
Copy
1 3 1 2 2 2
3 1 3 1 3 1
3 3 3 3 3 3
3 3 1 3 3 3
3 3 3 3 3 3
3 3 3 3 3 1
Comments