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

#define endl '\n'

const int maxn = 102;

int a[maxn][maxn];
int cnt[maxn][maxn];
vector<int> vt[maxn][maxn];


#define HK HopcroftKarp
namespace HopcroftKarp {
    const int maxv = 1e3 + 5;
    const int maxe = 1e6 + 5;
    int nx, ny, E, nmatches;
    int adj[maxe], nxt[maxe];
    int lst[maxv], ptr[maxv], lev[maxv], que[maxv], matx[maxv], maty[maxv];
    void init(int _nx, int _ny) {
        nx = _nx, ny = _ny;
        E = nmatches = 0, fill_n(lst, nx, -1);
        fill_n(matx, nx, -1), fill_n(maty, ny, -1);
    }
    void add(int x, int y) {
        adj[E] = y, nxt[E] = lst[x], lst[x] = E++;
    }
    int bfs() {
        int qh = 0, qe = 0;
        for (int x = 0; x < nx; x++) {
            if (~matx[x]) {
                lev[x] = 0;
            }
            else {
                lev[x] = 1;
                que[qe++] = x;
            }
        }
        int res = 0;
        while (qh < qe) {
            int x = que[qh++];
            for (int e = lst[x]; ~e; e = nxt[e]) {
                int y = adj[e];
                if (!~maty[y]) {
                    res = 1;
                }
                else if (!lev[maty[y]]) {
                    lev[maty[y]] = lev[x] + 1;
                    que[qe++] = maty[y];
                }
            }
        }
        return res;
    }
    int dfs(int x) {
        for (int& e = ptr[x]; ~e; e = nxt[e]) {
            int y = adj[e];
            if (!~maty[y] || (lev[maty[y]] == lev[x] + 1 && dfs(maty[y]))) {
                matx[x] = y;
                maty[y] = x;
                return 1;
            }
        }
        return 0;
    }
    int maxmat() {
        while (bfs()) {
            for (int x = 0; x < nx; x++) {
                ptr[x] = lst[x];
            }
            for (int x = 0; x < nx; x++) {
                if (!~matx[x]) {
                    nmatches += dfs(x);
                }
            }
        }
        return nmatches;
    }
    int vis[2][maxv];
    void dfs(int u, int r) {
        vis[r][u] = 1;
        if (!r) {
            for (int e = lst[u]; ~e; e = nxt[e]) {
                int v = adj[e];
                if (matx[u] != v && !vis[!r][v]) {
                    dfs(v, !r);
                }
            }
        }
        else {
            int v = maty[u];
            if (~v && !vis[!r][v]) {
                dfs(v, !r);
            }
        }
    }
    vector<int> mincover() {
        maxmat();
        vector<int> res;
        fill_n(vis[0], nx, 0), fill_n(vis[1], ny, 0);
        for (int x = 0; x < nx; x++) {
            if (!~matx[x] && !vis[0][x]) {
                dfs(x, 0);
            }
        }
        for (int x = 0; x < nx; x++) {
            if (!vis[0][x]) {
                res.push_back(x);
            }
        }
        for (int y = 0; y < ny; y++) {
            if (vis[1][y]) {
                res.push_back(nx + y);
            }
        }
        return res;
    }
    vector<int> maxind() {
        maxmat();
        vector<int> res;
        fill_n(vis[0], nx, 0), fill_n(vis[1], ny, 0);
        for (int x = 0; x < nx; x++) {
            if (!~matx[x] && !vis[0][x]) {
                dfs(x, 0);
            }
        }
        for (int x = 0; x < nx; x++) {
            if (vis[0][x]) {
                res.push_back(x);
            }
        }
        for (int y = 0; y < ny; y++) {
            if (!vis[1][y]) {
                res.push_back(nx + y);
            }
        }
        return res;
    }
}


int main() {
	//freopen("inp.txt", "r", stdin);
	ios::sync_with_stdio(false);
	cin.tie(0);
	int n = 0;
	int m = 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] --;
			cnt[i][a[i][j] / m] ++;
			vt[i][a[i][j] / m].push_back(a[i][j]);
		}
	}	
	for(int col = 0; col < m; col ++) {
		HK::init(n, n);
		for(int i=0;i<n;i++) {
			for(int j=0;j<n;j++) {
				if (cnt[i][j]) {
					HK::add(i, j);
				}
			}
		}
		
		int t = HK::maxmat();
		assert(t == n);
		for(int i=0;i<n;i++) {
			int v = HK::matx[i];
			cnt[i][HK::matx[i]] --;
			a[i][col] = vt[i][v].back(); vt[i][v].pop_back();
		}
	}
	
	for(int i = 0 ; i < n ;i++) {
		for(int j = 0 ; j < m ; j++) {
			cout << a[i][j] + 1<<" ";
		}
		cout << endl;
	}
	for(int i=0;i<n;i++) {
		for(int j=0;j<m;j++) {
			while (a[i][j] / m != i) {
				swap(a[i][j], a[a[i][j] / m][j]);	
			}
		}
	}
	for(int i=0;i<n;i++) {
		for(int j=0;j<m;j++) {
			cout << a[i][j] + 1<<" ";
		}
		cout << endl;
	}
	return 0;
}


Measured memory_bytes: 3072