B. The floor is lava!

Time limit: 2s Memory limit: 256MB Input: Output:

You finally trapped Lavutz and his K1K - 1 friends in a room (in total there are KK people). The room is very interesting, as it is split in nn rows and mm columns, and for each row ii and column jj (1in1 \leq i \leq n and 1jm1 \leq j \leq m) there is a cell which is at ai,ja_{i,j} units of height (distance in units from the ground). A person can move from cell (ii, jj) to one of the cells (i1i-1, jj), (i+1i+1, jj), (ii, j1j-1), (ii, j+1j+1) (if that doesn't go out of the boundaries of the room) in exactly 11 second, or they can just stay in cell (ii, jj).

You have a device which can raise the level of the lava, which is initially raised at level 00, but you don't want to hurt Lavutz or his friends. A person is hurt if the lava is raised at a level greater than the height of the cell where the person is at that moment.

Task

Now, you want to find for each LL from 11 to nmn \cdot m what is the minimum time you need to wait such that you can raise the lava to level LL and nobody gets hurt, or 1-1 if for any amount of time you will wait, you can't raise the lava to level LL without hurting anybody.

Input data

On the first line there are 33 integers: nn, mm (1n,m7001 \leq n, m \leq 700)- the number of rows and columns and KK (1K10 0001 \leq K \leq 10 \ 000) - the number of people in the room.

On the next nn lines, there will be ai,ja_{i, j} (1ai,jnm1 \leq a_{i, j} \leq n \cdot m) the height of cell (ii, jj). (1in1 \leq i \leq n and 1jm1 \leq j \leq m).

For each of the next KK lines there will be two integers xx, yy (1xn1 \leq x \leq n and 1ym1 \leq y \leq m) - the starting position of the KK people.

Output data

You will print nmn \cdot m integers. The ii-th integer is the answer for level ii.

Constraints and clarifications

  • Multiple people can move at the same time.
  • Multiple people can be in the same cell at the same time.
  • You can raise the lava to level 11 without hurting anybody (each person is at a height of at least 11).

Example

stdin

6 5 6
30 14 11 22 16
7 5 6 3 23
20 1 5 2 17
21 1 4 2 6
17 7 3 24 3
26 25 13 14 10
2 2
3 2
4 4
5 5
2 4
4 3

stdout

0 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 4 5 8 8 8 8 

Explanation

If you want to raise the lava to level 22, you need to wait 11 second for the person in cell (33, 22).
If you want to raise the lava to level 66, you need to wait 22 seconds for the persons in cell (44, 33).

Log in or sign up to be able to send submissions!