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(" "));
    }
}


struct XorShift {
    x: usize,
    y: usize,
    z: usize,
    w: usize
}

impl XorShift {
    fn new() -> XorShift {
        XorShift {
            x: 123456789,
            y: 362436069,
            z: 521288629,
            w: 88675123
        }
    }

    fn rotate(&mut self) {
        let t = self.x ^ (self.x << 11);
        self.x = self.y;
        self.y = self.z;
        self.z = self.w;
        self.w = (self.w ^ (self.w >> 19)) ^ (t ^ (t >> 8));
    }

    fn next_int(&mut self, n: usize) -> usize {
        self.rotate();
        let r = (self.w as i32) % (n as i32);
        if r < 0 {
            (r + n as i32) as usize
        } else {
            r as usize
        }
    }
}


fn gen(n: usize, m: usize) -> Vec<Vec<usize>> {
    let mut x = XorShift::new();
    let mut aw = vec![0; n*m];
    for i in 0..n {
        for j in 0..m {
            aw[i*m+j] = i*m+j;
        }
    }
    for i in 1..n*m {
        let w = x.next_int(i);
        let t = aw[i];
        aw[i] = aw[w];
        aw[w] = t;
    }

    let mut a = vec![vec![0; m]; n];
    for i in 0..n {
        for j in 0..m {
            a[i][j] = aw[i*m+j];
        }
    }
    a

}

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

    let mut a = a;
    // let mut a = gen(n, m);
    for i in 0..n  {
        a[i].sort();
    }
    // print_table(&a);

    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 sppz = vec![0; m];
            for ti in 0..n {
                if done[ti] {
                    continue;
                }
                for j in 0..m {
                    if b[ti][j] == INF && vacant[j] {
                        sppz[j] += 1;
                    }
                }
            }

            let mut bestn = n;
            let mut space = n+1;
            for ti in 0..n {
                if done[ti] {
                    continue;
                }
                let mut ts = n;
                for j in 0..m {
                    if b[ti][j] == INF {
                        ts = min(ts, sppz[j]);
                    }
                }
                if space > ts {
                    space = ts;
                    bestn = ti;
                }
            }
            if bestn == n {
                break;
            }
            for &ni in &ns[bestn] {
                let mut bestj = m;
                let mut bestv = n+10;
                for j in 0..m {
                    if vacant[j] && b[bestn][j] == INF {
                        if bestv > sppz[j] {
                            bestj = j;
                            bestv = sppz[j];
                        }
                    }
                }
                if bestj == m {
                    continue;
                }
                b[bestn][bestj] = ni;
                vacant[bestj] = false;
            }
            done[bestn] = true;
        }
    }

    // print_table(&b);


    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);
}