Problem:
Score : 1100 points

Problem Statement

We have a grid with N rows and M columns of squares.
Each integer from 1 to NM is written in this grid once.
The number written in the square at the i-th row from the top and the j-th column from the left is A_{ij}.

You need to rearrange these numbers as follows:

First, for each of the N rows, rearrange the numbers written in it as you like.

Second, for each of the M columns, rearrange the numbers written in it as you like.

Finally, for each of the N rows, rearrange the numbers written in it as you like.

After rearranging the numbers, you want the number written in the square at the i-th row from the top and the j-th column from the left to be M\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

1 \leq N,M \leq 100

1 \leq A_{ij} \leq NM

A_{ij} are distinct.

Input

Input is given from Standard Input in the following format:

N M
A_{11} A_{12} ... A_{1M}
:
A_{N1} A_{N2} ... A_{NM}

Output

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

B_{11} B_{12} ... B_{1M}
:
B_{N1} B_{N2} ... B_{NM}
C_{11} C_{12} ... C_{1M}
:
C_{N1} C_{N2} ... C_{NM}

Here B_{ij} is the number written in the square at the i-th row from the top and the j-th column from the left after Step 1, and C_{ij} is the number written in that square after Step 2.

Sample Input 1

3 2
2 6
4 3
1 5

Sample Output 1

2 6
4 3
5 1
2 1
4 3
5 6

Sample Input 2

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

Sample Output 2

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

Solution in Rust (1.15.1) (judge verdict: Runtime Error)

// https://atcoder.jp/contests/agc037/tasks/agc037_d
//
#![allow(unused_imports)]
use std::io::*;
use std::fmt::*;
use std::str::*;
use std::cmp::*;
use std::collections::*;

macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
    ($($r:tt)*) => {
        let s = {
            use std::io::Read;
            let mut s = String::new();
            std::io::stdin().read_to_string(&mut s).unwrap();
            s
        };
        let mut iter = s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
}

macro_rules! input_inner {
    ($iter:expr) => {};
    ($iter:expr, ) => {};

    ($iter:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($iter, $t);
        input_inner!{$iter $($r)*}
    };
}

macro_rules! read_value {
    ($iter:expr, ( $($t:tt),* )) => {
        ( $(read_value!($iter, $t)),* )
    };

    ($iter:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
    };

    ($iter:expr, chars) => {
        read_value!($iter, String).chars().collect::<Vec<char>>()
    };

    ($iter:expr, usize1) => {
        read_value!($iter, usize) - 1
    };

    ($iter:expr, $t:ty) => {
        $iter.next().unwrap().parse::<$t>().expect("Parse error")
    };
}

#[allow(unused_macros)]
macro_rules! dvec {
    ($t:expr ; $len:expr) => {
        vec![$t; $len]
    };

    ($t:expr ; $len:expr, $($rest:expr),*) => {
        vec![dvec!($t; $($rest),*); $len]
    };
}

#[allow(unused_macros)]
macro_rules! debug {
    ($($a:expr),*) => {
        println!(concat!($(stringify!($a), " = {:?}, "),*), $($a),*);
    }
}


const INF: usize = 1e9 as usize;

fn print_table(a: &Vec<Vec<usize>>) {
    for al in a {
        println!("{}", al.iter().map(|x| (x+1).to_string()).collect::<Vec<_>>().join(" "));
    }
}

fn main() {
    input! {
        n: usize, m: usize,
        a: [[usize1; m]; n]
    };

    let mut pmap = dvec!(0; n*m, 2);
    for i in 0..n {
        for j in 0..m {
            pmap[a[i][j]][0] = i;
            pmap[a[i][j]][1] = j;
        }
    }

    let mut b = dvec!(INF; n, m);
    for i in 0..n {
        let mut vacant = vec![true; m];
        let mut ns = vec![vec![]; n];
        for j in 0..m {
            ns[pmap[i*m+j][0]].push(i*m+j);
        }
        let mut done = vec![false; n];
        while true {
            let mut bestn = n;
            let mut space = m+10;
            for ti in 0..n {
                if done[ti] {
                    continue;
                }
                let mut ts = 0;
                for j in 0..m {
                    if b[ti][j] == INF && vacant[j] {
                        ts += 1;
                    }
                }
                if space > ts {
                    space = ts;
                    bestn = ti;
                }
            }
            if bestn == n {
                break;
            }
            for &ni in &ns[bestn] {
                for j in 0..m {
                    if vacant[j] && b[bestn][j] == INF {
                        b[bestn][j] = ni;
                        vacant[j] = false;
                        break;
                    }
                }
            }
            done[bestn] = true;
        }
    }


    let mut c = dvec!(INF; n, m);
    for j in 0..m {
        for i in 0..n {
            c[b[i][j]/m][j] = b[i][j];
        }
    }
    print_table(&b);
    print_table(&c);

    println!("ok");
}