//Relive your past life.
//Face your demons.
//The past is never dead,it is not even past.
//The memories are not only the key to the past but...also to the future.
//coded in Rusty Lake
#include<cmath>
#include<math.h>
#include<ctype.h>
#include<algorithm>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cerrno>
#include<cfloat>
#include<ciso646>
#include<climits>
#include<clocale>
#include<complex>
#include<csetjmp>
#include<csignal>
#include<cstdarg>
#include<cstddef>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<cwchar>
#include<cwctype>
#include<deque>
#include<exception>
#include<fstream>
#include<functional>
#include<iomanip>
#include<ios>
#include<iosfwd>
#include<iostream>
#include<istream>
#include<iterator>
#include<limits>
#include<list>
#include<locale>
#include<map>
#include<memory>
#include<new>
#include<numeric>
#include<ostream>
#include<queue>
#include<set>
#include<sstream>
#include<stack>
#include<stdexcept>
#include<streambuf>
#include<string>
#include<typeinfo>
#include<utility>
#include<valarray>
#include<vector>
#include<string.h>
#include<stdlib.h>
#include<stdio.h>
#define ll   long long
#define pb   push_back
#define mp   make_pair
#define orz  1000000007
using namespace std;
struct E{int x,y,z;};
struct FLOW{
    static const int N=205,INF=1000000007;
    vector<E> v[N];
    int n,s,t,ans,i[N],l[N],q[N],qs,qe;
    void bfs(){
        memset(l,-1,sizeof(l));
        l[s]=0,q[1]=s,qs=0,qe=1;
        while(qs!=qe){
            int k=q[++qs];
            for(int i=0;i<v[k].size();++i)if(v[k][i].y&&l[v[k][i].x]==-1)l[v[k][i].x]=l[k]+1,q[++qe]=v[k][i].x;        
        }
    }
    int dfs(int x,int y){
        if(x==t){
            ans+=y;
            return y;
        }
        for(;i[x]<v[x].size();++i[x]){
            E &o=v[x][i[x]];
            if(o.y&&l[x]<l[o.x]){
                int d=dfs(o.x,min(o.y,y));
                if(d){
                    o.y-=d,v[o.x][o.z].y+=d;
                    return d;
                }
            }
        }
        return 0;
    }
    int flow(){
        ans=0;
        while(1){
            bfs();
            if(l[t]==-1) return ans;
            memset(i,0,sizeof(i));
            while(dfs(s,INF));
        }
    }
    void add(int x,int y,int z){
        E X,Y;
        X.x=y,X.y=z,X.z=v[y].size();
        Y.x=x,Y.y=0,Y.z=v[x].size();
        v[x].pb(X),v[y].pb(Y);
    }
}_[105];
int n,m,a[105][105],b[105][105];
int main(){
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;++i){
        for(int j=1;j<=m;++j)scanf("%d",a[i]+j);
    }
    for(int k=1;k<=m;++k){
        _[k].n=n*2+2;
        _[k].s=_[k].n-1;
        _[k].t=_[k].n;
        for(int i=1;i<=n;++i){
            _[k].add(_[k].s,i,1);
            _[k].add(i+n,_[k].t,1);
            for(int j=k;j<=m;++j){
                _[k].add(i,(a[i][j]-1)/m+n+1,1);
            }
        }
        _[k].flow();
        for(int i=1;i<=n;++i){
            int o=0;
            for(int j=0;j<_[k].v[i].size();++j){
                if(_[k].v[i][j].y==0){
                    o=_[k].v[i][j].x-n;
                    break;
                }
            }
            for(int j=k;j<=m;++j){
                if((a[i][j]-1)/m+1==o){
                    if(j>k)swap(a[i][j],a[i][k]);
                    break;
                }
            }
        }
    }
    for(int i=1;i<=n;++i){
        for(int j=1;j<m;++j)printf("%d ",a[i][j]);
        printf("%d\n",a[i][m]);
    }
    for(int j=1;j<=m;++j){
        for(int i=1;i<=n;++i)b[(a[i][j]-1)/m+1][j]=a[i][j];
    }
    for(int i=1;i<=n;++i){
        for(int j=1;j<m;++j)printf("%d ",b[i][j]);
        printf("%d\n",b[i][m]);
    }
    //system("pause");
    return 0;
}


Measured memory_bytes: 19328