Under construction

Still being built. Things may change

Image Filter

Interview

Time limit1sMemory limit1024 MB

Summary
For each cell of a heightmap, pick the prediction filter and residual with the smallest absolute error, breaking ties by the lexicographically smallest pair.
Level

Easy3 of 10

Topics
Implementation, Greedy
Solved
No attempts yet

Problem

Problem setters exchange data and verify each other's work when they prepare a contest. Setter B built all the data for a problem that uses heightmaps and was satisfied with it.

Only the verification step was left, and then B found a problem. The heightmaps were saved as 16-bit grayscale bitmaps without much thought, and the files were too large for setters to send to each other in real time.

To solve this, a bitmap compression method was designed that uses filters. Compression processes each cell in order. Let Hp,qH_{p,q} be the height at row pp and column qq of the heightmap. When the cell being compressed is at row rr and column cc, define

  • A=Hr,c−1A = H_{r,c-1}, or 00 if that cell does not exist
  • B=Hr−1,cB = H_{r-1,c}, or 00 if that cell does not exist
  • C=Hr−1,c−1C = H_{r-1,c-1}, or 00 if that cell does not exist

Each height in the compressed image is expressed with one of the five filters in the table below. A compressed cell is written as the filter number and the difference between the height and the prediction made by that filter.

NumberFilter prediction
0000
11AA
22BB
33(A+B)(A + B) divided by 22, rounded down
44A+B−CA + B - C

For example, suppose the following 22 by 22 heightmap is given.

6 4
2 1

Consider how to express the 1 at the bottom right.

  • With filter 00, the difference from the prediction is 1−0=11 - 0 = 1
  • With filter 11, the difference is 1−2=−11 - 2 = -1
  • With filter 22, the difference is 1−4=−31 - 4 = -3
  • With filter 33, the difference is 1−(2+4)/2=−21 - (2 + 4)/2 = -2
  • With filter 44, the difference is 1−(2+4−6)=11 - (2 + 4 - 6) = 1

The total of the absolute differences over the whole compressed image must be minimized over all possible ways of choosing filters. If several choices reach the minimum, pick the one whose sequence of (filter number, difference) pairs is lexicographically smallest when the cells are listed from the top left in column order. Write a program that performs this compression.

Input

The input consists of TT test cases. The first line contains TT.

The first line of each test case contains the size of the heightmap, RR and CC (1≤R≤1001 \le R \le 100, 1≤C≤1001 \le C \le 100), separated by a space. The heightmap has RR rows and CC columns. The next RR lines each contain CC integers Hi,jH_{i,j} (0≤Hi,j<2160 \le H_{i,j} < 2^{16}), the pixel values of the heightmap, separated by spaces.

Output

For each test case, print RR lines. Each line contains CC pairs, separated by spaces, where each pair is the filter number used and the integer difference from that filter's prediction.

Examples1

  1. Example 1

    Input
    3
    2 2
    6 4
    2 1
    3 3
    0 0 0
    0 100 0
    0 0 0
    4 5
    0 0 0 0 0
    0 100 50 50 0
    0 50 100 50 0
    0 0 0 0 0
    
    Expected output
    0 6 3 1
    3 -1 0 1
    0 0 0 0 0 0
    0 0 0 100 0 0
    0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0
    0 0 0 100 3 0 1 0 0 0
    0 0 3 0 1 50 2 0 0 0
    0 0 0 0 0 0 0 0 0 0