Spirals

K spirals walk outward with run lengths 1,1,2,2,... on an N by M grid; for each cell print the earliest step any spiral reached it, within a 10^100 step limit.

Medium6ImplementationSimulationMathArrayNo attempts yetTime limit1sMemory limit64 MB

Problem

There is a table with NN rows and MM columns. The table starts empty, and KK spirals are placed on it. Each spiral starts moving from its own starting cell, turning either clockwise or counter-clockwise.

One spiral moves by the following rules.

  • In step 1 the spiral is on its starting cell.
  • After that it walks straight runs one after another. The run lengths are 1,1,2,2,3,3,4,4,1, 1, 2, 2, 3, 3, 4, 4, \dots in that order, and a run of length LL is LL single-cell moves in the same direction.
  • The first run goes up, that is, toward smaller row numbers.
  • The direction turns once at the end of every run. A clockwise spiral repeats up, right, down, left. A counter-clockwise spiral repeats up, left, down, right.

All spirals move at the same time, and each of them advances one cell per step. A spiral sometimes leaves the table and comes back inside it later.

After exactly 1010010^{100} steps, the value of each cell is the number of the earliest step in which some spiral stood on that cell.

Image 1

Image 1: a spiral moving counter-clockwise

Image 2

Image 2: a spiral moving clockwise

Input

The first line contains NN, MM (1N,M501 \le N, M \le 50) and KK (1KN×M1 \le K \le N \times M).

Each of the next KK lines contains integers XiX_i, YiY_i and TiT_i (1XiN1 \le X_i \le N, 1YiM1 \le Y_i \le M, 0Ti10 \le T_i \le 1). XiX_i and YiY_i are the row and the column of the starting cell of the ii-th spiral. Ti=0T_i = 0 means clockwise and Ti=1T_i = 1 means counter-clockwise. No two spirals share a starting cell.

Output

Print NN lines with MM numbers each, separated by single spaces. The jj-th number on the ii-th line is the value of the cell in row ii and column jj.

Note

The third example is explained by the picture below.

Third example

To make the picture easier to read, the letter A is attached to the numbers left by the first spiral and the letter B to the numbers left by the second spiral. Only the first 20 steps of the first spiral and the first 21 steps of the second spiral are drawn. The gray cells are the cells of the table. The other cells lie outside the table and are drawn to show how the spirals move outside it.