//2019.8.21 by ljz
//email 573902690@qq.com
//if you find any bug in my code
//please tell me
#include<bits/stdc++.h>
using namespace std;
#define res register int
#define LL long long
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f3f
#define unl __int128
#define eps 5.6e-8
#define RG register
#define db double
#define pc(x) __builtin_popcount(x)
typedef pair<int,int> Pair;
#define mp make_pair
#define fi first
#define se second
#define pi acos(-1.0)
#define pb push_back
#define ull unsigned LL
#define gc getchar
//inline char gc() {
//    static char buf[100000],*p1,*p2;
//    return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
//}
inline int read() {
    res s=0,ch=gc();
    while(ch<'0'||ch>'9')ch=gc();
    while(ch>='0'&&ch<='9')s=s*10+ch-'0',ch=gc();
    return s;
}
//inline int read() {
//    res s=0,ch=gc(),w=1;
//    while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=gc();}
//    while(ch>='0'&&ch<='9')s=s*10+ch-'0',ch=gc();
//    return s*w;
//}
//inline LL Read() {
//    RG LL s=0;
//    res ch=gc();
//    while(ch<'0'||ch>'9')ch=gc();
//    while(ch>='0'&&ch<='9')s=s*10+ch-'0',ch=gc();
//    return s;
//}
//inline LL Read() {
//    RG LL s=0;
//    res ch=gc(),w=1;
//    while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=gc();}
//    while(ch>='0'&&ch<='9')s=s*10+ch-'0',ch=gc();
//    return s*w;
//}
//inline void write(RG unl x){
//    if(x>10)write(x/10);
//    putchar(int(x%10)+'0');
//}
inline void swap(res &x,res &y) {
    x^=y^=x^=y;
}
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//clock_t start=clock();
//inline void ck(){
//    if(1.0*(clock()-start)/CLOCKS_PER_SEC>0.1)exit(0);
//}
const int N=100+10;
namespace MAIN{
    int n,m;
    int a[N][N],b[N*N],id[N*N];
    int S,T;
    int dep[N*N];
    struct E{
        int next,to,val;
        E() {}
        E(res next,res to,res val):next(next),to(to),val(val) {}
    }edge[N*N*10];
    int head[N*N],cnt=1;
    inline void addedge(const res &u,const res &v,const res &w){
        edge[++cnt]=E(head[u],v,w),head[u]=cnt;
        edge[++cnt]=E(head[v],u,0),head[v]=cnt;
    }
    int Q[N*N*10],he,ta;
    inline bool bfs(){
        for(res i=S;i<=T;i++)dep[i]=-1;
        dep[Q[he=ta=1]=S]=0;
        while(he<=ta){
            res u=Q[he++];
            for(res i=head[u];~i;i=edge[i].next){
                res tox=edge[i].to;
                if(edge[i].val&&dep[tox]==-1)dep[tox]=dep[u]+1,Q[++ta]=tox;
            }
        }
        return dep[T]!=-1;
    }
    int cur[N*N];
    int dfs(res x,res flow){
        if(x==T||!flow)return flow;
        for(res &i=cur[x];~i;i=edge[i].next){
            res tox=edge[i].to;
            if(dep[tox]==dep[x]+1){
                res f=dfs(tox,min(flow,edge[i].val));
                if(f){
                    edge[i].val-=f,edge[i^1].val+=f;
                    return f;
                }
            }
        }
        return 0;
    }
    inline int dinic(){
        res ans=0;
        while(bfs()){
            for(res i=S;i<=T;i++)cur[i]=head[i];
            while(int f=dfs(S,inf))ans+=f;
        }
        return ans;
    }
    vector<int> pos[N][N];
    bool lj[N][N];
    int ys[N*N];
    int f[N][N],g[N][N];
    inline void MAIN(){
        n=read(),m=read();
        for(res i=1;i<=n;i++)
            for(res j=1;j<=m;j++)
                id[a[i][j]=read()]=i;
        for(res i=1;i<=n;i++)
            for(res j=1;j<=m;j++)
                b[(i-1)*m+j]=i;
        S=0,T=n<<1|1;
        for(res i=1;i<=n*m;i++)pos[id[i]][b[i]].pb(i);
        for(res k=1;k<=m;k++){
            for(res i=1;i<=n;i++)
                for(res j=1;j<=n;j++)
                    lj[i][j]=bool(!pos[i][j].empty());
            for(res i=S;i<=T;i++)head[i]=-1;
            cnt=1;
            for(res i=1;i<=n;i++)addedge(S,i,1),addedge(i+n,T,1);
            for(res i=1;i<=n;i++)
                for(res j=1;j<=n;j++)
                    if(lj[i][j])addedge(i,j+n,1);
            res ret=dinic();
            for(res x=1;x<=n;x++)
                for(res i=head[x];~i;i=edge[i].next){
                    res tox=edge[i].to;
                    if(tox!=S&&!edge[i].val)ys[x]=tox-n;
                }
            for(res i=1;i<=n;i++)f[i][k]=g[ys[i]][k]=pos[i][ys[i]].back(),pos[i][ys[i]].pop_back();
        }
        for(res i=1;i<=n;i++){
            for(res j=1;j<=m;j++)
                printf("%d ",f[i][j]);
            puts("");
        }
        for(res i=1;i<=n;i++){
            for(res j=1;j<=m;j++)
                printf("%d ",g[i][j]);
            puts("");
        }
    }
}
int main(){
//    srand(19260817);
//    freopen("signin.in","r",stdin);
//    freopen("signin.out","w",stdout);
    MAIN::MAIN();
    return 0;
}

Measured memory_bytes: 1408