#include <bits/stdc++.h>
// iostream is too mainstream
#include <cstdio>
// bitch please
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <list>
#include <cmath>
#include <iomanip>
#include <time.h>
#define dibs reserve
#define OVER9000 1234567890
#define ALL_THE(CAKE,LIE) for(auto LIE =CAKE.begin(); LIE != CAKE.end(); LIE++)
#define tisic 47
#define soclose 1e-8
#define chocolate win
// so much chocolate
#define patkan 9
#define ff first
#define ss second
#define abs(x) (((x) < 0)?-(x):(x))
#define uint unsigned int
#define dbl long double
#define pi 3.14159265358979323846
using namespace std;
// mylittledoge

using cat = long long;

#ifdef DONLINE_JUDGE
	// palindromic tree is better than splay tree!
	#define lld I64d
#endif

int main() {
	cin.sync_with_stdio(0);
	cin.tie(0);
	cout << fixed << setprecision(10);
	int N, M;
	cin >> N >> M;
	vector< vector<int> > A(N, vector<int>(M));
	// for(int i = 0; i < N; i++) for(int j = 0; j < M; j++) A[i][j] = i*M+j;
	// for(int i = 0; i < N*M; i++) swap(A[i/M][i%M], A[rand()%N][rand()%M]);
	for(int i = 0; i < N; i++) for(int j = 0; j < M; j++) {
		cin >> A[i][j];
		A[i][j]--;
	}
	vector< vector<int> > occ(M, vector<int>(N, 0));
	vector< vector<int> > B = A;
	for(int i = 0; i < N; i++) for(int j = 0; j < M; j++) occ[j][B[i][j]/M]++;
	int rep = 0;
	srand(N);
	for(int r = 0; r < 20; r++) {
		vector<int> P(M);
		for(int j = 0; j < M; j++) {
			P[j] = j;
			swap(P[j], P[rand()%(j+1)]);
		}
		vector<int> R(N);
		for(int j = 0; j < N; j++) {
			R[j] = j;
			swap(R[j], R[rand()%(j+1)]);
		}
		for(int i = 0; i < N; i++) {
			int id = R[i];
			for(int j = 0; j < M; j++) {
				for(int k = 0; k < M; k++) {
					int d = 0;
					if(occ[P[k]][B[id][P[k]]/M] == 1) d--;
					else d++;
					if(occ[P[j]][B[id][P[j]]/M] == 1) d--;
					else d++;
					if(occ[P[j]][B[id][P[k]]/M] == 0) d++;
					else d--;
					if(occ[P[k]][B[id][P[j]]/M] == 0) d++;
					else d--;
					if(d <= 0) continue;
					occ[P[k]][B[id][P[k]]/M]--;
					occ[P[j]][B[id][P[j]]/M]--;
					occ[P[j]][B[id][P[k]]/M]++;
					occ[P[k]][B[id][P[j]]/M]++;
					swap(B[id][P[k]], B[id][P[j]]);
				}
			}
		}
	}
	while(true) {
		bool ok = true;
		vector<int> P(M);
		for(int j = 0; j < M; j++) {
			P[j] = j;
			swap(P[j], P[rand()%(j+1)]);
		}
		vector<int> R(N);
		for(int j = 0; j < N; j++) {
			R[j] = j;
			swap(R[j], R[rand()%(j+1)]);
		}
		for(int i = 0; i < N; i++) {
			int id = R[i];
			for(int j = 0; j < M; j++) if(occ[P[j]][B[id][P[j]]/M] > 1) {
				ok = false;
				bool b = false;
				for(int k = 0; k < M; k++)
				if(occ[P[j]][B[id][P[k]]/M] == 0 || occ[P[k]][B[id][P[j]]/M] == 0 || occ[P[k]][B[id][P[k]]/M] > 1) {
					int d = 0;
					if(occ[P[k]][B[id][P[k]]/M] == 1) d--;
					else d++;
					if(occ[P[j]][B[id][P[j]]/M] == 1) d--;
					else d++;
					if(occ[P[j]][B[id][P[k]]/M] == 0) d++;
					else d--;
					if(occ[P[k]][B[id][P[j]]/M] == 0) d++;
					else d--;
					if(d == 0) continue;
					occ[P[k]][B[id][P[k]]/M]--;
					occ[P[j]][B[id][P[j]]/M]--;
					occ[P[j]][B[id][P[k]]/M]++;
					occ[P[k]][B[id][P[j]]/M]++;
					swap(B[id][P[k]], B[id][P[j]]);
					b = true;
					break;
				}
				if(!b) for(int k = 0; k < M; k++)
				if(occ[P[j]][B[id][P[k]]/M] == 0 || occ[P[k]][B[id][P[j]]/M] == 0 || occ[P[k]][B[id][P[k]]/M] > 1) {
					occ[P[k]][B[id][P[k]]/M]--;
					occ[P[j]][B[id][P[j]]/M]--;
					occ[P[j]][B[id][P[k]]/M]++;
					occ[P[k]][B[id][P[j]]/M]++;
					swap(B[id][P[k]], B[id][P[j]]);
					b = true;
					break;
				}
			}
		}
		if(ok) break;
		rep++;
		int sum = 0;
		for(int i = 0; i < N; i++) for(int j = 0; j < M; j++) sum += abs(1-occ[j][i]);
		cerr << sum << "\n";
		if(rep == 2000) {
			cerr << "FAIL\n";
			return 0;
		}
	}
	for(int i = 0; i < N; i++)
		for(int j = 0; j < M; j++) cout << B[i][j]+1 << ((j == M-1) ? "\n" : " ");
	vector< vector<int> > C = B;
	for(int i = 0; i < M; i++) {
		vector<int> v(N);
		for(int j = 0; j < N; j++) v[j] = B[j][i];
		sort(begin(v), end(v));
		for(int j = 0; j < N; j++) C[j][i] = v[j];
	}
	for(int i = 0; i < N; i++)
		for(int j = 0; j < M; j++) cout << C[i][j]+1 << ((j == M-1) ? "\n" : " ");
	return 0;
}

// look at my code
// my code is amazing


Measured memory_bytes: 512