import java.util.*;
public class Main {
public static void main (String args[]) {
Scanner s = new Scanner(System.in);
int m = s.nextInt();
int n = s.nextInt();
int[][] matrix = new int[m][n];
for(int i = 0; i < m; i++) {
for(int j = 0; j < n; j++) {
matrix[i][j] = s.nextInt();
}
}
int[] arr = diagTraversal(matrix);
for(int i: arr) {
System.out.print(i + " ");
}
}
static int[] diagTraversal(int[][] matrix){
if(matrix == null || matrix.length == 0){
return new int[0];
}
int n = matrix.length;
int m = matrix[0].length;
int row = 0, col = 0, dir = 1;
int[] res = new int[n*m];
for(int i=0; i < res.length; i++){
res[i] = matrix[row][col];
if(dir == 1){
if(col == n-1){
row++;
dir = -1;
}else if(row == 0){
col++;
dir = -1;
}else{
row--;
col++;
}
} else {
if(row == n-1){
col++;
dir=1;
}else if(col == 0){
row++;
dir=1;
}else{
row++; col--;
}
}
}
return res;
}
}