Problem:
Score : 1100 points

Problem Statement

We have a grid with N rows and M columns of squares.
Each integer from 1 to NM is written in this grid once.
The number written in the square at the i-th row from the top and the j-th column from the left is A_{ij}.

You need to rearrange these numbers as follows:

First, for each of the N rows, rearrange the numbers written in it as you like.

Second, for each of the M columns, rearrange the numbers written in it as you like.

Finally, for each of the N rows, rearrange the numbers written in it as you like.

After rearranging the numbers, you want the number written in the square at the i-th row from the top and the j-th column from the left to be M\times (i-1)+j.
Construct one such way to rearrange the numbers. The constraints guarantee that it is always possible to achieve the objective.

Constraints

1 \leq N,M \leq 100

1 \leq A_{ij} \leq NM

A_{ij} are distinct.

Input

Input is given from Standard Input in the following format:

N M
A_{11} A_{12} ... A_{1M}
:
A_{N1} A_{N2} ... A_{NM}

Output

Print one way to rearrange the numbers in the following format:

B_{11} B_{12} ... B_{1M}
:
B_{N1} B_{N2} ... B_{NM}
C_{11} C_{12} ... C_{1M}
:
C_{N1} C_{N2} ... C_{NM}

Here B_{ij} is the number written in the square at the i-th row from the top and the j-th column from the left after Step 1, and C_{ij} is the number written in that square after Step 2.

Sample Input 1

3 2
2 6
4 3
1 5

Sample Output 1

2 6
4 3
5 1
2 1
4 3
5 6

Sample Input 2

3 4
1 4 7 10
2 5 8 11
3 6 9 12

Sample Output 2

1 4 7 10
5 8 11 2
9 12 3 6
1 4 3 2
5 8 7 6
9 12 11 10

Solution in C++14 (GCC 5.4.1) (judge verdict: Runtime Error)

#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];
	vector<pii> pos(n * m);
	REP(i, n)REP(j, m)pos[v[i][j] - 1] = { i,j };
	vvi next(n, vi(m, -1));
	REP(i, n) {
		MaxFlow mf(n + m + 2);
		REP(a, n)REP(b, m) {
			if (next[a][b]==-1) {
				mf.add_edge(a + 1, b + n + 1,1);
			}
		}
		vvi tmp(n);
		REP(j, m) {
			tmp[pos[i*m+j].first].push_back(pos[i * m + j].second);
		}
		vi cnt(n);
		REP(j, m) {
			cnt[pos[i*m+j].first]++;
		}
		REP(j, n)mf.add_edge(0, j + 1, cnt[j]);
		REP(j, m)mf.add_edge(j + n + 1, n + m + 1, 1);
		int k=mf.max_flow(0, n + m + 1);
		assert(k == m);
		vvi t(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) {
					t[i].push_back(mf.g[i+1][j].to-n-1);
				}
			}
		}

		REP(i, n)REP(j, t[i].size()) {
			next[i][t[i][j]] = v[i][tmp[i][j]];
		}
	}
	REP(i, n) {
		REP(j, m)cout << next[i][j] << " ";
		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] << " ";
		cout << endl;
	}

}