#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <string>
#include <cstring>
#include <queue>
#include <complex>
#include <cmath>
#include <set>
#include <map>
#include <numeric>
using namespace std;

#define REP(i,n) for(int i = 0; i < (int)n; ++i)
#define FOR(i,a,b) for(int i = (int)a; i <= (int)b; ++i)
#define FORd(i,a,b) for(int i = (int)a; i >= (int)b; --i)
#define FORit(it,v) for(__typeof((v).begin()) it = (v).begin(); it != (v).end(); ++it)
#define all(v) (v).begin(),(v).end()
#define mset(ar,x) memset(ar,x,sizeof(ar))
#define pub push_back
#define fr first
#define sc second
#define mp make_pair
#define eps 1e-6
#define inf 2000000000LL
#define huge 8000000000000000000LL

typedef long long llong;
typedef pair<int,int> Pii;

//--diarnwan's template--//

int m,n,nstep;
char line[100];
int peta[55][55];
Pii next[55][55][5];
const int Up = 1;
const int Rg = 2;
const int Dw = 3;
const int Lf = 4;

int main()
{
	scanf("%d %d",&m,&nstep);
	mset(peta,0);
	FOR(i,1,m){
		scanf("%s",line);
		n = strlen(line);
		FOR(j,1,n) peta[i][j] = (int)(line[j-1]-'0');
	}
	
	int k;
	FOR(i,1,m) FOR(j,1,n){
		// up
		k = i-1;
		while(peta[k][j] && peta[k][j] == peta[i][j]) k--;
		next[i][j][1] = mp(k,j);
		// right
		k = j+1;
		while(peta[i][k] && peta[i][k] == peta[i][j]) k++;
		next[i][j][2] = mp(i,k);
		// down
		k = i+1;
		while(peta[k][j] && peta[k][j] == peta[i][j]) k++;
		next[i][j][3] = mp(k,j);
		// left
		k = j-1;
		while(peta[i][k] && peta[i][k] == peta[i][j]) k--;
		next[i][j][4] = mp(i,k);
	}
	
	Pii BP;
	int DP,CP;
	BP = mp(1,1);
	DP = Rg;
	CP = Lf;
	
	while(nstep--){
		Pii now = next[BP.fr][BP.sc][DP];
		
		int DR = DP;
		if(CP == Lf) DR--;
		else if(CP == Rg) DR++;
		if(DR == 0) DR = 4;
		else if(DR == 5) DR = 1;
		
		now = next[now.fr][now.sc][DR];
		if(DR == 1) now.fr++;
		else if(DR == 2) now.sc--;
		else if(DR == 3) now.fr--;
		else if(DR == 4) now.sc++;
		
		if(peta[now.fr][now.sc] == 0){
			if(CP == Lf) CP = Rg;
			else{
				CP = Lf;
				DP++;
				if(DP == 5) DP = 1;
			}
		}else{
			BP = now;
		}
	}
	
	printf("%d\n",peta[BP.fr][BP.sc]);
	
	return 0;
}


	
