Problem:
Score : 1100 points

Problem Statement

We have a grid with N rows and M columns of squares.
Each integer from 1 to NM is written in this grid once.
The number written in the square at the i-th row from the top and the j-th column from the left is A_{ij}.

You need to rearrange these numbers as follows:

First, for each of the N rows, rearrange the numbers written in it as you like.

Second, for each of the M columns, rearrange the numbers written in it as you like.

Finally, for each of the N rows, rearrange the numbers written in it as you like.

After rearranging the numbers, you want the number written in the square at the i-th row from the top and the j-th column from the left to be M\times (i-1)+j.
Construct one such way to rearrange the numbers. The constraints guarantee that it is always possible to achieve the objective.

Constraints

1 \leq N,M \leq 100

1 \leq A_{ij} \leq NM

A_{ij} are distinct.

Input

Input is given from Standard Input in the following format:

N M
A_{11} A_{12} ... A_{1M}
:
A_{N1} A_{N2} ... A_{NM}

Output

Print one way to rearrange the numbers in the following format:

B_{11} B_{12} ... B_{1M}
:
B_{N1} B_{N2} ... B_{NM}
C_{11} C_{12} ... C_{1M}
:
C_{N1} C_{N2} ... C_{NM}

Here B_{ij} is the number written in the square at the i-th row from the top and the j-th column from the left after Step 1, and C_{ij} is the number written in that square after Step 2.

Sample Input 1

3 2
2 6
4 3
1 5

Sample Output 1

2 6
4 3
5 1
2 1
4 3
5 6

Sample Input 2

3 4
1 4 7 10
2 5 8 11
3 6 9 12

Sample Output 2

1 4 7 10
5 8 11 2
9 12 3 6
1 4 3 2
5 8 7 6
9 12 11 10

Solution in C++14 (GCC 5.4.1) (judge verdict: Accepted)

/* be name khoda */

// #define long_enable
#include <iostream>
#include <algorithm>
#include <cstring>
#include <numeric>
#include <vector>
#include <fstream>
#include <set>
#include <map>
using namespace std;

#ifdef long_enable
typedef long long int ll;
#else
typedef int ll;
#endif

typedef pair<ll, ll> pii;
typedef pair<pii, ll> ppi;
typedef pair<ll, pii> pip;
typedef vector<ll> vi;
typedef vector<pii> vpii;

#define forifrom(i, s, n) for (ll i = s; i < n; ++i)
#define forirto(i, n, e) for (ll i = (n) - 1; i >= e; --i)
#define fori(i, n) forifrom (i, 0, n)
#define forir(i, n) forirto (i, n, 0)

#define F first
#define S second
#define pb push_back
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define smin(a, b) a = min(a, (b))
#define smax(a, b) a = max(a, (b))

#define debug(x) cout << #x << " -> " << (x) << endl
#define debug2(x, y) cout << #x << ' ' << #y << " -> " << (x) << ' ' << (y) << endl
#define debug3(x, y, z) cout << #x << ' ' << #y << ' ' << #z << " -> " << (x) << ' ' << (y) << ' ' << (z) << endl
#define debug4(x, y, z, t) cout << #x << ' ' << #y << ' ' << #z << ' ' << #t << " -> " << (x) << ' ' << (y) << ' ' << (z) << ' ' << (t) << endl
#define debuga(x, n) cout << #x << " -> "; fori (i1_da, n) { cout << (x)[i1_da] << ' '; } cout << endl
#define debugaa(x, n, m) cout << #x << " ->\n"; fori (i1_daa, n) { fori (i2_daa, m) { cout << (x)[i1_daa][i2_daa] << ' '; } cout << '\n'; } cout << endl

const ll MOD = 1000000007;
const ll INF = 2000000000;
const long long BIG = 1446803456761533460LL;

#define XL (x << 1)
#define XR (XL | 1)
#define MD ((l + r) >> 1)
#define Add(a, b) a = ((a) + (b)) % MOD
#define Mul(a, b) a = (1LL * (a) * (b)) % MOD

// -----------------------------------------------------------------------

const ll maxn = 110;

ll n, m, G[maxn][maxn];
vpii A[maxn];
ll vis[maxn], sat[maxn];
pii match[maxn];
ll pos[maxn * maxn];
ll B[maxn][maxn];

bool dfs(ll x) {
    vis[x] = 1;
    for (auto e : A[x]) {
        ll y = e.F;
        if (match[y].F == -1 || (!vis[match[y].F] && dfs(match[y].F))) {
            match[y] = {x, e.S};
            return true;
        }
    }
    return false;
}

int main() {
    ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);

    cin >> n >> m;
    fori (i, n) fori (j, m) cin >> G[i][j], --G[i][j];

    memset(pos, -1, sizeof pos);
    fori (t, m) {
        fori (i, n) A[i].clear();
        fori (i, n) {
            fori (j, m) {
                if (pos[G[i][j]] == -1) {
                    ll x = G[i][j] / m;
                    A[x].eb(i, G[i][j]);
                }
            }
        }
        fill(match, match + n, make_pair(-1, -1));
        memset(sat, 0, sizeof sat);
        bool flag = true;
        while (flag) {
            memset(vis, 0, sizeof vis);
            flag = false;
            fori (i, n) {
                if (!sat[i] && dfs(i)) {
                    sat[i] = 1;
                    flag = true;
                }
            }
        }
        fori (i, n) pos[match[i].S] = t;
    }
    fori (i, n) fori (j, m) {
        B[pos[G[i][j]]][i] = G[i][j];
    }
    fori (i, n) {
        fori (j, m) {
            cout << B[j][i] + 1 << ' ';
        }
        cout << '\n';
    }
    fori (i, m) {
        sort(B[i], B[i] + n);
    }
    fori (i, n) {
        fori (j, m) {
            cout << B[j][i] + 1 << ' ';
        }
        cout << '\n';
    }

}