//#include "stdafx.h"

#include <algorithm> 
#include <cctype> 
#include <cmath> 
#include <cstdio> 
#include <cstdlib> 
#include <cstring> 
#include <deque> 
#include <iostream> 
#include <list> 
#include <map> 
#include <numeric> 
#include <queue> 
#include <set> 
#include <sstream> 
#include <stack> 
#include <string> 
#include <vector>

using namespace std; 

#define fo(i,j,n) for(i=j;i<n;++i)
#define Fo(i,j,n) for(i=n-1;i>=j;--i)
#define foo(i,j,v) fo(i,j,sz(v))
#define Foo(i,j,v) Fo(i,j,sz(v))
#define li(v) v.begin(),v.end()
#define sz(v) ((int)v.size())
#define CLR(a,v) memset((a),(v),sizeof(a))
#define inf 1000000001
//typedef long long Long;
typedef __int64 Long;
#define pi (2*acos(0))
#define eps 1e-9

#define two(X) (1<<(X))
#define twoL(X) (((Long)(1))<<(X))
#define contain(S,X) (((S)&two(X))!=0)
#define containL(S,X) (((S)&twoL(X))!=0)

char BUFFER[100000 + 5];
bool readn(int &n)	{ return scanf("%d",&n) == 1; } 
bool readl(Long &n)	{ return scanf("%I64d",&n) == 1; } 
bool readd(double &n){ return scanf("%lf",&n) == 1; } 
bool reads(string &s){ s = ""; int n = scanf("%s",BUFFER); if(n == 1)s = BUFFER; return n == 1; }
bool readln(string &s){ char *valid = gets(BUFFER); if(valid)s = BUFFER; return ((bool)valid); }

int row[] = {0,1,0,-1};
int col[] = {1,0,-1,0};
const int maxn = 55;
vector<string> v;
int f[maxn][maxn][4][2];

struct xx
{
	int dp,cp,r,c,tr,tc;
	void next()
	{
		while(1)
		{
			tr = r + row[dp]; tc = c + col[dp]; if(tr < 0 || tr >= sz(v) || tc < 0 || tc >= sz(v[0]))break;
			if(v[tr][tc] == '0')break; if(v[r][c] == v[tr][tc]){ r = tr; c = tc; continue;} r = tr; c = tc;  return;
		}
		if(cp == 0){ cp = 1; return ; } cp = 0; dp++; dp %= 4;
	}
	bool isvisit()
	{
		return (f[r][c][dp][cp] >= 0);
	}
	int getvalue()
	{
		return f[r][c][dp][cp];
	}
	void setvalue(int value)
	{
		f[r][c][dp][cp] = value;
	}
	int getcolor()
	{
		return (v[r][c] - '0');
	}
};



int main()
{
	//freopen("D://input.txt","r",stdin);
	CLR(f,-1); int i,n,m,value; cin >> m >> n; v.resize(m);
	foo(i,0,v)reads(v[i]); xx x; x.r = x.c = x.dp = x.cp = 0; x.setvalue(0);
	while(n--)
	{
		value = x.getvalue(); value++; x.next(); if(!x.isvisit()){ x.setvalue(value); continue; }
		value -= x.getvalue();  n %= value; break;
	}
	n = max(0,n);
	while(n--)x.next();
	cout << x.getcolor();
	printf("\n");
	return 0;
} 

