// Author -- Frame

#include<bits/stdc++.h>

#define lowbit(x) ((x)&(-(x)))
#define Finline __inline__ __attribute__ ((always_inline))
#define DEBUG fprintf(stderr,"Running on Line %d in Function %s\n",__LINE__,__FUNCTION__)
#define SZ(x) ((int)x.size())
#define mkpr std::make_pair
#define pb push_back

typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef std::pair<int,int> pi;
typedef std::pair<ll,ll> pl;
using std::min;
using std::max;

const int inf=0x3f3f3f3f,Inf=0x7fffffff;
const ll INF=0x3f3f3f3f3f3f3f3f;

Finline uint rnd(){static uint seed=416;return seed^=seed>>5,seed^=seed<<17,seed^=seed>>13;}
template <typename _Tp>_Tp gcd(const _Tp &a,const _Tp &b){return (!b)?a:gcd(b,a%b);}
template <typename _Tp>Finline _Tp abs(const _Tp &a){return a>=0?a:-a;}
template <typename _Tp>Finline void chmax(_Tp &a,const _Tp &b){(a<b)&&(a=b);}
template <typename _Tp>Finline void chmin(_Tp &a,const _Tp &b){(b<a)&&(a=b);}
template <typename _Tp>Finline void read(_Tp &x)
{
	char ch(getchar());bool f(false);while(!isdigit(ch)) f|=ch==45,ch=getchar();
	x=ch&15,ch=getchar();while(isdigit(ch)) x=(((x<<2)+x)<<1)+(ch&15),ch=getchar();
	f&&(x=-x);
}
template <typename _Tp,typename... Args>Finline void read(_Tp &t,Args &...args){read(t);read(args...);}
template <typename _Tp,typename... Args>Finline _Tp min(const _Tp &a,const _Tp &b,const Args &...args){return a<b?min(a,args...):min(b,args...);}
template <typename _Tp,typename... Args>Finline _Tp max(const _Tp &a,const _Tp &b,const Args &...args){return a<b?max(b,args...):max(a,args...);}
Finline int read_str(char *s)
{
	char ch(getchar());while(ch==' '||ch=='\r'||ch=='\n') ch=getchar();
	char *tar=s;*tar=ch,ch=getchar();while(ch!=' '&&ch!='\r'&&ch!='\n'&&ch!=EOF) *(++tar)=ch,ch=getchar();
	return tar-s+1;
}

const int N=215;
namespace flow
{
	struct edge{
		int v,nxt;
		int w,id;
	}c[N*N<<3];
	int front[N],edge_cnt;
	int cur[N],dep[N],S,T;
	int _q[N],_l,_r;
	Finline void addedge(int u,int v,int w,int id)
	{
		c[++edge_cnt]=(edge){v,front[u],w,id},front[u]=edge_cnt;
		c[++edge_cnt]=(edge){u,front[v],0,id},front[v]=edge_cnt;
	}
	Finline void init(){memset(front,255,sizeof(front)),edge_cnt=-1;}
	bool bfs()
	{
		memset(dep,255,sizeof(dep));
		memcpy(cur,front,sizeof(cur));
		dep[S]=0;
		_q[_l=_r=1]=S;
		while(_l!=_r+1)
		{
			int x=_q[_l++];
			for(int i=front[x];~i;i=c[i].nxt)
			{
				if(c[i].id>=0&&c[i].w&&!~dep[c[i].v])
				{
					dep[c[i].v]=dep[x]+1;
					_q[++_r]=c[i].v;
				}
			}
		}
		return ~dep[T];
	}
	int dfs(int x,int flow)
	{
		if(x==T||!flow) return flow;
		int f=0,rf;
		for(int &i=cur[x];~i;i=c[i].nxt)
		{
			if(c[i].id>=0&&dep[c[i].v]==dep[x]+1&&(rf=dfs(c[i].v,min(flow,c[i].w))))
			{
				flow-=rf,f+=rf;
				c[i].w-=rf,c[i^1].w+=rf;
				if(!flow) return f;
			}
		}
		return f;
	}
	int dinic()
	{
		int ans=0;
		while(bfs()) ans+=dfs(S,inf);
		return ans;
	}
}
using namespace flow;
int a[105][105],b[105][105];
int main()
{
	init();
	int n,m;read(n,m);
	S=n+n+1,T=S+1;
	for(int i=1;i<=n;++i)
	{
		for(int j=1;j<=m;++j)
		{
			read(a[i][j]);
			int col=(a[i][j]-1)/m+1;
			addedge(i,col+n,1,a[i][j]);
		}
	}
	for(int t=1;t<=m;++t)
	{
		for(int i=1;i<=n;++i)
		{
			addedge(S,i,1,0);
			addedge(i+n,T,1,0);
		}
		assert(dinic()==n);
		for(int i=1;i<=n;++i)
		{
			for(int _=front[i];~_;_=c[_].nxt)
			{
				if(!c[_].w&&c[_].id>0)
				{
					b[i][t]=c[_].id;
					c[_].id=c[_^1].id=-1;
				}
			}
		}
	}
	for(int i=1;i<=n;++i) for(int j=1;j<=m;++j) printf("%d%c",b[i][j]," \n"[j==m]);
	for(int i=1;i<=m;++i)
	{
		std::vector<int> v(n);
		for(int j=0;j<n;++j) v[j]=b[j+1][i];
		std::sort(v.begin(),v.end());
		for(int j=0;j<n;++j) b[j+1][i]=v[j];
	}
	for(int i=1;i<=n;++i) for(int j=1;j<=m;++j) printf("%d%c",b[i][j]," \n"[j==m]);
	return 0;
}


Measured memory_bytes: 4724