/*{{{*/
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<string>
#include<iostream>
#include<sstream>
#include<set>
#include<map>
#include<queue>
#include<bitset>
#include<vector>
#include<limits.h>
#include<assert.h>
#define SZ(X) ((int)(X).size())
#define ALL(X) (X).begin(), (X).end()
#define REP(I, N) for (int I = 0; I < (N); ++I)
#define REPP(I, A, B) for (int I = (A); I < (B); ++I)
#define FOR(I, A, B) for (int I = (A); I <= (B); ++I)
#define FORS(I, S) for (int I = 0; S[I]; ++I)
#define RS(X) scanf("%s", (X))
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define GET_POS(c,x) (lower_bound(c.begin(),c.end(),x)-c.begin())
#define CASET int ___T; scanf("%d", &___T); for(int cs=1;cs<=___T;cs++)
#define MP make_pair
#define PB push_back
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define F first
#define S second
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef long double LD;
typedef pair<int,int> PII;
typedef vector<int> VI;
typedef vector<LL> VL;
typedef vector<PII> VPII;
typedef pair<LL,LL> PLL;
typedef vector<PLL> VPLL;
template<class T> void _R(T &x) { cin >> x; }
void _R(int &x) { scanf("%d", &x); }
void _R(LL &x) { scanf("%lld", &x); }
void _R(double &x) { scanf("%lf", &x); }
void _R(char &x) { scanf(" %c", &x); }
void _R(char *x) { scanf("%s", x); }
void R() {}
template<class T, class... U> void R(T &head, U &... tail) { _R(head); R(tail...); }
template<class T> void _W(const T &x) { cout << x; }
void _W(const int &x) { printf("%d", x); }
void _W(const LL &x) { printf("%lld", x); }
void _W(const double &x) { printf("%.16f", x); }
void _W(const char &x) { putchar(x); }
void _W(const char *x) { printf("%s", x); }
template<class T,class U> void _W(const pair<T,U> &x) {_W(x.F); putchar(' '); _W(x.S);}
template<class T> void _W(const vector<T> &x) { for (auto i = x.begin(); i != x.end(); _W(*i++)) if (i != x.cbegin()) putchar(' '); }
void W() {}
template<class T, class... U> void W(const T &head, const U &... tail) { _W(head); putchar(sizeof...(tail) ? ' ' : '\n'); W(tail...); }
#ifdef HOME
 #define DEBUG(...) {printf("# ");printf(__VA_ARGS__);puts("");}
#else
 #define DEBUG(...)
#endif
int MOD = 1e9+7;
void ADD(LL& x,LL v){x=(x+v)%MOD;if(x<0)x+=MOD;}
/*}}}*/
const int SIZE = 1e6+10;
#define FN 500010
#define FM 2000010
#define INF 1000000000000000LL
struct E {
    int k;
    LL c;
    int tag;
    E(){}
    E( int _k, LL _c, int _tag ):k(_k),c(_c),tag(_tag){}
} es[FM];

struct Flow {
    int n,m,dis[FN];
    int qq[FN],qr,ql;
    int ptr[FN];
    vector<int> e[FN];
    void init( int _n ) {
        n=_n; m=0;
        for ( int i=0; i<n; i++ ) {
            vector<int>tmp;
            e[i].swap(tmp);
        }
    }
    void add_edge( int a, int b, LL c , int tag) {
        e[a].push_back(m); es[m]=E(b,c,tag); m++;
        e[b].push_back(m); es[m]=E(a,0,tag); m++;
    }
    bool BFS() {
        memset(dis,-1,n*sizeof(int));
        ql=qr=0;
        qq[qr++]=0;
        dis[0]=0;
        while ( ql!=qr && dis[n-1]==-1 ) {
            int p=qq[ql++];
            for(auto& it:e[p]) {
                E ee=es[it];
                if ( ee.c==0 || dis[ee.k]!=-1 ) continue;
                dis[ee.k]=dis[p]+1;
                qq[qr++]=ee.k;
            }
        }
        return dis[n-1]!=-1;
    }
    LL go( int p, LL c ) {
        if ( p==n-1 ) return c;
        LL ret=0,tmp;
        for(int &i=ptr[p];i<SZ(e[p]);i++){
            auto it=e[p][i];
            E &ee=es[it];
            if ( ee.c==0 || dis[p]+1!=dis[ee.k] ) continue;
            tmp=go(ee.k,min(c-ret,(LL)ee.c));
            ret+=tmp; ee.c-=tmp; es[it^1].c+=tmp;
            if ( ret==c ) break;
        }
        if ( ret==0 ) dis[p]=-1;
        return ret;
    }
    LL maxflow() {
        LL ret=0;
        while ( BFS() ) {
            memset(ptr,0,n*sizeof(int));
            ret+=go(0,1LL<<60);
        }
        return ret;
    }
} flow;
int a[111][111];
int an[111][111];
bool u[111111];
int main(){
    int N,M; R(N,M);
    FOR(i,1,N)FOR(j,1,M){
        R(a[i][j]);
        a[i][j]--;
    }
    flow.init(2+N*2);
    FOR(i,1,N)flow.add_edge(0,i,1,-1);
    FOR(i,1,N)flow.add_edge(i+N,N*2+1,1,-1);
    FOR(i,1,N){
        FOR(j,1,M){
            flow.add_edge(i,a[i][j]/M+N+1,1,a[i][j]);
        }
    }
    REP(i,M){
        //REP(j,flow.m)W("{",es[j].k,es[j].c,"}");
        flow.maxflow();
        for(int j=0;j<flow.m;j+=2){
            if(es[j].tag!=-1&&!u[es[j].tag]&&es[j].c==0){
                //W("[",i,"]",es[j^1].k,es[j].tag);
                u[es[j].tag]=1;
                es[j^1].c=0;
                an[i][es[j^1].k]=es[j].tag;
            }
        }
        REP(j,2*N)es[j*2].c++;
    }
    FOR(i,1,N)FOR(j,1,M){
        printf("%d%c",an[j-1][i]+1," \n"[j==M]);
    }
    REP(i,M)sort(an[i]+1,an[i]+N+1);
    FOR(i,1,N)FOR(j,1,M){
        printf("%d%c",an[j-1][i]+1," \n"[j==M]);
    }
    return 0;
}


Measured memory_bytes: 16896