#include<stdio.h>
#include<stdlib.h>
int main() {
    int m,n;
    if(scanf("%d %d",&m,&n) != 2){
        return 1;
    }
if(m < 0||n < 0){
    printf("-1\n");
    return 0;
}
int matrix[m];
for(int i = 0;i < m; i++){
    for(int j = 0; j < n; j++){
        scanf("%d",&matrix[i][j]);
    }
}
int top = 0,bottom = m-1,left = 0,right = n-1;
while(top <= bottom && left <= right){
    for(int i = left; i <= right; i++)
    printf("%d ",matrix[top][i]);
}
top++;
for(int i = top; i <= bottom; i++){
    printf("%d ",matrix[i][right]);
}
right--;
if(top <= bottom){
    for(int i = right; i >= left; i--){
    printf("%d ",matrix[bottom][i]);
}
bottom--;
}
if(left <= right){
    for(int i = bottom; i >= top;i--){
        printf("%d ",matrix[i][left]);
    }
    left++;
  }
 printf("\n");
 return 0;