#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <climits>
#include <cmath>
#include <complex>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <cstdint>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
#define MP make_pair
#define PB push_back
#define inf 1000000007
#define mod 1000000007
#define rep(i,n) for(int i = 0; i < (int)(n); ++i)
#define all(x) (x).begin(),(x).end()

template<typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val){
    std::fill( (T*)array, (T*)(array+N), val );
}
 
template<class T> inline bool chmax(T &a, T b){
    if(a<b){
        a = b;
        return true;
    }
    return false;
}

template<class T> inline bool chmin(T &a, T b){
    if(a>b){
        a = b;
        return true;
    }
    return false;
}

class BM {
private:
    struct edge {
        int to,cap,rev;
    };
    int U,V;
    vector<vector<edge> > G;
    vector<int> level;
    vector<int> iter;

public:
    BM(int u,int v) : U(u), V(v), G(U+V+2){
        for(int i = 0; i < U; i++){
            init_edge(0,i+1);
        }
        for(int i = 0; i < V; i++){
            init_edge(U+i+1,U+V+1);
        }
    }
    void init_edge(int from,int to) {
        G[from].push_back((edge){to,1,(int)G[to].size()});
    	G[to].push_back((edge){from,0,(int)G[from].size()-1});
    }
    void add_edge(int from,int to) {
        from += 1,to += U+1;
    	G[from].push_back((edge){to,1,(int)G[to].size()});
    	G[to].push_back((edge){from,0,(int)G[from].size()-1});
    }
    void bfs(int s) {
        fill(level.begin(),level.end(),-1);
    	queue<int> que;
    	level[s] = 0;
    	que.push(s);
    	while(!que.empty()){
            int v = que.front();
            que.pop();
            for(auto& e : G[v]){
                if(e.cap > 0 && level[e.to] < 0){
                    level[e.to] = level[v] + 1;
                    que.push(e.to);
                }
            }
        }
    }
    int dfs(int v,int t,int f) {
        if(v==t){
            return f;
        }
        for(int &i = iter[v];i<(int)G[v].size();i++){
            edge &e = G[v][i];
            if(e.cap > 0 && level[v] < level[e.to]){
                int d = dfs(e.to,t,min(f,e.cap));
                if(d>0){
                    e.cap -= d;
                    G[e.to][e.rev].cap += d;
                    return d;
                }
            }
        }
        return 0;
    }
    int solve() {
        level.resize(U+V+2), iter.resize(U+V+2);
        int flow = 0;
        int s=0,t=U+V+1;
        for(;;){
            bfs(s);
            if(level[t] < 0) return flow;
            fill(iter.begin(),iter.end(),0);
            int f;
            while((f=dfs(s,t,numeric_limits<int>::max())) > 0){
                flow += f;
            }
        }
    }
    vector<int> allocate() {
        vector<int> res(U, -1);
        for(int i = 0; i < U; i++){
            for(auto& e : G[i+1]){
                if(e.cap == 0 && e.to != 0){
                    res[i] = e.to-U-1;
                    break;
                }
            }
        }
        return res;
    }
};


int main(){
    int n,m;
    cin >> n >> m;
    vector<vector<int> > a(n,vector<int>(m));
    rep(i,n){
        rep(j,m){
            cin >> a[i][j];
        }
    }
    vector<vector<bool> > used(n,vector<bool>(m));
    vector<vector<int> > b = a;
    rep(k,m){
        BM bm(n,n);
        rep(i,n){
            rep(j,m){
                if(!used[i][j]){
                    bm.add_edge(i,(a[i][j]-1)/m);
                }
            }
        }
        bm.solve();
        vector<int> res = bm.allocate();
        rep(i,n){
            int K = res[i];
            rep(j,m){
                if(!used[i][j]){
                    if((a[i][j]-1)/m==K){
                        used[i][j] = true;
                        b[i][k] = a[i][j];
                        break; 
                    }
                }
            }
        }
    }
    rep(i,n){
        rep(j,m){
            cout << b[i][j] << " ";
        }
        cout << "\n";
    }
    rep(i,n){
        rep(j,m){
            rep(k,n){
                if((b[k][j]-1)/m == i){
                    cout << b[k][j] << " ";
                }
            }
        }
        cout << "\n";
    }
    return 0;
}

Measured memory_bytes: 752