#include <bits/stdc++.h>
using namespace std;
int a[1000][1000], b[1000];
int main ()
{
	freopen ("SortMT.inp", "r", stdin);
	freopen ("SortMT.out", "w", stdout);
	int m,n,max=0, min=20000;
	cin >> m >> n;
	for (int i=1; i<=m; i++)
		for (int j=1; j<=n; j++)
			{
			cin >> a[m][n];
			if (a[m][n]>max) max=a[m][n];
			if (a[m][n]<min) min=a[m][n];
			}
	cout << max << " " << min << '\n';
	for (int i=1; i<=m; i++)
	{
		for (int j=1; j<=n; j++)
		{
			b[j]=a[i][j];	
		}
		sort (b+1, b+n+1);
		for (int x=1; x<=n; x++)
		cout << b[x] << " ";
		cout << '\n';
	}
}
