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


Measured memory_bytes: 6396