#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using uint = unsigned int;
#define rep(i,n) for(int i=0;i<int(n);i++)
#define rep1(i,n) for(int i=1;i<=int(n);i++)
#define per(i,n) for(int i=int(n)-1;i>=0;i--)
#define per1(i,n) for(int i=int(n);i>0;i--)
#define all(c) c.begin(),c.end()
#define si(x) int(x.size())
#define pb emplace_back
#define fs first
#define sc second
template<class T> using V = vector<T>;
template<class T> using VV = vector<vector<T>>;
template<class T,class U> void chmax(T& x, U y){if(x<y) x=y;}
template<class T,class U> void chmin(T& x, U y){if(y<x) x=y;}
template<class T> void mkuni(V<T>& v){sort(all(v));v.erase(unique(all(v)),v.end());}
template<class S,class T> ostream& operator<<(ostream& o,const pair<S,T> &p){
	return o<<"("<<p.fs<<","<<p.sc<<")";
}
template<class T> ostream& operator<<(ostream& o,const vector<T> &vc){
	o<<"{";
	for(const T& v:vc) o<<v<<",";
	o<<"}";
	return o;
}
constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n-1); }

#ifdef LOCAL
#define show(x) cerr << "LINE" << __LINE__ << " : " << #x << " = " << (x) << endl
void dmpr(ostream& os){os<<endl;}
template<class T,class... Args>
void dmpr(ostream&os,const T&t,const Args&... args){
	os<<t<<" ~ ";
	dmpr(os,args...);
}
#define shows(...) cerr << "LINE" << __LINE__ << " : ";dmpr(cerr,##__VA_ARGS__)
#define dump(x) cerr << "LINE" << __LINE__ << " : " << #x << " = {";  \
	for(auto v: x) cerr << v << ","; cerr << "}" << endl;
#else
#define show(x) void(0)
#define dump(x) void(0)
#define shows(...) void(0)
#endif

V<int> bipartite_edge_coloring(int N, V<pair<int,int>> es){
	using P = pair<int,int>;
	int M = es.size();
	int D = 0;
	{
		V<int> d(N);
		for(P e:es) d[e.fs]++,d[e.sc]++;
		rep(i,N) chmax(D,d[i]);
	}
	VV<int> vc2e(N,V<int>(D,-1));
	V<int> col(M,-1);
	auto color = [&](int e,int c){
		int x = es[e].fs, y = es[e].sc;
		if(col[e] != -1){
			vc2e[x][col[e]] = -1;
			vc2e[y][col[e]] = -1;
		}
		col[e] = c;
		vc2e[x][c] = e, vc2e[y][c] = e;
	};
	V<int> buf(M);
	rep(t,M){
		int x = es[t].fs, y = es[t].sc;
		int c0 = 0, c1 = 0;
		while(vc2e[x][c0] != -1) c0++;
		while(vc2e[y][c1] != -1) c1++;
		if(c0 == c1){
			color(t,c0);
			continue;
		}
		int v = x, c = c1, I = 0;
		while(vc2e[v][c] != -1){
			int e = vc2e[v][c];
			v ^= es[e].fs^es[e].sc;
			c ^= c0^c1;
			buf[I++] = e;
		}
		v = x;
		rep(i,I){
			int e = buf[i];
			col[e] ^= c0^c1;
			swap(vc2e[v][c0],vc2e[v][c1]);
			v ^= es[e].fs^es[e].sc;
		}
		swap(vc2e[v][c0],vc2e[v][c1]);
		color(t,c1);
	}
	return col;
}

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);		//DON'T USE scanf/printf/puts !!
	cout << fixed << setprecision(20);

	int H,W; cin >> H >> W;
	VV<int> A(H,V<int>(W));
	rep(i,H) rep(j,W) cin >> A[i][j],A[i][j]--;

	using P = pair<int,int>;
	V<P> es;
	V<P> v2xy(H*W);
	rep(i,H) rep(j,W) v2xy[A[i][j]] = P(i,j);
	rep(i,H*W){
		int x,y;tie(x,y) = v2xy[i];
		es.pb(x,H+i/W);
	}
	auto col = bipartite_edge_coloring(H+H,es);
	VV<int> B(H,V<int>(W,-1));
	rep(v,H*W){
		int x,y;tie(x,y) = v2xy[v];
		B[x][col[v]] = v;
	}
	VV<int> C(H,V<int>(W,-1));
	rep(w,W){
		V<int> v(H);
		rep(i,H) v[i] = B[i][w];
		sort(all(v));
		rep(i,H) C[i][w] = v[i];
	}
	rep(i,H){
		rep(j,W) cout << B[i][j] + 1 <<" ";
		cout << endl;
	}
	rep(i,H){
		rep(j,W) cout << C[i][j] + 1 <<" ";
		cout << endl;
	}
}


Measured memory_bytes: 768