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: Accepted)

// 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
}

// ===

struct Matching {
    n: usize,
    graph: Vec<Vec<Vec<usize>>>,
    deg: Vec<usize>,
    itr: Vec<usize>,
    lvl: Vec<i32>,
}

impl Matching {
    fn new(n: usize) -> Self {
        Matching {
            n: n,
            graph: vec![vec![]; n],
            deg: vec![0; n],
            itr: vec![0; n],
            lvl: vec![0; n]
        }
    }

    fn add_edge(&mut self, u: usize, v: usize, cap: usize, label: usize) {
        let udeg = self.deg[u];
        let vdeg = self.deg[v];
        self.graph[u].push(vec![v, cap, vdeg, label]);
        self.graph[v].push(vec![u, 0, udeg, label]);
        self.deg[u] += 1;
        self.deg[v] += 1;
    }

    fn dfs(&mut self, v: usize, t: usize, f: usize) -> usize {
        if v == t {
            return f;
        }
        let mut it = self.itr[v];
        while it < self.graph[v].len() {
            self.itr[v] = it;

            let to = self.graph[v][it][0];
            let left_cap = self.graph[v][it][1];
            let rev_idx = self.graph[v][it][2];
            if left_cap > 0 && self.lvl[v] < self.lvl[to] {
                let d = self.dfs(to, t, min(left_cap, f));
                if d > 0 {
                    self.graph[v][it][1] -= d;
                    self.graph[to][rev_idx][1] += d;
                    return d;
                }
            }
            it += 1;
        }
        0
    }

    fn bfs(&mut self, s: usize) {
        for i in 0..self.n {
            self.lvl[i] = -1;
        }
        self.lvl[s] = 0;

        let mut queue = VecDeque::new();
        queue.push_back(s);
        while queue.len() >= 1 {
            let v = queue.pop_front().unwrap();
            let lv = self.lvl[v];
            let en = self.graph[v].len();
            for i in 0..en {
                let to = self.graph[v][i][0];
                let cap = self.graph[v][i][1];
                if cap > 0 && self.lvl[to] < 0 {
                    self.lvl[to] = lv + 1;
                    queue.push_back(to);
                }
            }
        }
    }

    fn max_flow(&mut self, s: usize, t: usize) -> usize {
        let mut flow = 0;
        loop {
            self.bfs(s);
            if self.lvl[t] < 0 {
                return flow;
            }
            for i in 0..self.n {
                self.itr[i] = 0;
            }
            loop {
                let f = self.dfs(s, t, 100000);
                if f == 0 {
                    break;
                }
                flow += f;
            }
        }
    }
}

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

    let mut a = a;
    // let mut a = gen(n, m);
    // 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 wj in 0..m {
        let mut mat = Matching::new(n*2+2);
        let source = n+n;
        let sink = n+n+1;
        for j in 0..n {
            mat.add_edge(source, j, 1, INF);
            mat.add_edge(n+j, sink, 1, INF);
        }
        for z in 0..n*m {
            let fi = pmap[z][0];
            if fi == INF {
                continue;
            }
            mat.add_edge(z / m, n + fi, 1, z);
        }

        let flow = mat.max_flow(source, sink);
        for fi in 0..n {
            for e in &mat.graph[fi] {
                if e[1] == 0 && n <= e[0] && e[0] < 2*n {
                    // used
                    b[(e[0])-n][wj] = e[3];
                    pmap[e[3]][0] = INF;
                    break;
                }
            }
        }
    }

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