#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;
template<class T>
inline bool chmax(T &a, T b) {
    if(a < b) {
        a = b;
        return true;
    }
    return false;
}

template<class T>
inline bool chmin(T &a, T b) {
    if(a > b) {
        a = b;
        return true;
    }
    return false;
}



#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
//const ll mod = 1000000007;

struct BipartiteMatching {
  vector< vector< int > > graph;
  vector< int > match, alive, used;
  int timestamp;

  BipartiteMatching(int n) : graph(n), alive(n, 1), used(n, 0), match(n, -1), timestamp(0) {}

  void add_edge(int u, int v) {
    graph[u].push_back(v);
    graph[v].push_back(u);
  }

  bool dfs(int idx) {
    used[idx] = timestamp;
    for(auto &to : graph[idx]) {
      int to_match = match[to];
      if(alive[to] == 0) continue;
      if(to_match == -1 || (used[to_match] != timestamp && dfs(to_match))) {
        match[idx] = to;
        match[to] = idx;
        return true;
      }
    }
    return false;
  }

  int bipartite_matching() {
    int ret = 0;
    for(int i = 0; i < graph.size(); i++) {
      if(alive[i] == 0) continue;
      if(match[i] == -1) {
        ++timestamp;
        ret += dfs(i);
      }
    }
    return ret;
  }

  void output() {
    for(int i = 0; i < graph.size(); i++) {
      if(i < match[i]) {
        cout << i << "-" << match[i] << endl;
      }
    }
  }
};


int N, M;
int A[105][105];
int B[105][105];
int C[105][105];
set<int> st[105];
bool connected[105][105];
void f(int column) {
    for(int i = 1; i <= 100; i++) {
        for(int j = 1; j <= 100; j++) {
            connected[i][j] = false;
        }
    }
    BipartiteMatching graph(2 * N + 1);
    for(int i = 1; i <= N; i++) {
        for(auto itr = st[i].begin(); itr != st[i].end(); itr++) {
            int now = *itr;
            now = (now + M - 1) / M;
            if(connected[now][i]) continue;
            graph.add_edge(now, i + N);
            connected[now][i] = true;
        }
    }
    //cerr << graph.bipartite_matching() << endl;
    graph.bipartite_matching();
    for(int i = 1; i <= N; i++) {
        //cerr << i << " " << graph.match[i] << endl;
        int index = graph.match[i] - N;
        auto itr = st[index].lower_bound((i - 1) * M + 1);
        //cerr << index << " " << (i-1)*M+1 << endl;
        //cerr << *itr << endl;
        B[index][column] = *itr;
        st[index].erase(itr);
    }
}

int main() {
    //cout.precision(10);
    cin.tie(0);
    ios::sync_with_stdio(false);
    cin >> N >> M;
    for(int i = 1; i <= N; i++) {
        for(int j = 1; j <= M; j++) {
            cin >> A[i][j];
            st[i].insert(A[i][j]);
        }
    }
    for(int i = 1; i <= M; i++) {
        f(i);
    }
    for(int i = 1; i <= N; i++) {
        for(int j = 1; j <= M; j++) {
            cout << B[i][j] << " ";
        }
        cout << endl;
    }
    for(int i = 1; i <= M; i++) {
        vector<int> v;
        for(int j = 1; j <= N; j++) v.push_back(B[j][i]);
        sort(v.begin(), v.end());
        for(int j = 1; j <= N; j++) {
            C[j][i] = v[j-1];
        }
    }
    for(int i = 1; i <= N; i++) {
        for(int j = 1; j <= M; j++) {
            cout << C[i][j] << " ";
        }
        cout << endl;
    }
    return 0;
}


Measured memory_bytes: 1152