#include <bits/stdc++.h>
using namespace std;
int main()
{
int n,to,r,c,tmp=1,now=1,flag=1;
cin>>n;
cin>>to;
int Map[n][n];
vector<int> run;
for (int i=0;i<n;i++) for (int j=0;j<n;j++) cin>>Map[i][j];
r=n/2;
c=n/2;
run.push_back(Map[r][c]);
while (now!=n*n)
{
for (int i=0;i<2;i++)
{
for (int j=0;j<tmp;j++)
{
if (to==0) run.push_back(Map[r][--c]);
else if(to==1) run.push_back(Map[--r][c]);
else if (to==2) run.push_back(Map[r][++c]);
else run.push_back(Map[++r][c]);
if (++now==n*n)
{
flag>>=1;
break;
}
}
if (!flag) break;
to==3?to=0:to++;
}
tmp++;
}
for (const int val:run) cout<<val;
cout<<"\n";
return 0;
}