#include <fstream>
#include <vector>
#include <algorithm>
#include <stdint.h>

using namespace std;

ifstream input("SortMT.inp");
ofstream output("SortMT.out");

int main()
{
	int n, m;
	int min = INT32_MAX, max = INT32_MIN;
	input >> n >> m;
	vector <vector <int> > a(n, vector <int>(m));
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
		{
			input >> a[i][j];
		}
		sort(a[i].begin(), a[i].end());
		if (a[i].front() < min)
		{
			min = a[i].front();
		}
		if (a[i].back() > max)
		{
			max = a[i].back();
		}
	}
	output << max << ' ' << min << '\n';
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
		{
			output << a[i][j] << ' ';
		}
		output << '\n';
	}
	input.close();
	output.close();
	return 0;
}