#include "bits/stdc++.h"
using namespace std;
using ll = long long;
using vll = std::vector<ll>;
using vvll = std::vector<vll>;
using vvvll = std::vector<vvll>;
using dd = double;
using vdd = std::vector<dd>;
using vvdd = std::vector<vdd>;
using vvvdd = std::vector<vvdd>;

constexpr ll INF = 1LL << 60;
constexpr dd EPS = 1e-11;

//cin,cout高速化のおまじない＋桁数指定
struct Fast{
	Fast(){
		cin.tie(0);
		ios::sync_with_stdio(false);
		cout << fixed << setprecision(std::numeric_limits<double>::max_digits10);
	}
} fast;

#define REPS(i, S, E) for (ll i = (S); i <= (E); i++)
#define REP(i, N) REPS(i, 0, N-1)
#define DEPS(i, S, E) for (ll i = (E); i >= (S); i--)
#define DEP(i, N) DEPS(i, 0, N-1)
#define EACH(e, v) for (auto&& e : v)

template<class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; }return false; }
template<class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; }return false; }
template<class T> inline T MaxE(vector<T>&v,ll S,ll E){T m=v[S]; REPS(i,S,E)chmax(m,v[i]); return m;}//v[S]～v[E]の最大値
template<class T> inline T MinE(vector<T>&v,ll S,ll E){T m=v[S]; REPS(i,S,E)chmin(m,v[i]); return m;}//v[S]～v[E]の最小値
template<class T> inline T MaxE(vector<T> &v, ll N) { return MaxE(v, 0, N - 1); } //先頭N個中の最大値
template<class T> inline T MinE(vector<T> &v, ll N) { return MinE(v, 0, N - 1); }
template<class T> inline T MaxE(vector<T> &v) { return MaxE(v, (ll)v.size()); }
template<class T> inline T MinE(vector<T> &v) { return MinE(v, (ll)v.size()); }
template<class T> inline ll MaxI(vector<T>&v,ll S,ll E){ll m=S; REPS(i,S,E){if(v[i]>v[m])m=i;} return m;}
template<class T> inline ll MinI(vector<T>&v,ll S,ll E){ll m=S; REPS(i,S,E){if(v[i]<v[m])m=i;} return m;}
template<class T> inline ll MaxI(vector<T> &v, ll N) { return MaxI(v, 0, N - 1); }
template<class T> inline ll MinI(vector<T> &v, ll N) { return MinI(v, 0, N - 1); }
template<class T> inline ll MaxI(vector<T> &v) { return MaxI(v, (ll)v.size()); }
template<class T> inline ll MinI(vector<T> &v) { return MinI(v, (ll)v.size()); }
template<class T> inline T Sum(vector<T> &v, ll S, ll E){ T s=v[S]; REPS(i, S+1, E)s+=v[i]; return s; }
template<class T> inline T Sum(vector<T> &v, ll N) { return Sum(v, 0, N - 1); }
template<class T> inline T Sum(vector<T> &v) { return Sum(v, v.size()); }
template<class T> inline T POW(T a, ll n){ T r=1; for (; n>0; n>>=1, a*=a){ if (n&1)r*=a; } return r; }
inline ll POW(int a, ll n){ return POW((ll)a, n); }
inline ll MSB(ll a){for(ll o=63,x=-1;;){ll m=(o+x)/2; if(a<(1LL<<m))o=m; else x=m; if(o-x==1)return x;}}
inline ll CEIL(ll a, ll b){ return (a+b-1)/b; }

template<class T=ll> inline vector<T> cinv(ll N){ vector<T> v(N); REP(i, N)cin>>v[i]; return move(v);}
template<class T=ll, class S=ll> inline vector<pair<T, S>> cinv2(ll N){
	vector<pair<T, S>> v(N); REP(i,N){cin>>v[i].first>>v[i].second;} return move(v); }
template<class T=ll,class S=ll,class R=ll> inline vector<tuple<T, S, R>> cinv3(ll N){
	vector<tuple<T,S,R>> v(N); REP(i,N){cin>>get<0>(v[i])>>get<1>(v[i])>>get<2>(v[i]);} return move(v);}
template<class T=ll,class S=ll,class R=ll,class Q=ll> inline vector<tuple<T,S,R,Q>> cinv4(ll N){
	vector<tuple<T,S,R,Q>> v(N); REP(i,N){cin>>get<0>(v[i])>>get<1>(v[i])>>get<2>(v[i])>>get<3>(v[i]);} return move(v);}
template<class T> inline void coutv(vector<T> &v, char deli=' '){
	ll N=(ll)v.size(); REP(i,N){cout << v[i] << ((i==N-1)?'\n':deli);} }
template<class T> void bye(T a){cout << a << '\n'; exit(0);}


