#include <bits/stdc++.h>

using namespace std;
using ll = long long;

#ifdef LOCAL
//#define TEST
#endif // LOCAL

bool dfs (int x, const vector<vector<vector<int>>> &cnt, vector<int> &match, vector<int> &rev_match, vector<char> &used)
{
    if (used[x])
        return false;
    used[x] = 1;

    for (int i = 0; i < int(cnt[x].size()); i++)
    if (!cnt[x][i].empty())
    if (match[i] == -1 || dfs(match[i], cnt, match, rev_match, used))
    {
        match[i] = x;
        rev_match[x] = i;
        return true;
    }

    return false;
}

void solve(istream &cin = std::cin, ostream &cout = std::cout)
{
    auto solve_test = [&] ()
    {
#ifndef TEST
        int n, m;
        if (!(cin >> n >> m))
            return false;

        vector<vector<int>> grid(n, vector<int>(m));
        for (auto &row : grid) for (auto &x : row)
            cin >> x;
#else
        const int n = 100;
        const int m = 100;
        vector<int> perm(n * m);
        iota(perm.begin(), perm.end(), 1);
        static mt19937 rng;
        shuffle(perm.begin(), perm.end(), rng);

        vector<vector<int>> grid(n, vector<int>(m));
        int ptr = 0;
        for (auto &row : grid) for (auto &x : row)
            x = perm[ptr++];
#endif

        vector<vector<char>> used(n, vector<char>(m));

        vector<vector<int>> b(n, vector<int>(m));
        auto c = b;

        vector<vector<vector<int>>> cnt(n, vector<vector<int>>(n));

        for (int i = 0; i < n; i++)
        for (int x : grid[i])
            cnt[i][(x - 1) / m].push_back(x);

        for (int j = 0; j < m; j++)
        {
            vector<int> match(n, -1), rev_match(n, -1);

            while (true)
            {
                vector<char> used(n);
                bool ok = false;

                for (int i = 0; i < n; i++)
                if (rev_match[i] == -1 && !used[i])
                    ok |= dfs(i, cnt, match, rev_match, used);

                if (!ok)
                    break;
            }

//            cerr << count(rev_match.begin(), rev_match.end(), -1) << endl;
//            cerr << "j = " << j << endl;
            assert(count(rev_match.begin(), rev_match.end(), -1) == 0);

            for (int i = 0; i < n; i++)
            {
                b[i][j] = cnt[i][rev_match[i]].back();
                cnt[i][rev_match[i]].pop_back();
            }
        }

        for (auto &row : b)
            assert(!count(row.begin(), row.end(), 0));

        for (int j = 0; j < m; j++)
        {
            vector<int> column(n);
            for (int i = 0; i < n; i++)
                column[i] = b[i][j];
            sort(column.begin(), column.end());
            for (int i = 0; i < n; i++)
                c[i][j] = column[i];
        }

        for (int it = 0; it < 2; it++)
        {
            const auto &matrix = (it == 0 ? b : c);
            for (const auto &row : matrix)
            {
                for (auto x : row)
                    cout << x << " ";
                cout << endl;
            }
        }

        return true;
    };

    while (solve_test());
}


int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);

	cout << fixed;

#ifdef LOCAL
	auto st = clock();

	ifstream fin("input.txt");

	solve(fin);

	cout << "clock: " << setprecision(4) << (clock() - st) / (double) CLOCKS_PER_SEC << endl;
#else
	solve();
#endif

	return 0;
}


Measured memory_bytes: 1024