#include "bits/stdc++.h"

using namespace std;
#ifdef _DEBUG
#define _GLIBCXX_DEBUG
#include "dump.hpp"
#else
#define dump(...)
#endif

#define int long long
#define ll long long
#define DBG 1
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define loop(n) rep(loop, (0), (n))
#define all(c) begin(c), end(c)
const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9) + 7;
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
using pii = pair<int, int>;
// template<class T> ostream &operator<<(ostream &os,T &t){dump(t);return os;}
template <typename T, typename S>istream &operator>>(istream &is, pair<T, S> &p) { is >> p.first >> p.second; return is; }
//template <typename T, typename S>ostream &operator<<(ostream &os, pair<T, S> &p) {os << p.first << " " << p.second;return os;}

template <typename T> void printvv(const vector<vector<T>> &v) {
	cerr << endl;
	rep(i, 0, v.size()) rep(j, 0, v[i].size()) {
		cerr << v[i][j];
		cerr << (j == v[i].size() - 1 ? '\n' : ' ');
	}
	cerr << endl;
}

#ifndef _DEBUG
#define printvv(...)
#endif
void YES(bool f) { cout << (f ? "YES" : "NO") << endl; }
void Yes(bool f) { cout << (f ? "Yes" : "No") << endl; }
template <class T, class U>bool chmin(T& a, U b) { if (a > b) { a = b; return true; }return false; }
template <class T, class U>bool chmax(T& a, U b) { if (a < b) { a = b; return true; }return false; }
struct max_flow {
	struct edge { int to, cap, rev, label; edge(int to, int cap, int rev,int label) :to(to), cap(cap), rev(rev),label(label) {} };
	int V;
	vector<vector<edge>> G;
	vector<int> itr, level;

	max_flow(int V) : V(V) { G.assign(V, vector<edge>()); }

	void add_edge(int from, int to, int cap, int label) {
		G[from].emplace_back(to, cap, (int)G[to].size(), label);
		G[to].emplace_back(from, 0, (int)G[from].size() - 1, label);
	}

	void bfs(int s) {
		level.assign(V, -1);
		queue<int> q;
		level[s] = 0; q.push(s);
		while (!q.empty()) {
			int v = q.front(); q.pop();
			for (auto &e : G[v]) {
				if (e.cap > 0 and level[e.to] < 0) {
					level[e.to] = level[v] + 1;
					q.push(e.to);
				}
			}
		}
	}

	int dfs(int v, int t, int f) {
		if (v == t) return f;
		for (int& i = itr[v]; i < (int)G[v].size(); ++i) {
			edge& e = G[v][i];
			if (e.cap > 0 and level[v] < level[e.to]) {
				int d = dfs(e.to, t, min(f, e.cap));
				if (d > 0) {
					e.cap -= d;
					G[e.to][e.rev].cap += d;
					return d;
				}
			}
		}
		return 0;
	}

	int run(int s, int t) {
		int ret = 0, f;
		while (bfs(s), level[t] >= 0) {
			itr.assign(V, 0);
			while ((f = dfs(s, t, INF)) > 0) ret += f;
		}
		return ret;
	}
};


signed main(signed argc, char *argv[]) {
	cin.tie(0);
	ios::sync_with_stdio(false);
	cout << fixed << setprecision(12);

	int N, M; cin >> N >> M;
	vector<vector<pii>>v(N, vector<pii>(M));
	rep(i, 0, N)rep(j, 0, M) { cin >> v[i][j].second; v[i][j].second--; v[i][j].first = v[i][j].second / M; }

	vector<int>check(N*M);
	vector<vector<int>>w(N, vector<int>(M));

	rep(j, 0, M) {
		max_flow f(2 * N + 2);
		int s = 2 * N, t = s + 1;
		rep(i, 0, N) {
			rep(k, 0, M) {
				if (not check[v[i][k].second]) {
					f.add_edge(i, v[i][k].first+N, 1, v[i][k].second);
				}
			}
		}
		rep(i, 0, N) {
			f.add_edge(s, i, 1, -1);
			f.add_edge(i + N, t, 1, -1);
		}
		f.run(s, t);
		rep(i, 0, N) {
			for (auto e : f.G[i]) {
				if (N <= e.to and e.to < 2 * N and e.cap == 0) {
					w[i][j] = e.label;
					check[e.label] = 1;
				}
			}
		}
	}


	rep(i, 0, N) {
		rep(j, 0, M) {
			cout << w[i][j] + 1 << (j == M - 1 ? '\n' : ' ');
		}
	}


	rep(j, 0, M) {
		vector<int>x;
		rep(i, 0, N) {
			x.eb(w[i][j]);
		}
		sort(all(x));
		rep(i, 0, N) {
			w[i][j] = x[i];
		}
	}

	rep(i, 0, N) {
		rep(j, 0, M) {
			cout << w[i][j] + 1 << (j == M - 1 ? '\n' : ' ');
		}
	}



	return 0;
}


Measured memory_bytes: 1548