#include<stdio.h>
int main()
{
    int M,N;
    scanf("%d %d",&M,&N);
    if(M<0 || N<0)
    {
        printf("Invalid input");
        return 0;
    }
    int arr[M][N];
    for(int i=0;i<M;i++)
    {
        for(int j=0;j<N;j++)
        {
            scanf("%d",&arr[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",arr[top][i]);
        }
        top++''
        for(int i=left;i<=bottom;i++)
        {
            printf("%d",arr[i][right]);
        }
        right--;
        if(top<=bottom)
        {
            for(int i=right;i>=left;i--)
            {
                printf("%d",arr[bottom][i]);
            }
            bottom--;
        }
        if(left<=right)
        {
            for(int i=right;i>=left;i--)
            {
                printf("%d",arr[i][left]);
            }
            left++;
        }
    }
    return 0;
}