#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 BipartiteMatching {
    int n;
    std::vector<std::vector<int>> G;
    std::vector<int> match, used;
    int timestamp;
    BipartiteMatching(int n) : n(n), G(n), match(n), used(n) {}
    void addEdge(int u, int v) {
        G[u].push_back(v);
        G[v].push_back(u);
    }
    bool dfs(int v) {
        used[v] = timestamp;
        for (int i = 0; i < G[v].size(); i++) {
            int u = G[v][i], w = match[u];
            if (w < 0 || used[w] != timestamp && dfs(w)) {
                match[v] = u;
                match[u] = v;
                return true;
            }
        }
        return false;
    }
    std::vector<std::pair<int, int>> getMaxMatching() {
        fill(match.begin(), match.end(), -1);
        fill(used.begin(), used.end(), 0);
        timestamp = 0;
        for (int i = 0; i < n; i++) {
            if (match[i] < 0) {
                timestamp++;
                dfs(i);
            }
        }
        std::vector<std::pair<int, int>> ret;
        for (int i = 0; i < n; i++) {
            if (i < match[i]) { ret.push_back({i, match[i]}); }
        }
        return ret;
    }
};

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) {
        BipartiteMatching 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 + N);
                }
            }
        }
        vpint m = bm.getMaxMatching();
        rep(j, N) {
            swap(A[m[j].first][i], A[m[j].first][alc[m[j].first][m[j].second - N]]);
        }
    }
    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: 708