#include <bits/stdc++.h>
using i64 = long long;

struct Dinic {
    struct Edge {
        int to, rev, cap, id;
        Edge(int t, int c, int r, int i) : to(t), rev(r), cap(c), id(i) {}
    };

    static constexpr int inf = std::numeric_limits<int>::max() / 2;
    std::vector<std::vector<Edge>> g;
    std::vector<int> level;
    std::vector<unsigned> iter;
    int size;

    void bfs(int s) {
        for (int i = 0; i < size; i++) level[i] = -1;
        level[s] = 0;

        std::deque<int> q { s };

        while (!q.empty()) {
            auto curr = q.front();
            q.pop_front();
            for (auto & e : g[curr]) {
                if (e.cap > 0 && level[e.to] < 0) {
                    level[e.to] = level[curr] + 1;
                    q.push_back(e.to);
                }
            }
        }
    }

    auto dfs(int v, int t, int f) {
        if (v == t) return f;

        for (unsigned i = 0; i < g[v].size(); i++) {
            iter[v] = i;
            auto &e = g[v][i];

            if (e.cap > 0 && level[v] < level[e.to]) {
                auto d = dfs(e.to, t, std::min(f, e.cap));
                if (d > 0) {
                    e.cap -= d;
                    g[e.to][e.rev].cap += d;
                    return d;
                }
            }
        }

        return 0;
    }

    explicit Dinic(int s) :
        g(s),
        level(s),
        iter(s),
        size(s) {}

    void add_edge(int from, int to, int cap, int id) {
        g[from].emplace_back(to, cap, g[to].size(), id);
        g[to].emplace_back(from, 0, g[from].size() - 1, id);
    }

    auto calc(int s, int t) {
        int flow = 0;
        while (true) {
            bfs(s);
            if (level[t] < 0) return flow;

            for (auto &el : iter) el = 0;

            auto f = dfs(s, t, inf);
            while (f > 0) {
                flow += f;
                f = dfs(s, t, inf);
            }
        }
    }
};

int main() {
    int n, m;
    std::cin >> n >> m;
    std::vector<std::pair<int, int>> v(n * m + 1);
    for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) {
        int a;
        std::cin >> a;
        v[a] = std::make_pair(i, j);
    }

    std::vector<std::vector<int>> sorted;
    for (int i = 0; i < m; i++) {
        Dinic d(2 * n + 2);
        int s = 2 * n, t = 2 * n + 1;
        for (int j = 0; j < n; j++) {
            d.add_edge(s, j, 1, 0);
            d.add_edge(j + n, t, 1, 0);
        }
        for (int j = 1; j <= n * m; j++) {
            if (v[j].first < 0) continue;
            d.add_edge(v[j].first, n + (j - 1) / m, 1, j);
        }

        d.calc(s, t);

        sorted.emplace_back();
        for (int i = 0; i < n; i++) {
            for (auto &e : d.g[i]) {
                if (e.cap) continue;
                sorted.back().push_back(e.id);
                v[e.id] .first = -1;
                break;
            }
        }
    }

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            std::cout << sorted[j][i] << " ";
        }
        std::cout << std::endl;
    }

    for (auto &e : sorted) std::sort(e.begin(), e.end());
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            std::cout << sorted[j][i] << " ";
        }
        std::cout << std::endl;
    }

    return 0;
}


Measured memory_bytes: 852