struct mll{
	static ll MOD;
	ll val;
	mll(ll v = 0): val(v % MOD){ if (val < 0) val += MOD; }
	mll operator - () const { return -val; }
	mll operator + (const mll &b) const { return val + b.val; }
	mll operator - (const mll &b) const { return val - b.val; }
	mll operator * (const mll &b) const { return val * b.val; }
	mll operator / (const mll &b) const { return mll(*this) /= b; }
	mll operator + (ll b) const { return *this + mll(b); }
	mll operator - (ll b) const { return *this - mll(b); }
	mll operator * (ll b) const { return *this * mll(b); }
	friend mll operator + (ll a, const mll &b) { return b + a; }
	friend mll operator - (ll a, const mll &b) { return -b + a; }
	friend mll operator * (ll a, const mll &b) { return b * a; }
	mll &operator += (const mll &b) { val=(val+b.val)%MOD; return *this; }
	mll &operator -= (const mll &b) { val=(val+MOD-b.val)%MOD; return *this; }
	mll &operator *= (const mll &b) { val=(val*b.val)%MOD; return *this; }
	mll &operator /= (const mll &b) {
		ll c=b.val, d=MOD, u=1, v=0;
		while (d){
			ll t = c / d;
			c -= t * d; swap(c, d);
			u -= t * v; swap(u, v);
		}
		val = val * u % MOD;
		if (val < 0) val += MOD;
		return *this;
	}
	mll &operator += (ll b) { return *this += mll(b); }
	mll &operator -= (ll b) { return *this -= mll(b); }
	mll &operator *= (ll b) { return *this *= mll(b); }
	mll &operator /= (ll b) { return *this /= mll(b); }
	bool operator == (const mll &b) { return val == b.val; }
	bool operator != (const mll &b) { return val != b.val; }
	bool operator == (ll b) { return *this == mll(b); }
	bool operator != (ll b) { return *this != mll(b); }
	friend bool operator == (ll a, const mll &b) { return mll(a) == b.val; }
	friend bool operator != (ll a, const mll &b) { return mll(a) != b.val; }
	friend ostream &operator << (ostream &os, const mll &a) { return os << a.val; }
	friend istream &operator >> (istream &is, mll &a) { return is >> a.val; }
	static mll Combination(ll a, ll b){
		chmin(b, a-b);
		if (b<0) return mll(0);
		mll c = 1;
		REP(i, b) c *= a-i;
		REP(i, b) c /= i+1;
		return c;
	}
};
ll mll::MOD = (ll)(1e9 + 7);// 998244353LL;//(ll)(1e9 + 7);
using vmll = std::vector<mll>;
using vvmll = std::vector<vmll>;
using vvvmll = std::vector<vvmll>;
using vvvvmll = std::vector<vvvmll>;


class DinicMethod{
public:
	struct Edge{ ll to, cap, rev; };
	vector<vector<Edge>> G;
private:
	vll level;
	vll iter;
public:
	DinicMethod(ll nodeNum): G(nodeNum){}

	void Link(ll v, ll u, ll cap, ll rcap = 0){//v→uに重みcapで張る、逆辺も張る
		this->G[v].push_back({u,  cap, (ll)this->G[u].size()});
		this->G[u].push_back({v, rcap, (ll)this->G[v].size()-1});
	}

	ll MaxFlow(ll s, ll t){//s→tの最大流を求める
		ll f, flow = 0;
		while (true){
			this->BfsLevelRenew(s);
			if (this->level[t] < 0) return flow;
			iter.assign(this->G.size(), 0);
			while ((f = this->DfsFindPath(s, t, INF)) > 0) { flow += f; }
		}
	}

private:
	void BfsLevelRenew(ll s) {
		this->level.assign(this->G.size(), -1);
		this->level[s] = 0;
		queue<ll> que;
		que.push(s);
		while (!que.empty()){
			ll from = que.front(); que.pop();
			EACH(e, this->G[from]){
				if (e.cap > 0 && this->level[e.to] < 0){
					this->level[e.to] = this->level[from] + 1;
					que.push(e.to);
				}
			}
		}
	}

	ll DfsFindPath(ll v, ll t, ll f){
		if (v == t) return f;

		for (ll &i = iter[v]; i < (ll)this->G[v].size(); i++){
			Edge &e = this->G[v][i];
			if (e.cap > 0  &&  this->level[v] < this->level[e.to]){
				ll d = this->DfsFindPath(e.to, t, min(f, e.cap));
				if (d > 0){
					e.cap -= d;
					this->G[e.to][e.rev].cap += d;
					return d;
				}
			}
		}
		return 0;
	}
};

class DinicMatching{
private:
	DinicMethod dm;
	ll sNum, tNum, s, t;
public:
	DinicMatching(ll sNum, ll tNum): dm(sNum+tNum+2), sNum(sNum), tNum(tNum),
		s(sNum+tNum), t(sNum+tNum+1)
	{
		REP(i, sNum){ dm.Link(this->s, i, 1); }
		REP(i, tNum){ dm.Link(i+sNum, this->t, 1); }
	}

