#include <bits/stdc++.h>
using namespace std;using ll=long long;using uint=unsigned int;using pii=pair<int,int>;using pll=pair<ll,ll>;using ull = unsigned long long;using ld=long double;template<typename T>void _(const char*s,T h){cerr<<s<<" = "<<h<<"\n";}template<typename T,typename...Ts>void _(const char*s,T h,Ts...t){int b=0;while(((b+=*s=='(')-=*s==')')!=0||*s!=',')cerr<<*s++;cerr<<" = "<<h<<",";_(s+1,t...);}// break continue pop_back 998244353
#define int ll
#define pii pll
#define f first
#define s second
#define pb emplace_back
#define forn(i,n) for(int i=0;i<(n);++i)
#define sz(a)((int)(a).size())
#define sqr(x) ((x)*(x))
struct init{init(){cin.tie(0);iostream::sync_with_stdio(0);cout<<fixed<<setprecision(10);cerr<<fixed<<setprecision(5);}~init(){
#ifdef LOCAL
#define dbg(...) _(#__VA_ARGS__,__VA_ARGS__)
cerr<<"Time elapsed: "<<(double)clock()/CLOCKS_PER_SEC<<"s.\n";
#else
#define dbg(...) 
#endif
}}init;template<typename T,typename U>void upx(T&x,U y){if(x<y)x=y;}template<typename T,typename U>void upn(T&x,U y){if(x>y)x=y;}mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());const int D=4,dx[]={+1,0,-1,0},dy[]={0,+1,0,-1};
const int N=101;
int a[N][N];
int b[N][N];
int u[N][N];
int c[N][N];
namespace dinic{
	const int OO=1e9;
	struct edge{
	    int from,to,flow,capacity;
	    edge(int f,int t,int c):from(f),to(t),flow(0),capacity(c){}
	};
	vector<vector<int>> g;
	vector<edge> es;
	int n,source,sink;
	vector<int> q,dist,y;
	void init(int _n,int s,int t){
		n=_n;
		source=s;
		sink=t;
		g.assign(n,vector<int>());
		es.clear();
		q.resize(n);
		dist.resize(n);
		y.resize(n);
	} 
	void ae(int a,int b,int c){
	    es.pb(a,b,c);g[a].pb(es.size()-1);
	    es.pb(b,a,0);g[b].pb(es.size()-1);
	}
	bool bfs(){
		fill(dist.begin(), dist.end(), n);
	    dist[source]=0;
	    int l=0,r=0;
	    q[r++]=source;
	    while(l<r){
	        int v=q[l++];
	        for(int x:g[v]){
	            if(dist[v]+1<dist[es[x].to]&&es[x].flow+1<=es[x].capacity){
	                dist[es[x].to]=dist[v]+1;
	                q[r++]=es[x].to;
	            }
	        }
	    }
	    return dist[sink]<n;
	}
	int dfs(int v,int pu=OO){
	    if(v==sink)return pu;
	    for(;y[v]<sz(g[v]);++y[v]){
	        int x=g[v][y[v]];
	        int val=min(pu,es[x].capacity-es[x].flow);
	        if(dist[es[x].to]==dist[v]+1&&es[x].flow+1<=es[x].capacity&&(val=dfs(es[x].to,val))){
	            es[x].flow+=val;
	            es[x^1].flow-=val;
	            return val;
	        }
	    }
	    return 0;
	}
	int dinic(){
	    int ans=0,res;
	    while(bfs()){
	    	fill(y.begin(), y.end(), 0);
	        while(res=dfs(source))ans+=res;
	    }
	    return ans;
	}
}

int32_t main(){
	int n,m;
	cin>>n>>m;
	forn(i,n){
		forn(j,m){
			cin>>a[i][j];
			a[i][j]--;
		}
	}
	memset(b,-1,sizeof b);
	forn(j,m){
		dinic::init(n+n+2,n+n,n+n+1);
		forn(i,n){
			dinic::ae(dinic::source,i,1);
			dinic::ae(n+i,dinic::sink,1);
			forn(k,m){
				if(u[i][k])continue;
				int x=a[i][k]/m;
				dinic::ae(i,n+x,1);
			}
		}
		assert(dinic::dinic()==n);
		forn(i,n){
			for(int x:dinic::g[i]){
				auto e=dinic::es[x];
				if(e.flow>0&&e.to>=n){
					forn(k,m){
						if(!u[i][k]&&a[i][k]/m==e.to-n){
							u[i][k]=1;
							b[i][j]=a[i][k]+1;
							break;
						}
					}
				}
			}
		}
	}

	forn(i,n){
		forn(j,m)cout<<b[i][j]<<' ';
		cout<<'\n';
	}
	forn(j,m){
		vector<int> v(n);
		forn(i,n)v[i]=b[i][j];
		sort(v.begin(), v.end());
		forn(i,n)c[i][j]=v[i];
	}
	forn(i,n){
		forn(j,m)cout<<c[i][j]<<' ';
		cout<<'\n';
	}
	return 0;
}


Measured memory_bytes: 4692