import java.util.*;
public class waveprintcolumn {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int m,n;
m=sc.nextInt();
n=sc.nextInt();
int [][] arr = new int [m][n];
for(int i=0;i<m;i++) {
for(int j=0;j<n;j++) {
arr[i][j]=sc.nextInt();
}
}
int count = m*n;
int left=0;
int top=0;
int bottom = arr.length; //BOTTOM = 3
int dir=1;
while(count>0) {
if(dir==1) {
for(int i=top;i<bottom;i++) {
System.out.print(arr[i][left]+","+" ");
count--;
}
left++;
dir=2;
}
if(dir==2) {
for(int j=bottom-1;j>=top;j--) {
System.out.print(arr[j][left]+","+" ");
count--;
}
left++;
dir=1;
}
}
System.out.print("END");
}
}