#include <bits/stdc++.h>
using namespace std;
int n, m;
int a[105][105], r[10005], c[10005];
bool f[105][105], f1[105][105];
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) scanf("%d", &a[i][j]), r[a[i][j]] = i;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      int x = (i - 1) * m + j, d = r[x];
      if (i % 2 == 0) {
        for (int k = 1; k <= m; k++)
          if (!f1[d][k] && !f[i][k]) {
            a[d][k] = x, f1[d][k] = f[i][k] = 1;
            break;
          }
      } else {
        for (int k = m; k >= 1; k--)
          if (!f1[d][k] && !f[i][k]) {
            a[d][k] = x, f1[d][k] = f[i][k] = 1;
            break;
          }
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) printf("%d ", a[i][j]), c[a[i][j]] = j;
    printf("\n");
  }
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      int d = (a[i][j] - 1) / m + 1;
      swap(a[i][j], a[d][j]);
    }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) printf("%d ", a[i][j]);
    printf("\n");
  }
}
