/*
AuThOr GaRyMr
*/
#include<bits/stdc++.h>
#define rb(a,b,c) for(int a=b;a<=c;++a)
#define rl(a,b,c) for(int a=b;a>=c;--a)
#define LL long long
#define IT iterator
#define PB push_back
#define II(a,b) make_pair(a,b)
#define FIR first
#define SEC second
#define FREO freopen("check.out","w",stdout)
#define rep(a,b) for(int a=0;a<b;++a)
#define KEEP while(1)
#define SRAND mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
#define random(a) rng()%a
#define ALL(a) a.begin(),a.end()
#define POB pop_back
#define ff fflush(stdout)
#define fastio ios::sync_with_stdio(false)
using namespace std;
const int INF=0x3f3f3f3f;
typedef pair<int,int> mp;
typedef pair<mp,mp> superpair;
int n,m,a[2001][30001],lie[30001];
int line[30001];
map<mp,int > represent;
map<int,int> Pick; 
const int GRAPH_SIZE=/*here->>>*/  100000 /*<<<=here*/;
int pin[GRAPH_SIZE],dep[GRAPH_SIZE],s,t;
struct EDGE{
	int u,v,c;
};
vector<EDGE> e;
vector<int> each[GRAPH_SIZE];
bool bfs(){
	queue<int> Q;
	Q.push(s);
	while(!Q.empty()){
		int now=Q.front();
		Q.pop();
		for(auto it:each[now]){
			int next=e[it].v;
			if(e[it].c)
			if(dep[next]>dep[now]+1){
				dep[next]=dep[now]+1;
				Q.push(next);
			}
		}
	}
	return dep[t]!=INF;
}
int dfs(int now,int flow){
	if(now==t){
		return flow;
	}
	for(int & i= pin[now];i<each[now].size();i++){
		int it=each[now][i];
		if(e[it].c&&dep[e[it].v]==dep[now]+1){
			int tmp;
			if(tmp=dfs(e[it].v,min(flow,e[it].c))){
				e[it].c-=tmp;
				e[it^1].c+=tmp;
				return tmp;
			}
		}
	}
	return 0;
}
int Dinic(){
	int max_flow=0;
	rep(i,GRAPH_SIZE)
	{
		
		dep[i]=INF;
	}
	dep[s]=0;
	while(bfs()){
		rep(i,GRAPH_SIZE){
			pin[i]=0;
		}
		int tmp;
		while(tmp=dfs(s,INF)){
			max_flow+=tmp;
		}
		rep(i,GRAPH_SIZE){
			dep[i]=INF;
		}
		dep[s]=0;
	}
	return max_flow;
}
void make_edge(int U,int V,int C){
	EDGE tmp;
	tmp.u=U;
	tmp.v=V;
	tmp.c=C;
	e.PB(tmp);
	each[U].PB(e.size()-1);
	swap(tmp.u,tmp.v);
	tmp.c=0;
	e.PB(tmp);
	each[V].PB(e.size()-1);
}
vector<int> SET[2]; 
int vis[301];
vector<int> EC[300];
void dfs_(int now,int col){
	SET[col].PB(now);
	vis[now]=1;
	for(auto it:EC[now]){
		if(!vis[it]) dfs_(it,col^1);
	}
}
void Gao(vector<vector< int> >  v){
	int N,M;
	SET[0].clear();
	SET[1].clear();
	N=v.size();
	M=v[0].size();
	if(!M) return;
	represent.clear();
	rep(i,N+10){
		vis[i]=0;
		each[i].clear();
		EC[i].clear();
	}
	vector<int> Connect[10001];
	rep(i,N)
		rep(j,M){
			Connect[v[i][j]].PB(i);
		}
	e.clear();
	s=N;
	t=N+1;
	
	rb(i,1,10000){
		if(Connect[i].size()==2){
			if(represent.find(II(Connect[i][1],Connect[i][0]))!=represent.end()) continue;
			EC[Connect[i][0]].PB(Connect[i][1]);
			EC[Connect[i][1]].PB(Connect[i][0]); 
			represent[II(Connect[i][0],Connect[i][1])]=i;
			represent[II(Connect[i][1],Connect[i][0])]=i;
		}
	}rep(i,N){
		if(!vis[i]) dfs_(i,0);
	}
	for(auto it:SET[0]){
		for(auto it2:EC[it]){
			make_edge(it,it2,1);
		}
		make_edge(s,it,1);
	} 
	for(auto it:SET[1]){
		make_edge(it,t,1); 
	}
	Dinic();
	Pick.clear();
	rep(i,e.size())
	{
		if(!(i&1)){
			if(e[i].c==0){
				if(e[i].u<N&&e[i].v<N){
					Pick[represent[II(e[i].u,e[i].v)]]=1;
				}
			}
		}
	}
	rep(i,N){
		rep(j,M){
			if(Pick[v[i][j]]){
				lie[v[i][j]]=1;
				vector<int> OK;
				rep(k,M){
					if(k!=j){
						OK.PB(v[i][k]);
					} 
				}
				v[i]=OK;
				break;
			}
		}
	}
	Gao(v);
	map<int,bool> ok__; 
	rep(i,N){
		rep(j,M-1){
			if(ok__[v[i][j]]) continue;
			ok__[v[i][j]]=1;
			lie[v[i][j]]++;
		}
	}
}
int main(){
	fastio;
	cin>>n>>m;
	vector<vector<int> >  sets(n+n);
	rb(i,1,n)
		rb(j,1,m){
			cin>>a[i][j];
		}
	rb(i,1,n){
		rb(j,1,m-1)
		sets[i-1].PB(a[i][j]);
		sets[i-1].PB(a[i][m]);
	}
	rb(i,1,n){
		rb(j,1,m-1){
			sets[i+n-1].PB((i-1)*(m)+j);
		}
		sets[i+n-1].PB(i*m);
	} 
	Gao(sets);
	rb(i,1,n){
		vector<int> res(m);
		rb(j,1,m){
			rb(k,1,m)
			if(lie[a[i][k]]==j){
				res[j-1]=k;
			} 
		}
		rb(j,1,m){
			cout<<a[i][res[j-1]]<<" ";
		}
		cout<<endl; 
	} 
	rb(i,1,n){
		vector<int> res(m);
		rb(j,1,m){
			rb(k,1,m)
			if(lie[(i-1)*(m)+k]==j){
				res[j-1]=k;
			}
		}
		rb(j,1,m){
			cout<<res[j-1]+(i-1)*m<<" ";
		}
		cout<<endl;
	}
	return 0;
}
/*
4 4
1 12 16 6
14 9 8 7
4 3 10 5
11 15 13 2

*/

Measured memory_bytes: 61232