//#pragma GCC optimize("Ofast")
//#pragma GCC target("avx")
//#undef LOCAL
#include <bits/stdc++.h>

using namespace std;
using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;
constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n - 1); }
template <class T> using V = vector<T>;
template <class T> using VV = V<V<T>>;

#ifdef LOCAL
struct PrettyOS {
    ostream& os;
    bool first;
    template <class T> auto operator<<(T&& x) {
        if (!first) os << ", ";
        first = false;
        os << x;
        return *this;
    }
};
template <class... T> void dbg0(T&&... t) {
    (PrettyOS{cerr, true} << ... << t);
}
#define dbg(...)                                            \
    do {                                                    \
        cerr << __LINE__ << " : " << #__VA_ARGS__ << " = "; \
        dbg0(__VA_ARGS__);                                  \
        cerr << endl;                                       \
    } while (false);
#else
#define dbg(...)
#endif

template <class T, class U>
ostream& operator<<(ostream& os, const pair<T, U>& p) {
    return os << "P(" << p.first << ", " << p.second << ")";
}

template <class T> ostream& operator<<(ostream& os, const V<T>& v) {
    os << "[";
    for (auto d : v) os << d << ", ";
    return os << "]";
}

/*
struct E {
    int to, rev, cap;
};
VV<E> g;
auto add_edge = [&](int from, int to, int cap) {
    g[from].push_back(E{to, int(g[to].size()), cap});
    g[to].push_back(E{from, int(g[from].size())-1, 0});
};
*/

template<class C>
struct MaxFlow {
    C flow;
    V<char> dual;  // false: S-side true: T-side
};

template<class C, class E>
struct MFExec {
    static constexpr C INF = numeric_limits<C>::max();
    C eps;
    VV<E>& g;
    int s, t;
    V<int> level, iter;

    C dfs(int v, C f) {
        if (v == t) return f;
        C res = 0;
        for (int& i = iter[v]; i < int(g[v].size()); i++) {
            E& e = g[v][i];
            if (e.cap <= eps || level[v] >= level[e.to]) continue;
            C d = dfs(e.to, min(f, e.cap));
            e.cap -= d;
            g[e.to][e.rev].cap += d;
            res += d;
            f -= d;
            if (f == 0) break;
        }
        return res;
    }

    MaxFlow<C> info;
    MFExec(VV<E>& _g, int _s, int _t, C _eps)
        : eps(_eps), g(_g), s(_s), t(_t) {
        int N = int(g.size());

        C& flow = (info.flow = 0);
        while (true) {
            queue<int> que;
            level = V<int>(N, -1);
            level[s] = 0;
            que.push(s);
            while (!que.empty()) {
                int v = que.front(); que.pop();
                for (E e: g[v]) {
                    if (e.cap <= eps || level[e.to] >= 0) continue;
                    level[e.to] = level[v] + 1;
                    que.push(e.to);
                }
            }
            if (level[t] == -1) break;
            iter = V<int>(N, 0);
            while (true) {
                C f = dfs(s, INF);
                if (!f) break;
                flow += f;
            }
        }
        for (int i = 0; i < N; i++) info.dual.push_back(level[i] == -1);
    }
};

template<class C, class E>
MaxFlow<C> get_mf(VV<E>& g, int s, int t, C eps) {
    return MFExec<C, E>(g, s, t, eps).info;
}

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


void pr_arr(VV<int> a) {
    int n = int(a.size()), m = int(a[0].size());
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            cout << a[i][j] + 1 << " ";
        }
        cout << endl;
    }
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(20);

    int n, m;
    cin >> n >> m;

    VV<int> a(n, V<int>(m));
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            cin >> a[i][j]; a[i][j]--;
        }
    }


    for (int ph = 0; ph < m; ph++) {
        VV<E> g(2 + 2 * n);
        int sv = 2 * n, tv = sv + 1;
        auto add_edge = [&](int from, int to, int cap) {
            g[from].push_back(E{to, int(g[to].size()), cap});
            g[to].push_back(E{from, int(g[from].size())-1, 0});
        };
        for (int i = 0; i < n; i++) {
            add_edge(sv, i, 1);
            add_edge(n + i, tv, 1);
        }

        for (int i = 0; i < n; i++) {
            for (int j = ph; j < m; j++) {
                int r = a[i][j] / m;
                dbg(i, r);
                add_edge(i, n + r, 1);
            }
        }
        auto mf = get_mf(g, sv, tv, 0);
        dbg(mf.flow);
        assert(mf.flow == n);

        for (int i = 0; i < n; i++) {
            int r = -1;
            for (auto e: g[i]) {
                if (n <= e.to && e.cap == 0) {
                    r = e.to - n;
                    break;
                }
            }
            assert(r != -1);
            for (int j = ph; j < m; j++) {
                if (a[i][j] / m != r) continue;
                swap(a[i][ph], a[i][j]);
                break;
            }
        }

        //pr_arr(a);
    }

    pr_arr(a);

    for (int j = 0; j < m; j++) {
        V<int> v(n);
        for (int i = 0; i < n; i++) {
            v[i] = a[i][j];
        }
        sort(v.begin(), v.end());
        for (int i = 0; i < n; i++) {
            a[i][j] = v[i];
        }
    }
    pr_arr(a);
    return 0;
}


Measured memory_bytes: 712