#include <bits/stdc++.h>
using namespace std;
class Flow {
public:
    static constexpr int INF = 1E9;
    int n;
    vector<pair<int, int>> e;
    vector<vector<int>> g;
    vector<int> cur, h;
    bool bfs(int s, int t) {
        h.assign(n, -1);
        queue<int> que;
        h[s] = 0;
        que.push(s);
        while (!que.empty()) {
            int u = que.front();
            que.pop();
            for (int i : g[u]) {
                int v, c;
                tie(v, c) = e[i];
                if (c > 0 && h[v] == -1) {
                    h[v] = h[u] + 1;
                    if (v == t)
                        return true;
                    que.push(v);
                }
            }
        }
        return false;
    }
    int dfs(int u, int t, int f) {
        if (u == t)
            return f;
        int r = f;
        for (int &i = cur[u]; i < g[u].size(); ++i) {
            int j = g[u][i];
            int v, c;
            tie(v, c) = e[j];
            if (c > 0 && h[v] == h[u] + 1) {
                int a = dfs(v, t, min(r, c));
                e[j].second -= a;
                e[j ^ 1].second += a;
                r -= a;
                if (r == 0)
                    return f;
            }
        }
        return f - r;
    }
    Flow(int n) : n(n) {g.resize(n);}
    void addEdge(int u, int v, int c) {
        g[u].push_back(e.size());
        e.emplace_back(v, c);
        g[v].push_back(e.size());
        e.emplace_back(u, 0);
    }
    int maxFlow(int s, int t) {
        int ans = 0;
        while (bfs(s, t)) {
            cur.assign(n, 0);
            ans += dfs(s, t, INF);
        }
        return ans;
    }
};
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, m;
    cin >> n >> m;
    vector<vector<int>> a(n);
    for (int i = 0; i < n; ++i) {
        a[i].resize(m);
        for (int j = 0; j < m; ++j) {
            cin >> a[i][j];
            --a[i][j];
        }
    }
    for (int c = 0; c < m; ++c) {
        Flow f(2 * n + 2);
        const int s = 2 * n, t = s + 1;
        for (int i = 0; i < n; ++i) {
            f.addEdge(s, i, 1);
            f.addEdge(i + n, t, 1);
        }
        for (int i = 0; i < n; ++i)
            for (int j = c; j < m; ++j)
                f.addEdge(i, a[i][j] / m + n, 1);
        f.maxFlow(s, t);
        for (int i = 0; i < n; ++i) {
            int x;
            for (int e : f.g[i]) {
                int v, cap;
                tie(v, cap) = f.e[e];
                if (cap == 0) {
                    x = v - n;
                    break;
                }
            }
            for (int j = c; j < m; ++j) {
                if (a[i][j] / m == x) {
                    swap(a[i][j], a[i][c]);
                    break;
                }
            }
        }
    }
    for (int i = 0; i < n; ++i)
        for (int j = 0; j < m; ++j)
            cout << a[i][j] + 1 << " \n"[j == m - 1];
    for (int i = 0; i < m; ++i) {
        vector<int> x(n);
        for (int j = 0; j < n; ++j)
            x[a[j][i] / m] = a[j][i];
        for (int j = 0; j < n; ++j)
            a[j][i] = x[j];
    }
    for (int i = 0; i < n; ++i)
        for (int j = 0; j < m; ++j)
            cout << a[i][j] + 1 << " \n"[j == m - 1];
    return 0;
}

Measured memory_bytes: 784