#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <string>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <stdio.h>
using namespace std;
int MOD = 1000000007;
class XorShift {
public:
	unsigned int x, y, z, w;
	double nL[65536];

	XorShift()
	{
		init();
	}

	void init()
	{
		x = 314159265; y = 358979323; z = 846264338; w = 327950288;
		double n = 1 / (double)(2 * 65536);
		for (int i = 0; i < 65536; i++) {
			nL[i] = log(((double)i / 65536) + n);
		}
	}

	inline unsigned int next()
	{
		unsigned int t = x ^ x << 11; x = y; y = z; z = w; return w = w ^ w >> 19 ^ t ^ t >> 8;
	}

	inline double nextLog() {
		return nL[next() & 0xFFFF];
	}

	inline int nextInt(int m)
	{
		return (int)(next() % m);
	}


	inline int nextInt(int min, int max)
	{
		return min + nextInt(max - min + 1);
	}


	inline double nextDouble()
	{
		return (double)next() / ((long long)1 << 32);
	}


};
XorShift rnd;
struct FastSet {
	vector<int> list;
	vector<int> pos;
	void init(int N) {
		pos.resize(N, -1);
		list.reserve(N);
	}
	void insert_all() {
		list.clear();
		list.resize(pos.size());
		for (int i = 0; i < list.size(); i++) {
			pos[i] = list[i] = i;
		}
	}
	void insert(int a) {
		if (pos[a] == -1) {
			pos[a] = list.size();
			list.push_back(a);
		}
	}
	void erase(int a) {
		if (pos[a] >= 0) {
			swap(list[pos[a]], list.back());
			pos[list[pos[a]]] = pos[a];
			pos[a] = -1;
			list.pop_back();
		}
	}
	void flip(int a) {
		if (pos[a] == -1) {
			pos[a] = list.size();
			list.push_back(a);
		}
		else {
			swap(list[pos[a]], list.back());
			pos[list[pos[a]]] = pos[a];
			pos[a] = -1;
			list.pop_back();
		}
	}
	inline int size() {
		return list.size();
	}
	inline int rand() {
		return list[rnd.nextInt(list.size())];
	}
	inline void erase_all() {
		for (int i : list) {
			pos[i] = -1;
		}
		list.clear();
	}
};
vector<vector<FastSet> >fs;
vector<vector<pair<int, int> > > A;

const int64_t CYCLES_PER_SEC = 2800000000;
struct Timer {
	int64_t start;
	Timer() { reset(); }
	void reset() { start = getCycle(); }
	void plus(double a) { start -= (a * CYCLES_PER_SEC); }
	inline double get() { return (double)(getCycle() - start) / CYCLES_PER_SEC; }
	inline int64_t getCycle() {
		uint32_t low, high;
		__asm__ volatile ("rdtsc" : "=a" (low), "=d" (high));
		return ((int64_t)low) | ((int64_t)high << 32);
	}
};
Timer timer;

signed main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	int N, M;
	cin >> N >> M;
	timer.reset();
	A.resize(N, vector < pair<int, int> >(M));
	fs.resize(M, vector<FastSet>(N));
	for (int i = 0; i < M; i++) {
		for (int j = 0; j < N; j++) {
			fs[i][j].init(N);
		}
	}

	for (int i = 0; i < N; i++) {
		for (int j = 0; j < M; j++) {
			cin >> A[i][j].first;
			A[i][j].second = (A[i][j].first - 1) / M;
			fs[j][A[i][j].second].insert(i);
		}
	}


	while (timer.get() < 1.8) {
		int a = rnd.nextInt(M);
		int b = rnd.nextInt(N);
		if (fs[a][b].size() > 1) {
			int c = fs[a][b].rand();
			
			vector<int> kouho;
			for (int i = 0; i < M; i++) {
				if (fs[i][b].size() == 0) {
					kouho.push_back(i);
				}
			}
			if(kouho.size() > 0){
				int d = kouho[rnd.nextInt(kouho.size())];
				fs[a][A[c][a].second].erase(c);
				fs[d][A[c][d].second].erase(c);
				swap(A[c][a], A[c][d]);
				fs[a][A[c][a].second].insert(c);
				fs[d][A[c][d].second].insert(c);
			}
		}
	}
	vector < vector<int> > res(M, vector<int>(N));

	for (int i = 0; i < N; i++) {
		for (int j = 0; j < M; j++) {
			res[j][i] = A[i][j].first;
		}
	}
	for (int i = 0; i < N; i++) {
		for (int j = 0; j < M; j++) {
			cout << res[j][i] << " ";
		}
		cout << endl;
	}

	for (int i = 0; i < M; i++) {
		sort(res[i].begin(), res[i].end());
	}
	for (int i = 0; i < N; i++) {
		for (int j = 0; j < M; j++) {
			cout << res[j][i] << " ";
		}
		cout << endl;
	}


}

Measured memory_bytes: 9600