#include<bits/stdc++.h>
using namespace std;

#define int long long
#define double long double
#define rep(i, n) for (int i = 0; i < (n); i++)
#define reps(i, n) for (int i = 1; i <= (n); i++)
#define all(x) (x).begin(), (x).end()
#define uniq(x) (x).erase(unique(all(x)), (x).end())
#define bit(n) (1LL << (n))
#define dump(x) cerr << #x " = " << (x) << endl
using vint=vector<int>;
using vvint=vector<vint>;
using pint=pair<int, int>;
using vpint=vector<pint>;
template<typename T> using priority_queue_rev=priority_queue<T, vector<T>, greater<T>>;
constexpr double pi = 3.1415926535897932384626433832795028;
constexpr int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
constexpr int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int gcd(int a, int b) {
    while (b) { swap(a %= b, b); }
    return a;
}
int lcm(int a, int b) { return a / gcd(a, b) * b; }
template<typename T1, typename T2> bool chmax(T1 &a, const T2 &b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}
template<typename T1, typename T2> bool chmin(T1 &a, const T2 &b) {
    if (b < a) {
        a = b;
        return true;
    }
    return false;
}
template<typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &rhs) {
    os << "(" << rhs.first << ", " << rhs.second << ")";
    return os;
}
template<typename T> ostream &operator<<(ostream &os, const vector<T> &rhs) {
    os << "{";
    for (auto itr = rhs.begin(); itr != rhs.end(); itr++) {
        os << *itr << (next(itr) != rhs.end() ? ", " : "");
    }
    os << "}";
    return os;
}
struct Setup {
    static constexpr int PREC = 20;
    Setup() {
        cout << fixed << setprecision(PREC);
        cerr << fixed << setprecision(PREC);
    };
} setup;

struct HopcroftKarp {
    vector<vector<int>> graph;
    vector<int> dist, match;
    vector<bool> used, vv;

    HopcroftKarp(int n, int m) : graph(n), match(m, -1), used(n) {}

    void addEdge(int u, int v) {
        graph[u].push_back(v);
    }

    void bfs() {
        dist.assign(graph.size(), -1);
        queue<int> que;
        for (int i = 0; i < graph.size(); i++) {
            if (!used[i]) {
                que.emplace(i);
                dist[i] = 0;
            }
        }

        while (!que.empty()) {
            int a = que.front();
            que.pop();
            for (auto &b : graph[a]) {
                int c = match[b];
                if (c >= 0 && dist[c] == -1) {
                    dist[c] = dist[a] + 1;
                    que.emplace(c);
                }
            }
        }
    }

    bool dfs(int a) {
        vv[a] = true;
        for (auto &b : graph[a]) {
            int c = match[b];
            if (c < 0 || (!vv[c] && dist[c] == dist[a] + 1 && dfs(c))) {
                match[b] = a;
                used[a] = true;
                return (true);
            }
        }
        return (false);
    }

    vpint getMaxMatching() {
        while (true) {
            bfs();
            vv.assign(graph.size(), false);
            int flow = 0;
            for (int i = 0; i < graph.size(); i++) {
                if (!used[i] && dfs(i)) { ++flow; }
            }
            if (flow == 0) { break; }
        }
        vpint ret;
        for (int i = 0; i < match.size(); i++) {
            ret.push_back({match[i], i});
        }
        return ret;
    }

    void output() {
        for (int i = 0; i < match.size(); i++) {
            if (~match[i]) {
                cout << match[i] << "-" << i << endl;
            }
        }
    }
};

int N, M;
int A[110][110];
signed main() {
    cin >> N >> M;
    rep(i, N)
        rep(j, M) {
            cin >> A[i][j];
            A[i][j]--;
        }
    rep(i, M) {
        HopcroftKarp bm(N, N);
        vvint alc(N, vint(N, -1));
        rep(j, N) {
            for (int k = i; k < M; k++) {
                int c = A[j][k] / M;
                if (alc[j][c] < 0) {
                    alc[j][c] = k;
                    bm.addEdge(j, c);
                }
            }
        }
        vpint m = bm.getMaxMatching();
        rep(j, N) {
            swap(A[m[j].first][i], A[m[j].first][alc[m[j].first][m[j].second]]);
        }
    }
    rep(i, N) {
        rep(j, M) { cout << A[i][j] + 1 << " "; }
        cout << endl;
    }
    rep(i, M) {
        rep(j, N) {
            for (int k = j + 1; k < N; k++) {
                if (A[j][i] > A[k][i]) { swap(A[j][i], A[k][i]); }
            }
        }
    }
    rep(i, N) {
        rep(j, M) { cout << A[i][j] + 1 << " "; }
        cout << endl;
    }
}


Measured memory_bytes: 588