//#pragma GCC optimize("O3")
#ifdef ONPC
#include "bits/stdc++.h"
#else
#include <bits/stdc++.h>
#endif

using namespace std;

//defines
typedef long long ll;
typedef long double ld;
#define TIME clock() * 1.0 / CLOCKS_PER_SEC
#define prev _prev
#define y0 y00
#define y1 y11

//permanent constants
const ld pi = acos(-1.0);
const int day[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int digarr[10] = {6, 2, 5, 5, 4, 5, 6, 3, 7, 6};
const int dx[4] = {0, 1, 0, -1};
const int dy[4] = {1, 0, -1, 0};
const int dxo[8] = {-1, -1, -1, 0, 1, 1, 1, 0};
const int dyo[8] = {-1, 0, 1, 1, 1, 0, -1, -1};
const int dxk[8] = {-1, -2, -1, 2, -2, 1, 1, 2};
const int dyk[8] = {-2, -1, 2, -1, 1, -2, 2, 1};
const int alf = 26;
const int dig = 10;
const int two = 2;
const int th = 3;
const ll prost = 239;
const ll bt = 30;
const ld eps = 1e-8;
const ll INF = (ll)(1e18 + 239);
const ll BIG = (ll)(1e9 + 239);
const int MOD = 998244353;
const ll MOD2 = (ll)MOD * (ll)MOD;

//random
mt19937 rnd(239); //(chrono::high_resolution_clock::now().time_since_epoch().count());

//constants
const int M = (int)(2e5 + 239);
const int N = (int)(2e3 + 239);
const int L = 18;
const int T = (1 << 19);
const int B = (int)sqrt(M);
const int X = 110;

int n, m, a[X][X];

int pa[X], pb[X];
vector<int> v[X];
bool used[X];

bool dfs(int p)
{
    used[p] = true;
    for (int i : v[p])
    {
        if (pb[i] == -1 || (!used[pb[i]] && dfs(pb[i])))
        {
            pa[p] = i;
            pb[i] = p;
            return true;
        }
    }
    return false;
}

void kuhn()
{
    memset(pa, -1, sizeof(pa));
    memset(pb, -1, sizeof(pb));
    for (int i = 0; i < n; i++)
    {
        memset(used, 0, sizeof(used));
        if (dfs(i))
            continue;
    }
}

vector<int> pos[X];
bool can[X][X];
int b[X][X];

int32_t main()
{
#ifdef ONPC
    freopen("input.txt", "r", stdin);
#endif
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin >> n >> m;
    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++)
        {
            cin >> a[i][j];
            a[i][j]--;
        }
    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++)
            can[i][j] = true;
    for (int i = 0; i < m; i++)
    {
        for (int x = 0; x < n; x++)
            v[x].clear();
        for (int x = 0; x < n; x++)
            for (int y = 0; y < m; y++)
                if (can[x][y])
                    v[x].push_back(a[x][y] / m);
        kuhn();
        for (int x = 0; x < n; x++)
        {
            int need = pa[x];
            for (int y = 0; y < m; y++)
                if (can[x][y] && need == (a[x][y] / m))
                {
                    can[x][y] = false;
                    pos[x].push_back(y);
                    break;
                }
        }
    }
    for (int i = 0; i < n; i++)
    {
        for (int j : pos[i])
            cout << a[i][j] + 1 << " ";
        cout << "\n";
    }
    for (int j = 0; j < m; j++)
    {
        vector<int> cur;
        for (int i = 0; i < n; i++)
            cur.push_back(a[i][pos[i][j]]);
        sort(cur.begin(), cur.end());
        for (int i = 0; i < n; i++)
            b[i][j] = cur[i];
    }
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
            cout << b[i][j] + 1 << " ";
        cout << "\n";
    }
    return 0;
}

Measured memory_bytes: 640