#define y1 askjdkasldjlkasd
#include <bits/stdc++.h>
#undef y1

using namespace std;

#define pb push_back
#define mp make_pair
#define fi(a, b) for(int i=a; i<=b; i++)
#define fj(a, b) for(int j=a; j<=b; j++)
#define fo(a, b) for(int o=a; o<=b; o++)
#define fdi(a, b) for(int i=a; i>=b; i--)
#define fdj(a, b) for(int j=a; j>=b; j--)
#define fdo(a, b) for(int o=a; o>=b; o--)
#define sz(x) (int)x.size()

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<pii> vpii;
typedef pair<ll, ll> pll;
typedef vector<pll> vpll;
typedef vector<ll> vll;

#ifdef LOCAL
#define err(...) fprintf(stderr, __VA_ARGS__)
#else
#define err(...) while (0)
#endif

double START_TIME;

void exit() {
#ifdef LOCAL	
	cerr << "TIME: " << setprecision(5) << fixed << (clock() - START_TIME) / CLOCKS_PER_SEC << endl;
#endif	
	exit(0);
}

template<typename A, typename B>
ostream& operator<<(ostream& os, pair<A, B> p) {
	os << "(" << p.first << ", " << p.second << ")";
	return os;
}

template<typename T>
ostream& operator<<(ostream& os, vector<T> v) {
	fi(0, sz(v) - 1) {
		os << v[i] << " ";
	}
	return os;
}

template<typename T>
ostream& operator<<(ostream& os, set<T> t) {
	for (auto z : t) {
		os << z << " ";
	}
	return os;
}

template<typename T1, typename T2>
ostream& operator<<(ostream& os, map<T1, T2> t) {
	cerr << endl;
	for (auto z : t) {
		os << "\t" << z.first << " -> " << z.second << endl;
	}
	return os;
}

#ifdef LOCAL
#define dbg(x) {cerr << __LINE__ << "\t" << #x << ": " << x << endl;}
#define dbg0(x, n) {cerr << __LINE__ << "\t" << #x << ": "; for (int ABC = 0; ABC < n; ABC++) cerr << x[ABC] << ' '; cerr << endl;}
#else
#define dbg(x) while(0){}
#define dbg0(x, n) while(0){}
#endif

#ifdef LOCAL
#define ass(x) if (!(x)) { cerr << __LINE__ << "\tassertion failed: " << #x << endl, abort(); }
#else
#define ass(x) assert(x)
#endif

///////////////////////////////////////////////////

const int MAX = 114;

int n, m;
int a[MAX][MAX];

int getcl(int v) {
	return v / m;
}

vi e[MAX];

void buildg(int c) {
	fi(0, MAX - 1) e[i].clear();
	fi(0, n - 1) {
		vi v;
		fj(c, m - 1) {
			v.pb(getcl(a[i][j]));
		}
		sort(v.begin(), v.end());
		v.resize(unique(v.begin(), v.end()) - v.begin());
		for (int z : v) e[i].pb(z);
	}
}

int C;
int u[MAX];
int mt[MAX];
int f[MAX];

bool dfs(int x) {
	u[x] = C;
	for (int y : e[x]) {
		if (mt[y] == -1) {
			mt[y] = x;
			return true;
		}
	}
	for (int y : e[x]) {
		if (u[mt[y]] != C && dfs(mt[y])) {
			mt[y] = x;
			return true;
		}
	}
	return false;
}

void findmatching() {
	memset(mt, -1, sizeof(mt));
	memset(f, 0, sizeof(f));
	fi(0, n - 1) {
		for (int z : e[i]) {
			if (mt[z] == -1) {
				f[i] = 1;
				mt[z] = i;
				break;
			}
		}
	}
	fi(0, n - 1) {		
		if (f[i] == 0) {
			C++;
			dfs(i);
		}
	}		
	/*
	fi(0, n - 1) err("i = %d mt = %d\n", i, mt[i]);
	err("\n");
	*/
}

void refresh(int c) {
	fi(0, n - 1) {
		int r = mt[i];
		assert(r != -1);
		bool f = false;
		fj(c, m - 1) {
			if (getcl(a[r][j]) == i) {
				f = true;
				swap(a[r][j], a[r][c]);
				break;
			}
		}
		assert(f);
	}
}

void print() {
	fi(0, n - 1) {
		fj(0, m - 1) {
			printf("%d ", a[i][j] + 1);
		}
		printf("\n");
	}	
}

void sortcol() {
	fi(0, m - 1) {
		fj(0, n - 1) {
			fo(j + 1, n - 1) {
				if (a[o][i] < a[j][i]) {
					swap(a[o][i], a[j][i]);
					assert(getcl(a[o][i]) != getcl(a[j][i]));
				}
			}
		}
	}
}

void solve () {
	fj(0, m - 1) {
		buildg(j);
		findmatching();
		refresh(j);
//		print();
//		printf("\n");
	}	
	print();
	sortcol();
	print();
}

int main() {
#ifdef LOCAL
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	START_TIME = (double)clock();
#endif
	
	scanf("%d %d", &n, &m);
	fi(0, n - 1) fj(0, m - 1) {
		scanf("%d", &a[i][j]);
		a[i][j]--;
	}
	solve();

	exit();
	return 0;
}





Measured memory_bytes: 384