import java.util.;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
// System.out.println(āEnter the sizeā);
Scanner sc=new Scanner(System.in);
int row=sc.nextInt();
int col=sc.nextInt();
int[][] Arr=new int[row][col];
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
Arr[i][j]=sc.nextInt();
}
/ for(int i=0;i<size;i++)
{
for(int j=0;j<size;j++)
System.out.print(Arr[i][j]);
}*/
SpiralPrint(Arr);
sc.close();
}
public static void SpiralPrint(int[][] Arr) {
int top=0,bottom=3,left=0,right=3;
int dir=1;
int count=(bottom+1)*(right+1);
while(left<=right&&top<=bottom)
{
if(count>0)
{
if(dir==1) {
for(int i=left;i<=right;i++)
{
System.out.print(Arr[top][i]+", ");
count--;
}
dir=2;
top++;
}
}
if(count>0)
if(dir==2) {
for(int i=top;i<=bottom;i++)
{
System.out.print(Arr[i][right]+", ");
count--;
}
dir=3;
right--;
}
if(count>0)
if(dir==3) {
for(int i=right;i>=left;i--)
{
System.out.print(Arr[bottom][i]+", ");
count--;
}
dir=4;
bottom--;
}
if(count>0)
if(dir==4) {
for(int i=bottom;i>=top;i--)
{
System.out.print(Arr[i][left]+", ");
count--;
}
dir=1;
left++;
}
}
System.out.print("END");
}
}