#include <iostream>
#include <cstdio>
using namespace std;
void CirPrint( int *number,int n,int m)
{
if(number==NULL) return;
int start=0;
int cout=0;
while(cout<n*m)
{
for(int i=start;i<n-start&&cout<n*m;i++)
{
printf("%3d",number[start*n+i]);
cout++;
}
for(int i=start+1;i<m-start&&cout<n*m;i++)
{
printf("%3d",number[i*n+(n-1-start)]);
cout++;
}
for(int i=n-2-start;i>=start&&cout<n*m;i--)
{
printf("%3d",number[(m-1-start)*n+i]);
cout++;
}
for(int i=m-2-start;i>start&&cout<n*m;i--)
{
printf("%3d",number[i*n+start]);
cout++;
}
start++;
}
}
int main()
{
int *number;
int n,m;
scanf("%d %d",&n,&m);
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
scanf("%d",&(number[i*m+j]));
}
CirPrint(number,n,m);
//cout << "Hello world!" << endl;
return 0;
}