/* cerberus97 - Hanit Banga */

#include <iostream>
#include <iomanip>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <algorithm>

using namespace std;

#define pb push_back
#define fast_cin() ios_base::sync_with_stdio(false); cin.tie(NULL)

typedef long long ll;
typedef long long LL;
typedef long double ld;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;

// Attribution - t3nsor codebook
struct Edge {
  int u, v;
  LL cap, flow;
  int id;
  Edge() {}
  Edge(int u, int v, LL cap, int id = -1): u(u), v(v), cap(cap), flow(0), id(id) {}
};

struct Dinic {
  int N;
  vector<Edge> E;
  vector<vector<int>> g;
  vector<int> d, pt;
  
  Dinic(int N): N(N), E(0), g(N), d(N), pt(N) {}

  void AddEdge(int u, int v, LL cap, int id = -1) {
    if (u != v) {
      E.emplace_back(Edge(u, v, cap, id));
      g[u].emplace_back(E.size() - 1);
      E.emplace_back(Edge(v, u, 0));
      g[v].emplace_back(E.size() - 1);
    }
  }

  bool BFS(int S, int T) {
    queue<int> q({S});
    fill(d.begin(), d.end(), N + 1);
    d[S] = 0;
    while(!q.empty()) {
      int u = q.front(); q.pop();
      if (u == T) break;
      for (int k: g[u]) {
        Edge &e = E[k];
        if (e.flow < e.cap && d[e.v] > d[e.u] + 1) {
          d[e.v] = d[e.u] + 1;
          q.emplace(e.v);
        }
      }
    }
    return d[T] != N + 1;
  }

  LL DFS(int u, int T, LL flow = -1) {
    if (u == T || flow == 0) return flow;
    for (int &i = pt[u]; i < g[u].size(); ++i) {
      Edge &e = E[g[u][i]];
      Edge &oe = E[g[u][i]^1];
      if (d[e.v] == d[e.u] + 1) {
        LL amt = e.cap - e.flow;
        if (flow != -1 && amt > flow) amt = flow;
        if (LL pushed = DFS(e.v, T, amt)) {
          e.flow += pushed;
          oe.flow -= pushed;
          return pushed;
        }
      }
    }
    return 0;
  }

  LL MaxFlow(int S, int T) {
    LL total = 0;
    while (BFS(S, T)) {
      fill(pt.begin(), pt.end(), 0);
      while (LL flow = DFS(S, T))
        total += flow;
    }
    return total;
  }
};

const int N = 1e2 + 10;

int a[N][N], b[N][N], c_transpose[N][N];
bool used[N][N];

int main() {
	fast_cin();
	int n, m;
	cin >> n >> m;
	for (int i = 1; i <= n; ++i) {
		for (int j = 1; j <= m; ++j) {
			cin >> a[i][j];
			--a[i][j];
		}
	}
	for (int col = 1; col <= m; ++col) {
		int source = 0, sink = 2 * n + 1;
		Dinic dinic(sink + 1);
		for (int i = 1; i <= n; ++i) {
			dinic.AddEdge(source, i, 1);
			dinic.AddEdge(i + n, sink, 1);
		}
		for (int i = 1; i <= n; ++i) {
			for (int j = 1; j <= m; ++j) {
				if (!used[i][j]) {
					dinic.AddEdge(i, a[i][j] / m + 1 + n, 1, j);
				}
			}
		}
		assert(dinic.MaxFlow(source, sink) == n);
		// cout << "YAY" << endl;
		for (auto &e : dinic.E) {
			if (e.id != -1 and e.flow == 1) {
				b[e.u][col] = a[e.u][e.id] + 1;
				used[e.u][e.id] = true;
				// cout << e.u << ' ' << e.id << endl;
			}
		}
	}
	for (int i = 1; i <= m; ++i) {
		for (int j = 1; j <= n; ++j) {
			c_transpose[i][j] = b[j][i];
		}
		sort(c_transpose[i] + 1, c_transpose[i] + 1 + n);
	}
	for (int i = 1; i <= n; ++i) {
		for (int j = 1; j <= m; ++j) {
			cout << b[i][j] << ' ';
		}
		cout << '\n';
	}
	for (int i = 1; i <= n; ++i) {
		for (int j = 1; j <= m; ++j) {
			cout << c_transpose[j][i] << ' ';
		}
		cout << '\n';
	}
}

Measured memory_bytes: 2060