#include <iostream>
#include <string>
#include <cmath>
#include<algorithm>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<iomanip>
#define _USE_MATH_DEFINES
#include <math.h>
#include <functional>
#include<complex>
using namespace std;

#define rep(i,x) for(ll i=0;i<x;i++)
#define repn(i,x) for(ll i=1;i<=x;i++)

typedef long long ll;
const ll INF = 1e17;
const ll MOD = 1000000007;
const ll MAX = 4000001;
const double eps = 1E-18;

ll max(ll a, ll b) {
	if (a > b) { return a; }
	return b;
}

ll min(ll a, ll b) {
	if (a > b) { return b; }
	return a;
}

ll gcd(ll a, ll b) {
	if (b == 0) { return a; }
	if (a < b) { return gcd(b, a); }
	return gcd(b, a % b);
}

ll lcm(ll a, ll b) {
	return a / gcd(a, b) * b;
}

struct edge {
	ll ind;
	ll fr;
	ll to;
	ll d;
};

class mint {
	long long x;
public:
	mint(long long x = 0) : x((x% MOD + MOD) % MOD) {}
	mint operator-() const {
		return mint(-x);
	}
	mint& operator+=(const mint& a) {
		if ((x += a.x) >= MOD) x -= MOD;
		return *this;
	}
	mint& operator-=(const mint& a) {
		if ((x += MOD - a.x) >= MOD) x -= MOD;
		return *this;
	}
	mint& operator*=(const  mint& a) {
		(x *= a.x) %= MOD;
		return *this;
	}
	mint operator+(const mint& a) const {
		mint res(*this);
		return res += a;
	}
	mint operator-(const mint& a) const {
		mint res(*this);
		return res -= a;
	}
	mint operator*(const mint& a) const {
		mint res(*this);
		return res *= a;
	}

	mint operator^(const ll& t)const {
		if (!t) return 1;
		mint a = pow(t >> 1);
		a *= a;
		if (t & 1) a *= *this;
		return a;
	}

	mint pow(ll t) const {
		if (!t) return 1;
		mint a = pow(t >> 1);
		a *= a;
		if (t & 1) a *= *this;
		return a;
	}
	// for prime MOD
	mint inv() const {
		return pow(MOD - 2);
	}
	mint& operator/=(const mint& a) {
		return (*this) *= a.inv();
	}
	mint operator/(const mint& a) const {
		mint res(*this);
		return res /= a;
	}

	friend ostream& operator<<(ostream& os, const mint& m) {
		os << m.x;
		return os;
	}
};

///////////////////////////

struct Ford_Fulkerson {

	struct f_edge {
		ll ind;
		ll to;
		ll cap;
		ll fl;
		ll rev;//反対方向の辺の添え字
	};


	ll N;
	ll s, t;
	vector<vector<edge>> g;//グラフ片方向にのみ張る、flowで双方向に構築
	vector<vector<f_edge>> flow;
	ll maxflow;

	vector<bool> see;
	ll dfs(ll v, ll x) {
		see[v] = 1;

		if (v == t) { return x; }

		rep(i, flow[v].size()) {
			const auto e = flow[v][i];

			if (see[e.to] == 1) { continue; }
			if (e.cap == e.fl) { continue; }

			ll y = min(x, e.cap - e.fl);

			y = dfs(e.to, y);
			if (y == 0) { continue; }

			flow[v][i].fl += y;
			flow[e.to][e.rev].fl -= y;
			return y;
		}

		return 0;
	}

	Ford_Fulkerson(ll N0, vector<vector<edge>> g0, ll s0, ll t0) {
		N = N0;
		g = g0;
		s = s0;
		t = t0;

		flow.resize(N + 1);
		repn(v, N) {
			for (edge e : g[v]) {
				ll w = e.to;
				flow[v].push_back({ e.ind,w,e.d,0,flow[w].size() });
				flow[w].push_back({ e.ind,v,0,0,flow[v].size() - 1 });
			}
		}

		maxflow = 0;

		if (s == t) { maxflow = INF; return; }

		while (1) {
			see.assign(N + 1, 0);
			ll f = dfs(s, INF);
			if (f == 0) { break; }
			maxflow += f;
		}

		maxflow = min(INF, maxflow);

		return;

	}
};

int main() {
	ll N, M;
	cin >> N >> M;

	vector<vector<ll>> A(N + 1, vector<ll>(M + 1));
	repn(i, N)repn(j, M) cin >> A[i][j];

	vector<ll> fr(N*M + 1), to(N*M + 1);
	repn(i, N)repn(j, M) {
		ll x = A[i][j];
		to[x] = (x - 1) / M + 1;
		fr[x] = i;
	}

	//repn(x, N*M) {
	//	cout << x << " " << fr[x] << " " << to[x] << endl;
	//}

	vector<ll> gr(N*M + 1, 0);

	repn(i, M) {
		vector<vector<edge>> g(2 * N + 3);
		repn(i, N) {
			g[2 * N + 1].push_back({ 0,2 * N + 1,i,1 });
			g[N + i].push_back({ 0,N + i,2 * N + 2,1 });
		}

		repn(x, N*M) {
			if (gr[x] != 0) { continue; }
			g[fr[x]].push_back({ x,fr[x],N + to[x],1 });
		}

		Ford_Fulkerson gmatch(2 * N + 2, g, 2 * N + 1, 2 * N + 2);

		repn(v, N) {
			for (auto e : gmatch.flow[v]) {
				if (e.to==2*N+1) { continue; }
				//cout << e.ind << " " << v << " " << e.to << " " << e.fl << endl;
				if (e.fl == 1) { gr[e.ind] = i; }
			}
		}
		//cout << endl;
	}

	vector <vector<ll>> B(N + 1, vector<ll>(M + 1)), C(N + 1, vector<ll>(M + 1));
	repn(x, M*N) {
		B[fr[x]][gr[x]] = x;
		C[to[x]][gr[x]] = x;
	}
	
	

	repn(i, N) {
		repn(j, M) cout << B[i][j]<<" ";
		cout << endl;
	}
	repn(i, N) {
		repn(j, M) cout << C[i][j]<<" ";
		cout << endl;
	}

	system("PAUSE");

}

Measured memory_bytes: 2752