#include<fstream>
#include<iostream>
#include<vector>
#include <cstring>
using namespace std;
ifstream fin("spiral.in");
ofstream fout("spiral.out");



int n,m,k,i,j,p,x,y,a[1005][1005],t,b[1005][1005];

void init()
{
    int i,j;
    for(i=0;i<=n+1;i++)
    {
        for(j=0;j<=m+1;j++)
        {
            a[i][j]=-1;
            b[i][j]=-1;
        }
    }
}

void solve(int n,int m,int x,int y)
{
    bool gasit=true;
    b[x][y]=-1;
    fout<<a[x][y]<<" ";
    //fout<<"merge";
    while(gasit==true)
    {
        gasit=false;
        //fout<<"ok";
        if(b[x][y+1]==0 and b[x][y-1]==-1 and b[x-1][y]==-1 and   gasit==false )
        {
            gasit=true;
            y++;
            cout<<a[x][y]<<" ";
            b[x][y]=-1;
        }

        if(b[x+1][y]==0 and b[x-1][y]==-1 and b[x][y+1]==-1 and   gasit==false )
        {
            gasit=true;
            x++;
            cout<<a[x][y]<<" ";
            b[x][y]=-1;
        }

        if(b[x][y-1]==0  and b[x][y+1]==-1 and b[x+1][y]==-1 and   gasit==false )
        {
            gasit=true;
            y--;
            cout<<a[x][y]<<" ";
            b[x][y]=-1;
        }

        if(b[x-1][y]==0  and b[x+1][y]==-1 and b[x][y-1]==-1 and   gasit==false )
        {
            gasit=true;
            x--;
            cout<<a[x][y]<<" ";
            b[x][y]=-1;
        }

    }
}



int main()
{
    cin>>n>>m;
    init();
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=m;j++)
        {
            cin>>a[i][j];
            b[i][j]=0;
        }
    }
    solve(n,m,1,1);




}