#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (int)n; i++)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pi;
typedef pair<pi, pi> pp;
typedef pair<ll, ll> pl;
const double EPS = 1e-9;
const ll MOD = 1000000007;
const int inf = 1 << 30;
const ll linf = 1LL << 60;

struct edge{int to, cap, rev; };

struct DINIC {

    vector<edge> G[300];
    int level[300];
    int iter[300];

    void init(int _N) {
        rep(i,_N) G[i].clear();
    }

    void add_edge(int from, int to, int cap){
        G[from].push_back((edge){to, cap, G[to].size()});
        G[to].push_back((edge){from, 0, G[from].size()-1});
    }

    void bfs(int s){
        memset(level,-1,sizeof(level));
        queue<int> que;
        level[s] = 0;
        que.push(s);
        while(!que.empty()){
            int v = que.front(); que.pop();
            for(int i = 0; i < G[v].size(); i++){
                edge &e = G[v][i];
                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 < 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 max_flow(int s, int t){
        int flow = 0;
        for(;;){
            bfs(s);
            if(level[t] < 0) return flow;
            memset(iter,0, sizeof(iter));
            int f;
            while((f = dfs(s, t, inf)) > 0){
                flow += f;
            }
        }
    }
} din;

int n, m;
int A[100][100];
const int M = 100;

int main() {
    cin >> n >> m;
    rep(i,n) rep(j,m) cin >> A[i][j];
    rep(i,n) rep(j,m) A[i][j]--;
    rep(i,m) {
        din.init(2*M+2);
        rep(j,n) din.add_edge(2*M, j, 1);
        rep(j,n) din.add_edge(j+M,2*M+1, 1);
        for (int j = i; j < m; j++) {
            rep(k,n) {
                din.add_edge(A[k][j]/m, k+M, 1);
            }
        }
        din.max_flow(2*M, 2*M+1);
        rep(j,n) {
            for (edge& e : din.G[j]) {
                if (e.cap == 0 && e.to != 2*M) {
                    for (int k = i; k < m; k++) {
                        if (A[e.to-M][k]/m == j) {
                            swap(A[e.to-M][k], A[e.to-M][i]);
                            break;
                        }
                    }
                }
            }
        }
    }
    rep(i,n) {
        rep(j,m) {
            cout << A[i][j]+1;
            if (j == m-1) cout << endl;
            else cout << " ";
        }
    }
    vector<int> v;
    rep(i,m) {
        v.clear();
        rep(j,n) v.push_back(A[j][i]);
        sort(v.begin(), v.end());
        rep(j,n) A[j][i] = v[j];
    }
    rep(i,n) {
        rep(j,m) {
            cout << A[i][j]+1;
            if (j == m-1) cout << endl;
            else cout << " ";
        }
    }
}

Measured memory_bytes: 768