#include<bits/stdc++.h>
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define pb push_back
using namespace std;
using ll = long long;
using vi = vector<ll>;
using pi = pair<ll, ll>;
const int maxn = 2e2 + 6;//, mod = 1e9 + 7;
struct FlowEdge {
    int v, u;
    long long cap, flow = 0;
    FlowEdge(int v, int u, long long cap) : v(v), u(u), cap(cap) {}
};
int match[maxn];
struct Dinic {
    const long long flow_inf = 1e18;
    vector<FlowEdge> edges;
    vector<vector<int>> adj;
    int n, m = 0;
    int s, t;
    vector<int> level, ptr;
    queue<int> q;

    Dinic(int n, int s, int t) : n(n), s(s), t(t) {
        adj.resize(n);
        level.resize(n);
        ptr.resize(n);
    }

    void add_edge(int v, int u, long long cap) {
        edges.emplace_back(v, u, cap);
        edges.emplace_back(u, v, 0);
        adj[v].push_back(m);
        adj[u].push_back(m + 1);
        m += 2;
    }

    bool bfs() {
        while (!q.empty()) {
            int v = q.front();
            q.pop();
            for (int id : adj[v]) {
                if (edges[id].cap - edges[id].flow < 1)
                    continue;
                if (level[edges[id].u] != -1)
                    continue;
                level[edges[id].u] = level[v] + 1;
                q.push(edges[id].u);
            }
        }
        return level[t] != -1;
    }

    long long dfs(int v, long long pushed) {
        if (pushed == 0)
            return 0;
        if (v == t)
            return pushed;
        for (int& cid = ptr[v]; cid < (int)adj[v].size(); cid++) {
            int id = adj[v][cid];
            int u = edges[id].u;
            if (level[v] + 1 != level[u] || edges[id].cap - edges[id].flow < 1)
                continue;
            long long tr = dfs(u, min(pushed, edges[id].cap - edges[id].flow));
            if (tr == 0)
                continue;
            edges[id].flow += tr;
            edges[id ^ 1].flow -= tr;
            return tr;
        }
        return 0;
    }

    long long solve() {
        long long f = 0;
        while (true) {
            fill(level.begin(), level.end(), -1);
            level[s] = 0;
            q.push(s);
            if (!bfs())
                break;
            fill(ptr.begin(), ptr.end(), 0);
            while (long long pushed = dfs(s, flow_inf)) {
                f += pushed;
            }
        }
        int N = (n-2)/2;
		for(auto i : edges) {
			if(i.flow==1&&i.u>1&&i.v>1) {
				if(i.u>i.v) swap(i.u, i.v);
				//assert();
				//cout << i.u << " " << i.v << "edge\n";
				match[i.u-2] = i.v-2-N;
			}
		}
        return f;
    }
};
int n, m;
int mat[maxn][maxn];
map<int, int> cnt[maxn];
set<int> h[maxn];
void gay_sort(int x) {
	for(int _ = 0; _ < n; _++)
		for(int i = 1; i < n; i++)
			if(mat[i][x]<mat[i-1][x]) swap(mat[i][x], mat[i-1][x]);
}
int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cin >> n >> m;
	for(int t, i = 0; i < n; i++) {
		for(int j = 0; j < m; j++) {
			cin >> t, t--;
			cnt[i][t/m]++;
			h[i].insert(t);
		}
	}
	for(int i = 0; i < m; i++) {//super retarded algorithm
		//cout << i << "============\n";
		Dinic d(n + n + 2, 0, 1);// n rows + n colours + s + t
		for(int i = 0; i < n; i++) d.add_edge(0, 2+i, 1);
		for(int i = 0; i < n; i++) d.add_edge(2+n+i, 1, 1);
		for(int j = 0; j < n; j++) {
			for(auto x : h[j]) {
				d.add_edge(2+j, 2+n+x/m, 1);
			}
		}
		d.solve();
		for(int j = 0; j < n; j++) {
			for(auto x : h[j]) {
				if(x/m==match[j]) {mat[j][i] = x;break;}
			}
			h[j].erase(mat[j][i]++);
		}
	}
	for(int i = 0; i < n; i++, cout << "\n")
		for(int j = 0; j < m; j++)
			cout << mat[i][j] << " ";
	for(int i = 0; i < m; i++) gay_sort(i);
	for(int i = 0; i < n; i++, cout << "\n")
		for(int j = 0; j < m; j++)
			cout << mat[i][j] << " ";
	
}



Measured memory_bytes: 2592