#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>

using namespace std;
#define PB push_back
#define MP make_pair
#define st first
#define nd second
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())

using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
using vll = vector<long long>;
using ld = long double;

#define unordered_map __fast_unordered_map
template<class Key, class Value, class Hash = std::hash<Key>>
using unordered_map = __gnu_pbds::gp_hash_table<Key, Value, Hash>;

template<class C> void mini(C &a4, C b4) { a4 = min(a4, b4); }
template<class C> void maxi(C &a4, C b4) { a4 = max(a4, b4); }

template<class TH> void _dbg(const char *sdbg, TH h){ cerr<<sdbg<<'='<<h<<endl; }
template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) {
  while(*sdbg!=',')cerr<<*sdbg++;
  cerr<<'='<<h<<','; _dbg(sdbg+1, a...);
}

template<class T> ostream &operator<<(ostream& os, vector<T> V) {
  os << "["; for (auto vv : V) os << vv << ","; return os << "]";
}
template<class L, class R> ostream &operator<<(ostream &os, pair<L,R> P) {
  return os << "(" << P.st << "," << P.nd << ")";
}

#ifdef LOCAL
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#else
#define debug(...) (__VA_ARGS__)
#define cerr if(0)cout
#endif

const int kMaxN = 128;

int adj[kMaxN][kMaxN];
int n, m;
int match_left[kMaxN], match_right[kMaxN];
bool vis[kMaxN];

bool DfsMatch(int v) {
  if (vis[v]) { return false; }
  vis[v] = true;
  for (int s = 0; s < n; ++s) {
    if (!adj[v][s]) { continue; }
    if (match_right[s] == -1 || DfsMatch(match_right[s])) {
      match_left[v] = s;
      match_right[s] = v;
      return true;
    }
  }
  return false;
}

int Match() {
  fill_n(match_left, n, -1);
  fill_n(match_right, n, -1);
  int ans = 0;
  bool any;

  do {
    any = false;
    fill_n(vis, n, false);
    for (int v = 0; v < n; ++v) {
      if (!vis[v] && match_left[v] == -1 && DfsMatch(v)) {
        any = true; ++ans;
      }
    }
  } while (any);
  return ans;
}

int mat[kMaxN][kMaxN];
int gid[kMaxN][kMaxN];
int ans[kMaxN][kMaxN];

vi at_inter[kMaxN][kMaxN];

int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout << fixed << setprecision(11);
  cerr << fixed << setprecision(6);

  
  cin >> n >> m;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      cin >> mat[i][j];
      gid[i][j] = (mat[i][j] - 1) / m;
      ++adj[i][gid[i][j]];
      at_inter[i][gid[i][j]].PB(mat[i][j]);
    }
  }

  for (int col = 0; col < m; ++col) {
    int sz = Match();
    assert(sz == n);

    for (int i = 0; i < n; ++i) {
      const int j = match_left[i];
      ans[i][col] = at_inter[i][j].back();
      at_inter[i][j].pop_back();
      --adj[i][j];
    }
  }

  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      cout << ans[i][j] << " ";
    }
    cout << "\n";
  }

  for (int col = 0; col < m; ++col) {
    vi elems;
    for (int i = 0; i < n; ++i) { elems.PB(ans[i][col]); }
    sort(ALL(elems));
    for (int i = 0; i < n; ++i) { ans[i][col] = elems[i]; }
  }

  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      cout << ans[i][j] << " ";
    }
    cout << "\n";
  }
}



Measured memory_bytes: 1280