Scanner scn = new Scanner(System.in);
int N =scn.nextInt();
int M =scn.nextInt();
int [][] arr = new int[N][M];
for(int i=0 ; i<N ; i++)
{
for(int j=0 ; j<M ; j++)
{
arr[i][j]=scn.nextInt();
}
}
waveprint(arr);
}
public static void waveprint(int[][] arr)
{
int left=0;
int top=0;
int right=arr[top].length-1;
int bottom=arr.length-1;
int count=(bottom+1)*(right+1);
int dir=1;
while(top<=bottom && left<=right)
{
if(count>0)
{
if(dir==1)
{
for(int i=left ; i<=right ; i++)
{
System.out.print(arr[top][i]+", ");
count–;
}
top++;
dir=2;
}
}
if(count>0)
{
if(dir==2)
{
for(int i = right ; i>=0 ; i–)
{
System.out.print(arr[top][i]+ ", ");
count–;
}
top++;
dir=1;
}
}
if(count>0)
{
if(dir==1)
{
for(int i =left ; i<=right ; i++)
{
System.out.print(arr[top][i]+", ");
count--;
}
top++;
dir=2;
}
}
if(count>0)
{
if(dir==2)
{
for(int i=right ; i>=0 ; i--)
{
System.out.print(arr[top][i]+", ");
count--;
}
}
}
}
System.out.print("END");
}
}