#include <bits/stdc++.h>
using namespace std;

template<typename T,bool directed>
struct Dinic{
    struct edge{
        int to,rev; T cap;
        edge(int to,T cap,int rev):to(to),cap(cap),rev(rev){}
    };
    vector<vector<edge>> G;
    vector<int> level,iter;
    int N;
    Dinic(int n,int N):G(n),level(n),iter(n),N(N){}
    int add_edge(int from,int to,T cap){
        G[from].emplace_back(to,cap,G[to].size());
        G[to].emplace_back(from,directed?0:cap,G[from].size()-1);
        return G[to].back().rev;
    }
    void bfs(int s){
        fill(level.begin(),level.end(),-1);
        queue<int> que;
        level[s]=0;
        que.emplace(s);
        while(!que.empty()){
            int v=que.front(); que.pop();
            for (auto &e:G[v]){
                if (e.cap>0&&level[e.to]<0){
                    level[e.to]=level[v]+1;
                    que.emplace(e.to);
                }
            }
        }
    }
    T dfs(int v,int t,T f){
        if (v==t) return f;
        for (int &i=iter[v];i<G[v].size();++i){
            edge &e=G[v][i];
            if (e.cap>0&&level[v]<level[e.to]){
                T d=dfs(e.to,t,min(f,e.cap));
                if (d==0) continue;
                e.cap-=d;
                G[e.to][e.rev].cap+=d;
                return d;
            }
        }
        return 0;
    }
    T max_flow(int s,int t,T lim){
        T flow=0;
        while(1){
            bfs(s);
            if (level[t]<0||lim==0) break;
            fill(iter.begin(),iter.end(),0);
            while(1){
                T f=dfs(s,t,lim);
                if (f==0) break;
                flow+=f;
                lim-=f;
            }
        }
        return flow;
    }
    T max_flow(int s,int t){
        return max_flow(s,t,numeric_limits<T>::max()/2);
    }
    vector<int> used_es(int s,int t){
        max_flow(s,t);
        vector<int> res(N);
        for (int v=1;v<=N;++v){
            for (auto e:G[v]){
                if (!e.to) continue;
                if (!e.cap) res[e.to-N-1]=v;
            }
        }
        return res;
    }
};

const int MAX_N=110;

int N,M;
int A[MAX_N][MAX_N],B[MAX_N][MAX_N];

void solve1(){
    vector<vector<bool>> used(N,vector<bool>(M,false));
    for (int j=0;j<M;++j){
        Dinic<int,true> D((N+1)*2,N);
        for (int i=1;i<=N;++i) D.add_edge(0,i,1);
        for (int i=N+1;i<=2*N;++i) D.add_edge(i,2*N+1,1);
        for (int i=0;i<N;++i){
            for (int jj=0;jj<M;++jj){
                if (!used[i][jj]){
                    D.add_edge((A[i][jj]-1)/M+1,N+1+i,1);
                }
            }
        }
        vector<int> arrange=D.used_es(0,2*N+1);
        for (int i=0;i<N;++i){
            for (int jj=0;jj<M;++jj){
                if (used[i][jj]) continue;
                if ((A[i][jj]-1)/M+1==arrange[i]){
                    used[i][jj]=true;
                    B[i][j]=A[i][jj];
                    break;
                }
            }
        }
    }
    for (int i=0;i<N;++i)
        for (int j=0;j<M;++j)
            cout << B[i][j] << (j==M-1?'\n':' ');
}

void solve2(){
    for (int j=0;j<M;++j){
        vector<int> col(N);
        for (int i=0;i<N;++i) col[i]=B[i][j];
        sort(col.begin(),col.end());
        for (int i=0;i<N;++i) A[i][j]=col[i];
    }
    for (int i=0;i<N;++i)
        for (int j=0;j<M;++j)
            cout << A[i][j] << (j==M-1?'\n':' ');
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    cin >> N >> M;
    for (int i=0;i<N;++i)
        for (int j=0;j<M;++j)
            cin >> A[i][j];
    solve1(); solve2();
}

Measured memory_bytes: 764