#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<vector>
#include<map>
#include<set>
#include<string>
#include<queue>
#include<stack>
using namespace std;
#define MOD 1000000007
#define INF (1<<29)
#define LINF (1LL<<60)
#define EPS (1e-10)
typedef long long Int;
typedef pair<Int, Int> P;

class Edge{
public:
    Int from, to, rev;
    Int cost, lim;
    Edge(Int from, Int to, Int cost, Int lim = -1, Int rev = -1):
        from(from),to(to),cost(cost),lim(lim),rev(rev){}
};


class Graph{
public:
    Int n;
    Int m;
    vector<vector<Edge>> edge;
    Graph(Int n = 0):n(n),m(0){
        edge = vector<vector<Edge>>(n);
    }
    void add_edge(Int from, Int to, Int cost = 1, Int lim = -1)
        {
            if(lim == -1){
                edge[from].push_back(Edge(from, to, cost));
                return;
            }
            edge[from].push_back(Edge(from, to, cost, lim, edge[to].size()));
            edge[to].push_back(Edge(to, from, -cost, 0, edge[from].size() - 1));
        }
};



class Dinic{
public:
    Graph *g;
    vector<int> iter, level, come;

    Dinic(){};
    void bfs(int s){
        queue<int> q;
        fill(level.begin(), level.end(), INF);
        level[s] = 0;
        q.push(s);
        while(!q.empty()){
            int x = q.front();q.pop();
            for(auto e:g->edge[x]){
                if(level[e.to] != INF)continue;
                if(e.lim <= 0)continue;
                level[e.to] = level[x] + 1;
                q.push(e.to);
            }
        }
    }

    Int dfs(int s, int t, Int f){
        if(s == t)return f;
        if(come[s])return 0;
        come[s] = true;
        for(int &i = iter[s];i < g->edge[s].size();i++){
            Edge &e = g->edge[s][i];
            if(e.lim <= 0)continue;
            Int tmp = dfs(e.to, t, min(f, e.lim));
            if(tmp == 0)continue;
            e.lim -= tmp;
            g->edge[e.to][e.rev].lim += tmp;
            return tmp;
        }
        return 0;
    }
    Int max_flow(Graph &_g, int s, int t){
        g = &_g;
        iter = vector<int>(g->n);
        level = vector<int>(g->n);
        come = vector<int>(g->n, 0);
        Int res = 0;
        while(true){
            bfs(s);
            if(level[t] == INF)break;
            fill(iter.begin(), iter.end(), 0);
            while(true){
                fill(come.begin(), come.end(), false);
                Int tmp = dfs(s, t, INF);
                if(tmp == 0)break;
                else res += tmp;
            }
        }
        return res;   
    }  
};

Int a[108][108];
Int new_a[108][108];
int main(){
    Int n, m, s, t;
    Dinic d;
    cin >> n >> m;
    Graph g(n*m+n+n+2);
    s = n * m  + n+n;
    t = s + 1;
    for(int i = 0;i < n;i++){
        for(int j = 0;j < m;j++){
            cin >> a[i][j];
            g.add_edge(n*m+((a[i][j]-1)/m), a[i][j]-1, 1, 1);
            g.add_edge(a[i][j]-1, n*m+n+i,1, 1);
        }
        g.add_edge(s, n*m+i, 1, 1);
        g.add_edge(n*m+n+i, t, 1, 1);
    }
    for(int i = 0;i < m;i++){
        Dinic d;
        vector<int> tmp;
        d.max_flow(g, s, t);
        for(int j = 0;j < n;j++){
            for(auto e:g.edge[n*m+n+j]){
                if(e.lim == 0)continue;
                if(e.to < n*m){
                    new_a[j][i] = e.to+1;
                    tmp.push_back(e.to);
                    break;
                }
            }
        }
        d.max_flow(g, t, s);
        for(auto v:tmp){
            for(auto &e:g.edge[v])e.lim = 0;
        }
    }

    for(int i = 0;i < n;i++){
        for(int j = 0;j < m;j++){
            cout << new_a[i][j] << " ";
        }cout << endl;
    }

    for(int i = 0;i < m;i++){
        for(int j = 0;j < n;j++){
            for(int k = 0;k < n-1;k++){
                if(new_a[k][i] > new_a[k+1][i]){
                    swap(new_a[k][i], new_a[k+1][i]);
                }
            }
        }
    }

    for(int i = 0;i < n;i++){
        for(int j = 0;j < m;j++){
            cout << new_a[i][j] << " ";
        }cout << endl;
    }
       
    
    return 0;
}

Measured memory_bytes: 2944