// Tenshi ni Fureta yo!

#include <bits/stdc++.h>

using namespace std;

typedef long long LL;
typedef unsigned int UI;
typedef pair<int, int> pii;

#define DEBUG(...) fprintf(stderr, __VA_ARGS__)
#define MP make_pair
#define fst first
#define snd second
#define SZ(u) ((int) (u).size())
#define ALL(u) (u).begin(), (u).end()

inline void proc_status()
{
	ifstream t("/proc/self/status");
	cerr << string(istreambuf_iterator<char>(t), istreambuf_iterator<char>()) << endl;
}

template<typename T> inline int chkmin(T &a, const T &b) { return b < a ? a = b, 1 : 0; }
template<typename T> inline int chkmax(T &a, const T &b) { return b > a ? a = b, 1 : 0; }
template<typename T> inline T sqr(const T &val) { return val * val; }

namespace fastIO
{
	const int MAX_BUFFER_SIZE = 1 << 16;

	char buffer[MAX_BUFFER_SIZE], *cur = buffer, *ed = buffer;

	inline char getc() { return *(cur == ed ? ed = buffer + fread(buffer, 1, MAX_BUFFER_SIZE, stdin), cur = buffer : cur)++; }
}
using fastIO::getc;

template<typename T> inline T read()
{
	register T sum(0), fg(1);
	register char ch(getc());
	for(; !isdigit(ch); ch = getc()) if(ch == '-') fg = -1;
	for(;  isdigit(ch); ch = getc()) sum = sum * 10 - '0' + ch;
	return sum * fg;
}

// END tpl.

const int MAXN = 100;

int n, m;

int a[MAXN + 5][MAXN + 5];

inline void input()
{
	n = read<int>(), m = read<int>();
	for(int i = 1; i <= n; ++i)
		for(int j = 1; j <= m; ++j)
			a[i][j] = read<int>();
}

namespace MAT
{
	const int MAX_NODE = MAXN * 2;

	vector<int> X, Y;
	int G[MAX_NODE + 5][MAX_NODE + 5];

	inline void add_X(int u) { X.push_back(u); }
	inline void add_Y(int u) { Y.push_back(u); }
	inline void add_edge(int u, int v) { ++G[u][v]; }

	int p[MAX_NODE + 5], vis[MAX_NODE + 5];

	inline int dfs(int u)
	{
		vis[u] = 1;
		for(auto v : Y) if(G[u][v])
		{
			int t = p[v];
			p[u] = v, p[v] = u, p[t] = 0;
			if(!vis[t] && (!t || dfs(t))) return 1;
			p[u] = 0, p[v] = t, p[t] = v;
		}
		return 0;
	}

	inline int solve()
	{
		memset(p, 0, sizeof p);
		int ans = 0;
		for(int i = 1; i <= n; ++i) if(!p[i])
		{
			memset(vis, 0, sizeof vis);
			ans += dfs(i);
		}
		for(int i = 1; i <= n; ++i) --G[i][p[i]];
		return ans;
	}
}

inline void solve()
{
	static int X[MAXN + 5], Y[MAXN + 5], cnt = 0, id[MAXN * 2 + 5];

	for(int i = 1; i <= n; ++i) MAT::add_X(X[i] = ++cnt), id[X[i]] = i;
	for(int i = 1; i <= n; ++i) MAT::add_Y(Y[i] = ++cnt), id[Y[i]] = i;

	for(int i = 1; i <= n; ++i)
		for(int j = 1; j <= m; ++j)
		{
			int c = (a[i][j] - 1) / m + 1;
			MAT::add_edge(X[i], Y[c]);
		}

	auto output = [] ()
	{
		for(int i = 1; i <= n; ++i)
		{
			for(int j = 1; j <= m; ++j) printf("%d ", a[i][j]);
			puts("");
		}
	};

	auto build_col = [] (int k)
	{
		MAT::solve();
		for(int i = 1; i <= n; ++i)
			for(int j = k; j <= m; ++j) if((a[i][j] - 1) / m + 1 == id[MAT::p[i]]) { swap(a[i][k], a[i][j]); break; }
	};

	for(int i = 1; i <= m; ++i) build_col(i);
	output();

	auto sort_col = [] (int k)
	{
		static vector<int> A;
		A.clear();

		for(int i = 1; i <= n; ++i) A.push_back(a[i][k]);
		sort(ALL(A));
		for(int i = 1; i <= n; ++i) a[i][k] = A[i - 1];
	};

	for(int i = 1; i <= m; ++i) sort_col(i);
	output();
}

int main()
{
#ifdef K_ON
	freopen("D.in", "r", stdin);
	freopen("D.out", "w", stdout);
#endif

	input();
	solve();

	return 0;
}




Measured memory_bytes: 512