#include<stdio.h>
int main(void)
{
int m, n;
int a[400][400] = {}, st[100][100] = {};
scanf("%d %d", &m, &n);
int i, j;
for (i = 1; i <= m; i++)
{
for (j = 1; j <= n; j++)
{
scanf("%d", &a[i][j]);
}
}
int sum = n * m-1;
i = j = 1;
printf("%d ", a[1][1]);
st[1][1] = 1;
while (sum > 0)
{
if (i < m && j <= n && st[i + 1][j] == 0&&(st[i][j-1]!=0||(st[i][j - 1]==1&&st[i+1][j]==0)||j==1))
{
st[i + 1][j] = 1;
printf("%d", a[i + 1][j]);
i++;
}
else if ((i == m||st[i+1][j]==1) && j < n && st[i][j + 1] == 0)
{
st[i][j + 1] = 1;
printf("%d", a[i][j + 1]);
j++;
}
else if ((j == n||st[i][j+1]==1) && st[i - 1][j] == 0 && i - 1 > 0)
{
st[i - 1][j] = 1;
printf("%d", a[i-1][j]);
i--;
}
else if ((i == 1||st[i-1][j]==1) && st[i][j - 1] == 0 && j - 1 > 0)
{
st[i][j - 1] = 1;
printf("%d", a[i][j - 1]);
j--;
}
sum--;
if (sum != 0)
printf(" ");
}
return 0;
}