#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <cassert>
#define FILL(a, n, x) memset((a), (x), sizeof ((a)[0]) * (n))
#define FILLA(a, x) memset((a), (x), sizeof (a))
#define COPY(a, n, b) memcpy((b), (a), sizeof ((a)[0]) * (n))
#define COPYA(a, b) memcpy((b), (a), sizeof (a))
using namespace std;

typedef pair<int, int> pii;
typedef long long li;

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

namespace dinic {
  const int maxn = 1e5, maxm = 1e6;
  typedef int FlowType;
  const FlowType inf_f = ~0U >> 1;

  void Init(int n);
  void AddEdge(int u, int v, FlowType cap, int id);
  FlowType Solve(int s, int t);
  vector< pair<int, FlowType> > Result(void);
}

namespace dinic {
  struct Edge {
    int v, id;
    FlowType cap, cap0;
  } edge[maxm << 1];

  int n, m, s, t;
  vector<int> g[maxn];

  void Init(int _n) {
    n = _n, m = 0;
    for (int i = 0; i < n; ++i) g[i].clear();
  }

  void AddEdge(int u, int v, FlowType c, int id = -1) {
    g[u].push_back(m);
    edge[m++] = (Edge){v, id, c, c};
    g[v].push_back(m);
    edge[m++] = (Edge){u, id, 0, 0};
  }

  int iter[maxn], dist[maxn];

  bool Bfs(void) {
    queue<int> que;
    que.push(s);
    FILL(dist, n, -1);
    dist[s] = 0;
    while (!que.empty()) {
      int u = que.front(); que.pop();
      for (int i = 0; i < g[u].size(); ++i) {
        Edge &e = edge[g[u][i]];
        if (e.cap && dist[e.v] == -1) {
          dist[e.v] = dist[u] + 1;
          que.push(e.v);
        }
      }
    }
    return dist[t] >= 0;
  }

  FlowType Dfs(int u, FlowType f) {
    if (u == t) return f;
    FlowType sum = 0;
    for (int &i = iter[u]; i < g[u].size(); ++i) {
      Edge &e = edge[g[u][i]], &rev = edge[g[u][i] ^ 1];
      if (e.cap && dist[e.v] == dist[u] + 1) {
        FlowType res = Dfs(e.v, min(f - sum, e.cap));
        e.cap -= res;
        rev.cap += res;
        if ((sum += res) == f) break;
      }
    }
    return sum;
  }

  FlowType Solve(int _s, int _t) {
    s = _s, t = _t;
    FlowType ans = 0;
    while (Bfs()) {
      FILL(iter, n, 0);
      ans += Dfs(s, inf_f);
    }
    return ans;
  }

  vector< pair<int, FlowType> > Result(void) {
    vector< pair<int, FlowType> > ans;
    for (int u = 0; u < n; ++u) {
      for (int i = 0; i < g[u].size(); ++i) {
        Edge &e = edge[g[u][i]];
        if (e.cap < e.cap0 && e.id != -1) {
          ans.push_back(make_pair(e.id, e.cap0 - e.cap));
        }
      }
    }
    return ans;
  }
}

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

const int maxn = 100;

int n, m;
pii a[maxn][maxn];
bool rm[maxn][maxn];
pii b[maxn][maxn];

void Run1(void) {
  FILLA(rm, 0);
  for (int i = 0; i < m; ++i) {
    dinic::Init(2 * n + 2);
    for (int r = 0; r < n; ++r) {
      for (int c = 0; c < m; ++c) {
        if (!rm[r][c]) {
          dinic::AddEdge(r, n + a[r][c].first, 1, r * m + c);
        }
      }
    }
    int S = 2 * n, T = S + 1;
    for (int r = 0; r < n; ++r) {
      dinic::AddEdge(S, r, 1);
      dinic::AddEdge(n + r, T, 1);
    }
    dinic::Solve(S, T);
    auto res = dinic::Result();
    assert(res.size() == n);
    for (int j = 0; j < n; ++j) {
      int t = res[j].first;
      int r = t / m, c = t % m;
      rm[r][c] = true;
      b[r][i] = a[r][c];
    }
  }
  COPYA(b, a);
}

void Run2(void) {
  static pii col[maxn];
  for (int c = 0; c < m; ++c) {
    for (int r = 0; r < n; ++r) {
      col[r] = a[r][c];
    }
    sort(col, col + n);
    for (int r = 0; r < n; ++r) {
      a[r][c] = col[r];
    }
  }
}

void Print(void) {
  for (int r = 0; r < n; ++r) {
    for (int c = 0; c < m; ++c) {
      printf("%d%c", a[r][c].second, " \n"[c == m - 1]);
    }
  }
}

int main(void) {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      int id;
      scanf("%d", &id);
      a[i][j] = pii((id - 1) / m, id);
    }
  }
  Run1();
  Print();
  Run2();
  Print();
  return 0;
}


Measured memory_bytes: 3712