atcoder#AGC037D. [AGC037D] Sorting a Grid

[AGC037D] Sorting a Grid

Score : 11001100 points

Problem Statement

We have a grid with NN rows and MM columns of squares. Each integer from 11 to NMNM is written in this grid once. The number written in the square at the ii-th row from the top and the jj-th column from the left is AijA_{ij}.

You need to rearrange these numbers as follows:

  1. First, for each of the NN rows, rearrange the numbers written in it as you like.
  2. Second, for each of the MM columns, rearrange the numbers written in it as you like.
  3. Finally, for each of the NN rows, rearrange the numbers written in it as you like.

After rearranging the numbers, you want the number written in the square at the ii-th row from the top and the jj-th column from the left to be M×(i1)+jM\times (i-1)+j. Construct one such way to rearrange the numbers. The constraints guarantee that it is always possible to achieve the objective.

Constraints

  • 1N,M1001 \leq N,M \leq 100
  • 1AijNM1 \leq A_{ij} \leq NM
  • AijA_{ij} are distinct.

Input

Input is given from Standard Input in the following format:

NN MM

A11A_{11} A12A_{12} ...... A1MA_{1M}

::

AN1A_{N1} AN2A_{N2} ...... ANMA_{NM}

Output

Print one way to rearrange the numbers in the following format:

B11B_{11} B12B_{12} ...... B1MB_{1M}

::

BN1B_{N1} BN2B_{N2} ...... BNMB_{NM}

C11C_{11} C12C_{12} ...... C1MC_{1M}

::

CN1C_{N1} CN2C_{N2} ...... CNMC_{NM}

Here BijB_{ij} is the number written in the square at the ii-th row from the top and the jj-th column from the left after Step 11, and CijC_{ij} is the number written in that square after Step 22.

3 2
2 6
4 3
1 5
2 6 
4 3 
5 1 
2 1 
4 3 
5 6
3 4
1 4 7 10
2 5 8 11
3 6 9 12
1 4 7 10 
5 8 11 2 
9 12 3 6 
1 4 3 2 
5 8 7 6 
9 12 11 10