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

const int M = 105, N = 105;

int m, n, it, a[M][N], b[M][N], c[M][N], mat[M], chk[M];
bool use[M][N];
vector<int> adj[M];

void init()
{
    it = 1;
    for (int i = 1; i <= m; i++)
    {
        adj[i].clear();
        mat[i] = chk[i] = 0;
    }
}

bool DFS(int u)
{
    chk[u] = it;
    for (int &v : adj[u])
        if (mat[v] == 0)
        {
            mat[v] = u;
            return true;
        }
    for (int &v : adj[u])
        if (chk[mat[v]] < it && DFS(mat[v]))
        {
            mat[v] = u;
            return true;
        }
    return false;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cin >> m >> n;
    for (int i = 1; i <= m; i++)
        for (int j = 1; j <= n; j++)
        {
            cin >> a[i][j];
            a[i][j]--;
        }
    for (int r = 1; r <= n; r++)
    {
        init();
        for (int i = 1; i <= m; i++)
            for (int j = 1; j <= n; j++)
                if (!use[i][j])
                    adj[i].push_back(a[i][j] / n + 1);
        for (int i = 1; i <= m; i++)
            if (DFS(i))
                ++it;
        for (int i = 1; i <= m; i++)
        {
            int row = mat[i];
            for (int j = 1; j <= n; j++)
                if (!use[row][j] && a[row][j] / n + 1 == i)
                {
                    b[row][r] = c[i][r] = a[row][j];
                    use[row][j] = true;
                    break;
                }
        }
    }
    for (int i = 1; i <= m; i++, cout << '\n')
        for (int j = 1; j <= n; j++)
            cout << b[i][j] + 1 << " ";
    for (int i = 1; i <= m; i++, cout << '\n')
        for (int j = 1; j <= n; j++)
            cout << c[i][j] + 1 << " ";
}
