#include<iostream>
#include<fstream>
using namespace std;
int main()
{ ifstream f( "matrice.txt" );
int v[21][31], n, m, i, j, min, max, l1, l2;
cin >> n >> m;
for ( i = 1; i <= n; i ++)
for ( j = 1; j <= m; j ++) f >> v[i][j];
max = v[1][1];
min = v[1][2];
for ( i = 2; i <= n; i ++) {
for ( j = 3; j <= m; j ++) {
if ( v[i][j] > max ) { max = v[i][j];
l1 = i; }
else if ( v[i][j] < min ) { min = v[i][j];
l2 = i; }
}
}
for ( i = 1; i <= n; i ++) {
for ( j = 1; j <= m; j ++)
swap ( v[l1][j] , v[l2][j] );
}
for ( i = 1; i <= n; i ++) {
for ( j = 1; j <= m; j ++)
cout << v[i][j] << " ";
cout << endl;
}
f.close();
return 0;
}