#define DEBUG 0

#include <bits/stdc++.h>
using namespace std;

#if DEBUG
// basic debugging macros
int __i__,__j__;
#define printLine(l) for(__i__=0;__i__<l;__i__++){cout<<"-";}cout<<endl
#define printLine2(l,c) for(__i__=0;__i__<l;__i__++){cout<<c;}cout<<endl
#define printVar(n) cout<<#n<<": "<<n<<endl
#define printArr(a,l) cout<<#a<<": ";for(__i__=0;__i__<l;__i__++){cout<<a[__i__]<<" ";}cout<<endl
#define print2dArr(a,r,c) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<a[__i__][__j__]<<" ";}cout<<endl;}
#define print2dArr2(a,r,c,l) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<setw(l)<<setfill(' ')<<a[__i__][__j__]<<" ";}cout<<endl;}

// advanced debugging class
// debug 1,2,'A',"test";
class _Debug {
    public:
        template<typename T>
        _Debug& operator,(T val) {
            cout << val << endl;
            return *this;
        }
};
#define debug _Debug(),
#else
#define printLine(l)
#define printLine2(l,c)
#define printVar(n)
#define printArr(a,l)
#define print2dArr(a,r,c)
#define print2dArr2(a,r,c,l)
#define debug
#endif

// define
#define MAX_VAL 999999999
#define MAX_VAL_2 999999999999999999LL
#define EPS 1e-6
#define mp make_pair
#define pb push_back

// typedef
typedef unsigned int UI;
typedef long long int LLI;
typedef unsigned long long int ULLI;
typedef unsigned short int US;
typedef pair<int,int> pii;
typedef pair<LLI,LLI> plli;
typedef vector<int> vi;
typedef vector<LLI> vlli;
typedef vector<pii> vpii;
typedef vector<plli> vplli;

// ---------- END OF TEMPLATE ----------

int A[100][100];
vi rem[100];
int ans[100][100],ans2[100][100];
vi adjList[200];
int match[200];
int visited[200];
int augment(int u) {
    if (visited[u]) return 0;

    int i;
    visited[u] = 1;
    for (i = 0; i < adjList[u].size(); i++) {
        int v = adjList[u][i];
        if ((match[v] == -1) || augment(match[v])) {
            match[u] = v,match[v] = u;
            return 1;
        }
    }

    return 0;
}
int main() {
    int i,j;
    int N,M;
    scanf("%d %d",&N,&M);
    for (i = 0; i < N; i++) {
        for (j = 0; j < M; j++) scanf("%d",&A[i][j]);
    }

    int k;
    for (i = 0; i < N; i++) {
        for (j = 0; j < M; j++) rem[i].pb(A[i][j]-1);
    }
    for (i = 0; i < M; i++) {
        for (j = 0; j < 2*N; j++) adjList[j].clear();
        for (j = 0; j < N; j++) {
            for (k = 0; k < rem[j].size(); k++) {
                adjList[j].pb(rem[j][k]/M+N);
                adjList[rem[j][k]/M+N].pb(j);
            }
        }
        for (j = 0; j < 2*N; j++) match[j] = -1;
        for (j = 0; j < N; j++) {
            for (k = 0; k < 2*N; k++) visited[k] = 0;
            augment(j);
        }
        for (j = 0; j < N; j++) {
            for (k = 0; k < rem[j].size(); k++) {
                if (rem[j][k]/M == match[j]-N) {
                    ans[j][i] = rem[j][k];
                    break;
                }
            }
            swap(rem[j][k],rem[j].back());
            rem[j].pop_back();
        }
    }
    for (i = 0; i < N; i++) {
        for (j = 0; j < M; j++) printf("%d ",ans[i][j]+1);
        printf("\n");
    }
    for (i = 0; i < M; i++) {
        for (j = 0; j < N; j++) ans2[i][j] = ans[j][i];
        sort(ans2[i],ans2[i]+N);
    }
    for (i = 0; i < N; i++) {
        for (j = 0; j < M; j++) printf("%d ",ans2[j][i]+1);
        printf("\n");
    }

    return 0;
}



Measured memory_bytes: 640