#include <map>
#include <set>
#include <ctime>
#include <queue>
#include <stack>
#include <cmath>
#include <vector>
#include <bitset>
#include <cstdio>
#include <cctype>
#include <string>
#include <numeric>
#include <cstring>
#include <cassert>
#include <climits>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std ;
#define rep(i, a, b) for (int i = (a); i <= (b); ++i)
#define per(i, a, b) for (int i = (a); i >= (b); --i)
#define loop(it, v) for (auto it = v.begin(); it != v.end(); it++)
#define cont(i, x) for (int i = head[x]; i; i = edge[i].nex)
#define clr(a) memset(a, 0, sizeof(a))
#define ass(a, cnt) memset(a, cnt, sizeof(a))
#define cop(a, b) memcpy(a, b, sizeof(a))
#define lowbit(x) (x & -x)
#define all(x) x.begin(), x.end()
#define SC(t, x) static_cast <t> (x)
#define ub upper_bound
#define lb lower_bound
#define pqueue priority_queue
#define mp make_pair
#define pb push_back
#define pof pop_front
#define pob pop_back
#define fi first
#define se second
#define y1 y1_
#define Pi acos(-1.0)
#define iv inline void
#define enter cout << endl
#define siz(x) ((int)x.size())
#define file(x) freopen(x".in", "r", stdin),freopen(x".out", "w", stdout)
typedef double db ;
typedef long long ll ;
typedef unsigned long long ull ;
typedef pair <int, int> pii ;
typedef vector <int> vi ;
typedef vector <pii> vii ;
typedef queue <int> qi ;
typedef queue <pii> qii ;
typedef set <int> si ;
typedef map <int, int> mii ;
typedef map <string, int> msi ;
const int maxn = 2e2 + 5 ;
const int inf = 0x3f3f3f3f ;
const int iinf = 1 << 30 ;
const ll linf = 2e18 ;
const int mod = 19491001 ;
const double eps = 1e-7 ;
template <class T> void read(T &a)
{
	int f = 1 ; a = 0;
	char ch = getchar() ;
	while (!isdigit(ch)) { if (ch == '-') f = -1 ; ch = getchar() ; }
	while (isdigit(ch)) { a =  (a << 3) + (a << 1) + ch - '0' ; ch = getchar() ; }
	a *= f ;
}

int n, m;

int a[maxn][maxn], sum[maxn][maxn], b[maxn][maxn];

struct node
{
	int nex, to;
}
edge[maxn * maxn];

int head[maxn], tot, match[maxn];

bool vis[maxn];

void add(int u, int v)
{
	edge[++tot].nex = head[u];
	edge[tot].to = v;
	head[u] = tot;
}

int fr(int a)
{
	return (a - 1) / m + 1;
}

void init()
{
	clr(head), tot = 0;
	rep(i, 1, n) rep(j, 1, n) if(sum[i][j]) add(i, n + j), add(n + j, i);
	return ;
}

bool dfs(int now)
{
	cont(i, now)
	{
		int to = edge[i].to;
		if(!vis[to])
		{
			vis[to] = 1;
			if( !match[to] || dfs(match[to]))
			{
				match[to] = now, match[now] = to;
				return 1;
			}
		}
	}
	return 0;
}

void hungarian(int list)
{
	clr(match);
	rep(i, 1, n)
	{
		if(!match[i])
		{
			clr(vis);
			dfs(i);
		}
	}
	rep(i, 1, n)
	{
		int col = match[i] - n;
		b[i][list] = col, sum[i][col] --;
	}
	return ;
}

signed main()
{
	scanf("%d %d", &n, &m);
	rep(i, 1, n) rep(j, 1, m)
	{
		scanf("%d", &a[i][j]);
		sum[i][fr(a[i][j])] ++;
	}
	rep(i, 1, m)
	{
		init();
		hungarian(i);
	}
	rep(i, 1, n) rep(j, 1, m) rep(k, j, m) if(fr(a[i][k]) == b[i][j])
	{
		swap(a[i][j], a[i][k]);
		break;
	}
	rep(i, 1, n) rep(j, 1, m) printf("%d%c", a[i][j], " \n"[j == m]);
	rep(i, 1, m) rep(j, 1, n) rep(k, j, n) if(fr(a[k][i]) == j)
	{
		swap(a[k][i], a[j][i]);
		break;
	}
	rep(i, 1, n) rep(j, 1, m) printf("%d%c", a[i][j], " \n"[j == m]);
	return 0;
}

Measured memory_bytes: 768