#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define pb push_back
#define mp make_pair
#define all(a) begin(a),end(a)
#define FOR(x,val,to) for(int x=(val);x<int((to));++x)
#define FORE(x,val,to) for(auto x=(val);x<=(to);++x)
#define FORR(x,arr) for(auto &x: arr)
#define FORS(x,plus,arr) for(auto x = begin(arr)+(plus); x != end(arr); ++x)
#define FORREV(x,plus,arr) for(auto x = (arr).rbegin()+(plus); x !=(arr).rend(); ++x)
#define REE(s_) {cout<<s_<<'\n';exit(0);}
#define GET(arr) for(auto &i: (arr)) sc(i)
#define whatis(x) cerr << #x << " is " << (x) << endl;
#define e1 first
#define e2 second
#define INF 0x7f7f7f7f
typedef std::pair<int,int> pi;
typedef std::vector<int> vi;
typedef std::vector<std::string> vs;
typedef int64_t ll;
typedef uint64_t ull;
#define umap unordered_map
#define uset unordered_set
using namespace std;
using namespace __gnu_pbds;

#ifdef _WIN32
#define getchar_unlocked() _getchar_nolock()
#define _CRT_DISABLE_PERFCRIT_LOCKS
#endif
template<class L, class R> ostream& operator<<(ostream &os, map<L, R> P) { for(auto const &vv: P)os<<"("<<vv.first<<","<<vv.second<<")"; return os; }
template<class T> ostream& operator<<(ostream &os, set<T> V) { os<<"[";for(auto const &vv:V)os<<vv<<","; os<<"]"; return os; }
template<class T> ostream& operator<<(ostream &os, vector<T> V) { os<<"[";for(auto const &vv:V)os<<vv<<","; os<<"]"; return os; }
template<class L, class R> ostream& operator<<(ostream &os, pair<L, R> P) { os<<"("<<P.first<<","<<P.second<<")"; return os; }
inline int fstoi(const string &str){auto it=str.begin();bool neg=0;int num=0;if(*it=='-')neg=1;else num=*it-'0';++it;while(it<str.end()) num=num*10+(*it++-'0');if(neg)num*=-1;return num;}
inline void getch(char &x){while(x = getchar_unlocked(), x < 33){;}}
inline void getstr(string &str){str.clear(); char cur;while(cur=getchar_unlocked(),cur<33){;}while(cur>32){str+=cur;cur=getchar_unlocked();}}
template<typename T> inline bool sc(T &num){ bool neg=0; int c; num=0; while(c=getchar_unlocked(),c<33){if(c == EOF) return false;} if(c=='-'){ neg=1; c=getchar_unlocked(); } for(;c>47;c=getchar_unlocked()) num=num*10+c-48; if(neg) num*=-1; return true;}template<typename T, typename ...Args> inline void sc(T &num, Args &...args){ bool neg=0; int c; num=0; while(c=getchar_unlocked(),c<33){;} if(c=='-'){ neg=1; c=getchar_unlocked(); } for(;c>47;c=getchar_unlocked()) num=num*10+c-48; if(neg) num*=-1; sc(args...); }
template<typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; //s.find_by_order(), s.order_of_key() <- works like lower_bound
template<typename T> using ordered_map = tree<T, int, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define N 100

int vis[N];

int a[N][N];
int matchit; //so you dont have to clear vis
//INCREMENT BEFORE EACH SALVE OF AUGS
//2 Grupy muszą być oddzielone w oznaczeniu, tzn. np zajmuja miejsca 0..n-1 n..n+m-1
//Jesli nie potrzeba takiego speeda, można uniknąć warunku powużej jak wywalisz
//podówjny assign (tylko zostawiasz dla 2 grupy)
vi adj[N];

int n,m;
int ass[N];
bool aug(int v){ //is row,seg enough?
    if(vis[v] == matchit)
        return 0;
    vis[v] = matchit;
    FORR(i,adj[v]){
        if(!~ass[i] || aug(ass[i])){
            /* whatis(v) */
            /* whatis(i) */
            ass[i] = v;
            return 1;
        }
    }
    return 0;
}

