#include<bits/stdc++.h>
using namespace std;
#define inf 2123333333
#define pii pair<int,int>

struct node { int t,v,op,next; }a[100010];

queue<int> q;

int ans1[110][110],ans2[110][110];
int num[110][110],head[210],cur[210],level[210],n,m,vs,vt,tot;
bool vis[10010];

inline int rd()
{
	int x=0;char ch=getchar();
	for (;ch<'0'||ch>'9';ch=getchar());
	for (;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0';
	return x;
}

inline void print(int x)
{
	static char s[233];
	if (!x) { putchar('0');putchar(' ');return; }
	int tot=0;
	for (;x;x/=10) s[++tot]=x%10+'0';
	for (;tot;tot--) putchar(s[tot]);
	putchar(' ');
}

inline void add(int x,int y,int z)
{
	a[++tot].t=y;a[tot].v=z;a[tot].op=tot+1;a[tot].next=head[x];head[x]=tot;
	a[++tot].t=x;a[tot].v=0;a[tot].op=tot-1;a[tot].next=head[y];head[y]=tot;
}

inline bool bfs()
{
	while (!q.empty()) q.pop();
	memset(level,0,sizeof(level));
	level[vs]=1;cur[vs]=head[vs];q.push(vs);
	while (!q.empty())
	{
		int x=q.front();q.pop();
		for (int i=head[x];i;i=a[i].next) if (a[i].v)
		{
			int t=a[i].t;
			if (level[t]) continue;
			level[t]=level[x]+1;cur[t]=head[t];
			if (t==vt) return true;
			q.push(t);
		}
	}
	return false;
}

inline int dfs(int x,int y)
{
	if (x==vt||!y) return y;
	int res=0;
	for (int &i=cur[x];i;i=a[i].next) if (a[i].v)
	{
		int t=a[i].t;
		if (level[t]!=level[x]+1) continue;
		int hh=dfs(t,min(y,a[i].v));
		if (!hh) continue;
		res+=hh;y-=hh;
		a[i].v-=hh;a[a[i].op].v+=hh;
		if (!y) break;
	}
	return res;
}

inline void work(int id)
{
	memset(head,0,sizeof(head));tot=0;
	vs=0;vt=n*2+1;
	for (int i=1;i<=n;i++) for (int j=1;j<=m;j++) if (!vis[num[i][j]]) add(i,(num[i][j]+m-1)/m+n,1);
	for (int i=1;i<=n;i++) add(vs,i,1),add(i+n,vt,1);
	while (bfs()) dfs(vs,inf);
	for (int i=1;i<=n;i++)
	{
		int now=0;
		for (int j=head[i];j;j=a[j].next) if (a[j].t>n&&!a[j].v) now=a[j].t-n;
		for (int j=1;j<=m;j++) if (!vis[num[i][j]]&&(num[i][j]+m-1)/m==now)
		{
			ans1[i][id]=num[i][j];vis[num[i][j]]=true;
			break;
		}
	}
}

int main()
{
	n=rd();m=rd();
	for (int i=1;i<=n;i++) for (int j=1;j<=m;j++) num[i][j]=rd();
	for (int i=1;i<=m;i++) work(i);
	for (int i=1;i<=n;i++)
	{
		for (int j=1;j<=m;j++) print(ans1[i][j]);
		putchar('\n');
	}
	for (int i=1;i<=n;i++)
	{
		for (int j=1;j<=m;j++) for (int k=1;k<=n;k++) if (ans1[k][j]>(i-1)*m&&ans1[k][j]<=i*m) ans2[i][j]=ans1[k][j];
		for (int j=1;j<=m;j++) print(ans2[i][j]);
		putchar('\n');
	}
	return 0;
}