#include <bits/stdc++.h>
#include <unistd.h>

using namespace std;

#define DEBUG(x) cerr<<#x<<": "<<x<<endl;
#define DEBUG_VEC(v) cerr<<#v<<":";for(int i=0;i<v.size();i++) cerr<<" "<<v[i]; cerr<<endl
#define DEBUG_MAT(v) cerr<<#v<<endl;for(int i=0;i<v.size();i++){for(int j=0;j<v[i].size();j++) {cerr<<v[i][j]<<" ";}cerr<<endl;}


typedef long long ll;

#define vi vector<int>
#define vl vector<ll>
#define vii vector< vector<int> >
#define vll vector< vector<ll> >
#define vs vector<string>
#define pii pair<int,int>
#define pis pair<int,string>
#define psi pair<string,int>
#define pll pair<ll,ll>
template<class S, class T> pair<S, T> operator+(const pair<S, T> &s, const pair<S, T> &t) { return pair<S, T>(s.first + t.first, s.second + t.second); }
template<class S, class T> pair<S, T> operator-(const pair<S, T> &s, const pair<S, T> &t) { return pair<S, T>(s.first - t.first, s.second - t.second); }
template<class S, class T> ostream& operator<<(ostream& os, pair<S, T> p) { os << "(" << p.first << ", " << p.second << ")"; return os; }
#define X first
#define Y second
#define rep(i,n) for(int i=0;i<(n);i++)
#define rep1(i,n) for(int i=1;i<=(n);i++)
#define rrep(i,n) for(int i=(n)-1;i>=0;i--)
#define rrep1(i,n) for(int i=(n);i>0;i--)
#define REP(i,a,b) for(int i=a;i<b;i++)
#define in(x, a, b) (a <= x && x < b)
#define all(c) c.begin(),c.end()
template<class T> bool chmax(T &a, const T &b) { if (a<b) { a = b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a = b; return 1; } return 0; }
#define UNIQUE(v) v.erase(std::unique(v.begin(), v.end()), v.end());
const ll inf = 1000000001;
const ll INF = (ll)1e18 + 1;
const ll MOD = 1000000007;
//const ll MOD = 998244353;
const double pi = 3.14159265358979323846;
#define Sp(p) cout<<setprecision(15)<< fixed<<p<<endl;
int dx[4] = { -1,0, 1,0 }, dy[4] = { 0,1,0,-1 };
int dx2[8] = { 1,1,0,-1,-1,-1,0,1 }, dy2[8] = { 0,1,1,1,0,-1,-1,-1 };
#define fio() cin.tie(0); ios::sync_with_stdio(false);
// #define mp make_pair
//#define endl '\n'

set<int> st;

vii b;
int cnt = 0;
//マッチングに使う
bool dfs(int v, vector<vector<pii>> &G, vi &match, vi &used) {
  used[v] = true;
  for (int i = 0; i < G[v].size(); i++) {
    int u = G[v][i].first, w = match[u];
    int val = G[v][i].second;
    if (st.count(val)) continue;
    if (w < 0 || !used[w] && dfs(w, G, match, used)) {
      match[v] = u;
      match[u] = v;
      int mi = min(u, v);
      b[mi][cnt] = val;
      return true;
    }
  }
  return false;
}

//二部グラフの最大マッチングを求める
//二部グラフの最大マッチング = 最小点カバー = n-最小辺カバー = n-最大安定集合
// http://buyoh.hateblo.jp/entry/2017/06/12/085722
// 頂点に重みがついてる最小点カバーのurl, Dinicで解ける
int bipartite_matching(vector<vector<pii>> &G) {
  int res = 0;
  vi match(G.size(), -1);
  vi used(G.size());
  for (int v = 0; v < G.size(); v++) {
    if (match[v] < 0) {
      fill(used.begin(), used.end(), 0);
      if (dfs(v, G, match, used)) {
        res++;
      }
    }
  }
  rep (i, b.size()) {
    st.insert(b[i][cnt]);
  }
  return res;
}


int main() {
  int h, w;
  cin >> h >> w;
  vii a(h, vi(w));
  rep (i, h) {
    rep (j, w) {
      cin >> a[i][j];
      a[i][j]--;
    }
  }
 
  vector<vector<pii>> G(2*h);
  rep (i, h) {
    rep (j, w) {
      G[i].push_back(pii(a[i][j] / w + h, a[i][j]));
      G[a[i][j] / w + h].push_back(pii(i, a[i][j]));
    }
  }
  //DEBUG_MAT(G);

  b.resize(h);
  rep (i, h) b[i].resize(w, -1);
  rep (j, w) {
    int res = bipartite_matching(G);
    /*
    DEBUG(res);
    DEBUG_MAT(b);
    for (auto itr = st.begin(); itr != st.end(); itr++) {
      cout << *itr << " ";
    }
    cout << endl;
    //*/
    cnt++;
  }
  
  rep (i, h) {
    rep (j, w) {
      cout << b[i][j] + 1 << " ";
    }
    cout << endl;
  }
 
  vii c(h, vi(w));
  rep (j, w) {
    vi temp(h);
    rep (i, h) temp[i] = b[i][j];
    sort(all(temp));
    rep (i, h) {
      c[i][j] = temp[i];
    }
  }
 
  rep (i, h) {
    rep (j, w) {
      cout << c[i][j] + 1 << " ";
    }
    cout << endl;
  }
}


Measured memory_bytes: 1152