	void Link(ll sIdx, ll tIdx){ dm.Link(sIdx, tIdx + this->sNum, 1); }

	vector<pair<ll, ll>> GetMatch(){ //最大マッチングを得る
		ll matchNum = dm.MaxFlow(this->s, this->t);
		vector<pair<ll, ll>> matches;
		REP(v, this->sNum){//ソース側ノードをなめる
			EACH(e, dm.G[v]){
				if (e.cap == 0 && e.to != this->s){
					matches.emplace_back(v, e.to - sNum);
					break;
				}
			}
		}
		return move(matches);
	}
};


#if 0
class DinicMatching{
private:
	struct Edge{ ll to, cap, rev; };
	vector<vector<Edge>> G;
	vll level;
	vll iter;
	ll sNum, tNum, s, t;
public:
	DinicMatching(ll sNum, ll tNum):
		G(sNum+tNum+2), sNum(sNum), tNum(tNum), s(sNum+tNum), t(sNum+tNum+1)
	{
		REP(i, sNum){ this->link(this->s, i, 1); }
		REP(i, tNum){ this->link(i+sNum, this->t, 1); }
	}

	void Link(ll sIdx, ll tIdx){ this->link(sIdx, tIdx + this->sNum, 1); }
#if 0
		this->G[v].push_back({u,  1, (ll)this->G[u].size()});
		this->G[u].push_back({v,  0, (ll)this->G[v].size()-1});
#endif

	vector<pair<ll,ll>> GetMatch(){ //最大マッチングを得る
		ll matchNum = MaxFlow(this->s, this->t);
		vector<pair<ll, ll>> matches;
		REP(v, sNum){//ソース側ノードをなめる
			EACH(e, G[v]){
				if (e.cap == 0 && e.to != s){
					matches.emplace_back(v, e.to-sNum);
					break;
				}
			}
		}
		return move(matches);
	}

	ll MaxFlow(ll s, ll t){//s→tの最大流を求める
		ll f, flow = 0;
		while (true){
			this->BfsLevelRenew(s);
			if (this->level[t] < 0) return flow;
			iter.assign(this->G.size(), 0);
			while ((f = this->DfsFindPath(s, t, INF)) > 0) { flow += f; }
		}
	}

private:
	void link(ll v, ll u, ll cap, ll rcap = 0){//v→uに重みcapで張る、逆辺も張る
		this->G[v].push_back({u,  cap, (ll)this->G[u].size()});
		this->G[u].push_back({v, rcap, (ll)this->G[v].size()-1});
	}

	void BfsLevelRenew(ll s) {
		this->level.assign(this->G.size(), -1);
		this->level[s] = 0;
		queue<ll> que;
		que.push(s);
		while (!que.empty()){
			ll from = que.front(); que.pop();
			EACH(e, this->G[from]){
				if (e.cap > 0 && this->level[e.to] < 0){
					this->level[e.to] = this->level[from] + 1;
					que.push(e.to);
				}
			}
		}
	}

	ll DfsFindPath(ll v, ll t, ll f){
		if (v == t) return f;

		for (ll &i = iter[v]; i < (ll)this->G[v].size(); i++){
			Edge &e = this->G[v][i];
			if (e.cap > 0  &&  this->level[v] < this->level[e.to]){
				ll d = this->DfsFindPath(e.to, t, min(f, e.cap));
				if (d > 0){
					e.cap -= d;
					this->G[e.to][e.rev].cap += d;
					return d;
				}
			}
		}
		return 0;
	}
};
#endif

void solve()
{
	ll N, M;
	cin >> N >> M;

	vvll A(N);
	REP(i, N) A[i] = cinv(M);
	REP(i, N)REP(j, M) A[i][j]--;

	vvll B(N);

	REP(r, M){
#if 0
		vector<pair<ll, ll>> pairs;
		REP(i, N) REP(j, M){
			pairs.emplace_back(i, A[i][j]/M);
		}
		DinicMatching dm(N, N, pairs);
#endif
		DinicMatching dm(N, N);
		REP(i, N){
			REP(j, M-r) dm.Link(i, A[i][j]/M);
		}
		auto matches = dm.GetMatch();

		REP(i, N){
			REP(j, M-r){
				if (A[i][j]/M == matches[i].second){
					B[i].push_back(A[i][j]);
					A[i].erase(A[i].begin()+j);
					break;
				}
			}
		}
	}

	vvll C(N);
	REP(r, M){
		vll tmp(N);
		REP(i, N) tmp[i] =  B[i][r];
		sort(tmp.begin(), tmp.end());
		REP(i, N) C[i].push_back(tmp[i]);
	}


	REP(i, N)REP(j, M) B[i][j]++;
	REP(i, N)REP(j, M) C[i][j]++;

	REP(i, N) coutv(B[i]);
	REP(i, N) coutv(C[i]);
}

int main(){
	solve();
	return 0;
}


Measured memory_bytes: 1156