#P3549. 「COI 2020」Paint

「COI 2020」Paint

Description

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

You are given a starting image drawn in MS Paint along with QQ 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

The first line contains integers RR and SS from the task description.
Each of the next RR lines contains SS non-negative integers less than 100 000100\ 000 that represent the starting image drawn in MS Paint. More precisely, the jj-th number in the ii-th row of the image represents the color of the pixel (i,j)(i, j).
The next line contains an integer QQ from the task description.
The ii-th of the next QQ lines contains integers rir_i, sis_i and cic_i (1riR1 ≤ r_i ≤ R, 1siS1 ≤ s_i ≤ S, 0ci<100 0000 ≤ c_i < 100\ 000), which represent the ii-th instruction that tells you to use the bucket tool with color cic_i on the pixel (ri,si)(r_i, s_i).

Output

You should output the final state of the image in the same format as it was given in the input.

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
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
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
1 1 1 3
1 1 2 2
1 1 1 2
3 3 1 3
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
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

Scoring

Subtask Score Constraints
11 88 1RS10 0001 ≤ R · S ≤ 10\ 000, 1Q10 0001 ≤ Q ≤ 10\ 000
22 99 R=1R = 1, 1S200 0001 ≤ S ≤ 200\ 000, 1Q100 0001 ≤ Q ≤ 100\ 000
33 3131 1RS200 0001 ≤ R · S ≤ 200\ 000, 1Q100 0001 ≤ Q ≤ 100\ 000 Each pixel will in every moment be colored either in color 00 or color 11.
44 5252 1RS200 0001 ≤ R · S ≤ 200\ 000, 1Q100 0001 ≤ Q ≤ 100\ 000