#include "bits/stdc++.h"

#define REP(i,n) for(ll i=0;i<ll(n);++i)
#define RREP(i,n) for(ll i=ll(n)-1;i>=0;--i)
#define FOR(i,m,n) for(ll i=m;i<ll(n);++i)
#define RFOR(i,m,n) for(ll i=ll(n)-1;i>=ll(m);--i)
#define ALL(v) (v).begin(),(v).end()
#define UNIQUE(v) v.erase(unique(ALL(v)),v.end());
#define INF 1000000001ll
#define MOD 1000000007ll
#define EPS 1e-9

constexpr int dx[8] = { 1,1,0,-1,-1,-1,0,1 };
constexpr int dy[8] = { 0,1,1,1,0,-1,-1,-1 };


using namespace std;

using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
using vvi = vector<vi>;
using vvl = vector<vl>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

template <class T> bool chmin(T& a, const T& b) { if (a > b) { a = b; return true; } return false; }
template <class T> bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } return false; }


#include "bits/stdc++.h"

struct Edge { int to; long long cap; int rev; long long cost; };
typedef std::vector<Edge> Edges;
typedef std::vector<Edges> Graph;


struct MaxFlow {
	Graph g;
	std::vector<int> level, iter;
	MaxFlow(int n) {
		level.resize(n);
		iter.resize(n);
		g.resize(n);
	}
	void add_edge(int from, int to, int cap, int cost = 0) {
		g[from].push_back(Edge{ to, cap, (int)g[to].size(),cost });
		g[to].push_back(Edge{ from,0,(int)g[from].size() - 1,-cost });
	}

	void bfs(int s) {
		std::fill(level.begin(), level.end(), -1);
		std::queue<int> q;
		level[s] = 0;
		q.push(s);
		while (!q.empty()) {
			int v = q.front(); q.pop();
			for (int i = 0; i < (int)g[v].size(); i++) {
				Edge& e = g[v][i];
				if (e.cap > 0 && level[e.to] < 0) {
					level[e.to] = level[v] + 1;
					q.push(e.to);
				}
			}
		}
	}
	long long dfs(int v, int t, long long f) {
		if (v == t)return f;
		for (int& i = iter[v]; i < (int)g[v].size(); i++) {
			Edge& e = g[v][i];
			if (e.cap > 0 && level[v] < level[e.to]) {
				long long d = dfs(e.to, t, std::min(f, (long long)e.cap));
				if (d > 0) {
					e.cap -= d;
					g[e.to][e.rev].cap += d;
					return d;
				}
			}
		}
		return 0;
	}
	long long max_flow(int s, int t) {
		long long flow = 0;
		for (;;) {
			bfs(s);
			if (level[t] < 0)return flow;
			std::fill(iter.begin(), iter.end(), 0);
			long long f;
			while ((f = dfs(s, t, std::numeric_limits<long long> ::max())) > 0) {
				flow += f;
			}
		}
	}
};


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

struct MinCostFlow {
	using P = std::pair<long long, int>;

	int n;
	std::vector<long long> h, dist;
	std::vector<int> prevv, preve;
	Graph g;
	MinCostFlow(int n_) : n(n_) {
		h.resize(n, 0);
		dist.resize(n);
		prevv.resize(n);
		preve.resize(n);
	}

	void add_edge(int from, int to, int cap, int cost) {
		g[from].push_back(Edge{ to, cap, (int)g[to].size(),cost });
		g[to].push_back(Edge{ from,0,(int)g[from].size() - 1,-cost });
	}

	long long min_cost_flow(int s, int t, long long f) {
		long long res = 0;
		while (f > 0) {
			std::priority_queue<P, std::vector<P>, std::greater<P>> que;
			std::fill_n(dist.begin(), n, std::numeric_limits<long long>::max());
			dist[s] = 0;
			que.push({ 0,s });
			while (!que.empty()) {
				P p = que.top(); que.pop();
				int v = p.second;
				if (dist[v] < p.first)continue;
				for (int i = 0; i < (int)g[v].size(); i++) {
					Edge& e = g[v][i];
					if (e.cap > 0 && dist[e.to] > dist[v] + e.cost + h[v] - h[e.to]) {
						dist[e.to] = dist[v] + e.cost + h[v] - h[e.to];
						prevv[e.to] = v;
						preve[e.to] = i;
						que.push({ dist[e.to],e.to });
					}
				}
			}
			if (dist[t] == std::numeric_limits<long long>::max()) {
				return -1;
			}
			for (int v = 0; v < n; v++)h[v] += dist[v];

			long long d = f;
			for (int v = t; v != s; v = prevv[v]) {
				d = std::min(d, (long long)g[prevv[v]][preve[v]].cap);
			}
			f -= d;
			res += d * h[t];
			for (int v = t; v != s; v = prevv[v]) {
				Edge& e = g[prevv[v]][preve[v]];
				e.cap -= d;
				g[v][e.rev].cap += d;
			}
		}
		return res;
	}
};
int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	int n, m;
	cin >> n >> m;
	vvi v(n, vi(m));
	REP(i, n)REP(j, m) {
		cin >> v[i][j];
		v[i][j]--;
	}

	vvi next(n, vi(m));
	vi used(n * m);
	vector<vector<queue<int>>> q(n, vector<queue<int>>(n));
	REP(i, n) {
		REP(j, m) {
			q[i][v[i][j] / m].push(v[i][j]);
		}
	}
	REP(j, m) {
		MaxFlow mf(n + n + 2);
		REP(i, n)REP(j, m) {
			if (!used[v[i][j]])mf.add_edge(i + 1, v[i][j] / m + n + 1, 1);
		}
		REP(i, n) {
			mf.add_edge(0, i + 1, 1);
			mf.add_edge(i + n + 1, n + n + 1, 1);
		}
		int k = mf.max_flow(0, 2 * n + 1);
		assert(k == n);
		vi pos(n);
		REP(i, n) {
			REP(j, mf.g[i + 1].size()) {
				if (mf.g[i + 1][j].to != 0 && mf.g[i + 1][j].cap == 0) {
					pos[i] = mf.g[i + 1][j].to - n - 1;
				}
			}
		}
		REP(i, n) {
			int tmp = q[i][pos[i]].front(); q[i][pos[i]].pop();
			used[tmp] = 1;
			next[i][j] = tmp;
		}
	}
	REP(i, n) {
		REP(j, m)
			cout << next[i][j] + 1 << " ";
		cout << endl;
	}
	vvi ans(n, vi(m));
	REP(j, m) {
		vi tmp(n);
		REP(i, n)tmp[i] = next[i][j];
		sort(ALL(tmp));
		REP(i, n)ans[i][j] = tmp[i];
	}
	REP(i, n) {
		REP(j, m)
			cout << ans[i][j] + 1 << " ";
		cout << endl;
	}
}

Measured memory_bytes: 8080