int main(){
    ios_base::sync_with_stdio(0);cin.tie(0);
    sc(n,m);
    FOR(i,0,n)
        FOR(x,0,m)
            sc(a[i][x]);
    int b[n][m];
    int c[n][m];
    memset(b,-1,sizeof b);
    memset(c,-1,sizeof c);
    /* bool has[n][m]; // does xth col have item corresponding to ith row */
    /* memset(has,0,sizeof has); */
    int res = 0;
    bool usd[n][m];
    memset(usd,0,sizeof usd);
    FOR(c,0,m){
        memset(ass,-1,sizeof ass);
        map<int,int> el[n];
        FOR(i,0,n){
            adj[i].clear();
            FOR(x,0,m){
                if(usd[i][x]) continue;
                int wrow = (a[i][x]-1)/m;
                el[i][wrow] = x;
                adj[i].pb(wrow);
            }
        }
        FOR(i,0,n){
            ++matchit;
            res += aug(i);
            /* whatis("mtch") */
        }
        FOR(x,0,n){
            /* whatis(x) */
            /* whatis(ass[x]) */
            b[ass[x]][c] = a[ass[x]][el[ass[x]][x]];
            usd[ass[x]][el[ass[x]][x]] = 1;
        }
    }
    /* memset(ass,-1,sizeof ass); */
    /* FOR(i,0,n){ */
    /*     FOR(x,0,m){ */
    /*         int wrow = (a[i][x]-1)/m; */
    /*         FOR(j,0,m){ */
    /*             if(b[i][j] == -1 && !has[wrow][j]){ */
    /*                 has[wrow][j] = 1; */
    /*                 b[i][j] = a[i][x]; */
    /*                 break; */
    /*             } */
    /*         } */
    /* /1* FORR(i,b){ *1/ */
    /* /1*     FORR(x,i) *1/ */
    /* /1*         cout << x << ' '; *1/ */
    /* /1*     cout << '\n'; *1/ */
    /* /1* } *1/ */
    /* /1* cout << '\n'; *1/ */
    /*     } */
    /* } */
    /* int ret = 0; */
    /* bool lewl; */
    /* do{ */
    /*     lewl = 0; */
    /* FOR(i,0,n){ */
    /*     FOR(x,0,m){ */
    /* /1* for(int i = n-1; i >= 0; --i){ *1/ */
    /* /1*     for(int x = m-1; x >= 0; --x){ *1/ */
    /*         /1* memset(vis,0,sizeof vis); *1/ */
    /*         ++matchit; */
    /*         /1* int wrow = (a[i][x]-1)/m; *1/ */
    /*         /1* whatis(i) *1/ */
    /*         /1* whatis(x) *1/ */
    /*         int df = -ret; */
    /*         /1* ret += aug(i,wrow); *1/ */
    /*         ret += aug(i,x); */
    /*         df += ret; */
    /*         lewl |= df; */
    /*         /1* whatis(df) *1/ */
    /*     } */
    /* } */
    /*     /1* memset(ass,-1,sizeof ass); *1/ */
    /* FOR(i,0,n){ */
    /*     FOR(x,0,m){ */
    /*         /1* if(a[i][x] == 7){ *1/ */
    /*         /1*     whatis(i) *1/ */
    /*         /1*     whatis(a[i][x]) *1/ */
    /*         /1*     whatis(ass[i][x]) *1/ */
    /*         int wrow = (a[i][x]-1)/m; */
    /*         /1* } *1/ */
    /*         if(ass[i][x] != -1){ */
    /*             /1* b[i][ass[i][x]] = a[i][x]; *1/ */
    /*         } */
    /*         /1* cout << ass[i][x] << ' '; *1/ */
    /*     } */
    /* } */
    /*     }while(lewl); */
    /* for(int i = n-1; i >= 0; --i){ */
    /*     for(int x = m-1; x >= 0; --x){ */
    /*         if(~ass[i][x]) continue; */
    /*         /1* memset(vis,0,sizeof vis); *1/ */
    /*         ++matchit; */
    /*         /1* int wrow = (a[i][x]-1)/m; *1/ */
    /*         /1* whatis(i) *1/ */
    /*         /1* whatis(x) *1/ */
    /*         int df = -ret; */
    /*         /1* ret += aug(i,wrow); *1/ */
    /*         ret += aug(i,x); */
    /*         df += ret; */
    /*         /1* whatis(df) *1/ */
    /*     } */
    /* } */
    /* FOR(i,0,n){ */
    /*     FOR(x,0,m){ */
            /* if(a[i][x] == 7){ */
            /*     whatis(i) */
            /*     whatis(a[i][x]) */
            /*     whatis(ass[i][x]) */
            /* } */
            /* if(ass[i][x] != -1){ */
            /*     b[i][ass[i][x]] = a[i][x]; */
            /* } */
            /* else{ */
            /*     whatis(a[i][x]) */
            /* } */
            /* /1* cout << ass[i][x] << ' '; *1/ */
        /* } */
    /* } */
    int col[m][n];
    FOR(i,0,n){
        FOR(x,0,m){
    /* FOR(i,0,n){ */
    /*     FOR(x,0,m){ */
            /* whatis(i) */
            /* whatis(x) */
            /* cout << ass[i][x] << ' '; */
            /* assert(~b[i][x]); */
            cout << b[i][x] << ' ';
            col[x][i] = b[i][x];
        }
        cout << '\n';
    }
    FOR(i,0,m)
        sort(col[i],col[i]+n);
    FOR(i,0,m)
        FOR(x,0,n)
            c[x][i] = col[i][x];
    /* memset(assrow,-1,sizeof assrow); */
    /* memset(assseg,-1,sizeof assrow); */
    /* memset(ass,-1,sizeof ass); */
    /* whatis(ret) */
    /* FORR(i,b){ */
    /*     FORR(x,i) */
    /*         cout << x << ' '; */
    /*     cout << '\n'; */
    /* } */
    FOR(i,0,n){
        FOR(x,0,m){
            /* cout << ass[i][x] << ' '; */
            /* assert(~b[i][x]); */
            cout << c[i][x] << ' ';
            /* col[x][i] = b[i][x]; */
        }
        cout << '\n';
    }
}



Measured memory_bytes: 1024