#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define ULL unsigned long long
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define pll pair<LL,LL>
#define x first
#define y second
#define pi acos(-1)
#define sqr(x) ((x)*(x))
#define pdd pair<double,double>
#define MEMS(x) memset(x,-1,sizeof(x))
#define MEM(x) memset(x,0,sizeof(x))
#define less Less
#define EPS 1e-4
#define arg ARG
#define pdd pair<double,double>
#define cpdd const pdd
struct Dinic{
    static const int MXN = 10000;
    struct Edge{ int v,f,re; Edge(int a,int b,int c):v(a),f(b),re(c){}};
    int n,s,t,level[MXN];
    vector<Edge> E[MXN];
    void init(int _n, int _s, int _t){
        n = _n; s = _s; t = _t;
        for (int i=0; i<=n; i++) E[i].clear();
    }
    void add_edge(int u, int v, int f){
        E[u].pb(Edge(v,f,E[v].size()));
        E[v].pb(Edge(u,0,E[u].size()-1));//direct
    }
    bool BFS(){
        MEMS(level);
        queue<int> que;
        que.push(s);
        level[s] = 0;
        while (!que.empty()){
            int u = que.front(); que.pop();
            for (auto it : E[u]){
                if (it.f > 0 && level[it.v] == -1){
                    level[it.v] = level[u]+1;
                    que.push(it.v);
                }
            }
        }
        return level[t] != -1;
    }
    int DFS(int u, int nf){
        if (u == t) return nf;
        int res = 0;
        for (auto &it : E[u]){
            if (it.f > 0 && level[it.v] == level[u]+1){
                int tf = DFS(it.v, min(nf,it.f));
                res += tf; nf -= tf; it.f -= tf;
                E[it.v][it.re].f += tf;
                if (nf == 0) return res;
            }
        }
        if (!res) level[u] = -1;
        return res;
    }
    int flow(int res=0){
        while ( BFS() )
        res += DFS(s,2147483647);
        return res;
    }
}flow;
vector<int> r[105][105];
int main(){
    int n,m;
    scanf("%d %d",&n,&m);
    int a[105][105];
    for(int i = 1;i<=n;i++){
        for(int j = 1;j<=m;j++){
            scanf("%d",&a[i][j]);
            r[i][(a[i][j]-1)/m+1].pb(a[i][j]);
        }
    }
    int b[105][105];
    for(int j=1;j<=m;j++){
        flow.init(2*n+10,0,2*n+1);
        for(int i=1;i<=n;i++){
            flow.add_edge(0,i,1);
            flow.add_edge(i+n,2*n+1,1);
        }
        for(int i = 1;i<=n;i++){
            for(int j = 1;j<=n;j++){
                if(!r[i][j].empty()){
                    flow.add_edge(i,j+n,1);
                }
            }
        }
        int x=flow.flow();
      //  printf("%d\n",x);
        for(int i = 1;i<=n;i++){
            for(auto it:flow.E[i]){
                if(it.f==0&&it.v>n){
                    b[i][j]=r[i][it.v-n].back();
                    r[i][it.v-n].pop_back();
                    break;
                }
            }
        }
    }
    for(int i = 1;i<=n;i++){
        for(int j = 1;j<=m;j++)
            printf("%d ",b[i][j]);
        printf("\n");
    }
    for(int i = 1;i<=n;i++){
        for(int j = 1;j<=m;j++){
            while((b[i][j]-1)/m+1!=i){
                swap(b[i][j],b[(b[i][j]-1)/m+1][j]);
            }
        }
    }
    for(int i = 1;i<=n;i++){
        for(int j = 1;j<=m;j++)
            printf("%d ",b[i][j]);
        printf("\n");
    }
}
/*
100
2 3
0 500000000 1000000000
1000000000 1000000000 1000000000
 */

Measured memory_bytes: 1536