#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

#define x first
#define y second

int m, n, np;
int a[100][100];
char sir[100];

pair<int, int> next[60][60][4][2];

int xx[] = {0, 1, 0, -1};
int yy[] = {1, 0, -1, 0};

//p1 in limitele matricei
bool valid(pair<int, int> p1)
{
     if(0 <= p1.x && p1.x < m && 0 <= p1.y && p1.y < n) return true;
     return false;
}
//p1, p2 ac culoare
bool compId(pair<int, int> p1, pair<int, int> p2)
{
     if(valid(p1) && a[p1.x][p1.y] == a[p2.x][p2.y])
          return true;
     return false;
}
//move in directia d
pair<int, int> go(pair<int, int> p, int d)
{
    pair<int, int> r = p, move;
    move.x = p.x + xx[d]; move.y = p.y + yy[d];
    while(compId(move, p))
    {
       r = move;
       move.x += xx[d];
       move.y += yy[d];
    }   
    return r;
};
pair<int, int> getNext(pair<int, int> p, int& DP, int& CP)
{
    pair<int, int> r;
    r = go(p, DP);
    r = go(p, (DP + CP + 4)%4);
    
    r.x += xx[DP]; r.y += yy[DP];
    if(valid(r) && a[r.x][r.y]) return r;
    else 
    {
         if(CP == -1) CP = 1;
         else CP = -1, DP = (DP + 1) % 4;
         return p; 
    }
}
int solve()
{
    memset(next, -1, sizeof(next));

}
int main()
{
    int i, j;
#ifndef ONLINE_JUDGE
    freopen("d.in", "r", stdin);
    freopen("d.out", "w", stdout);
#endif

    scanf("%d %d", &m, &np);
    for(i = 0; i < m; ++i)
    {
        scanf(" %s", sir);
        for(j = 0; sir[j]; ++j) a[i][j] = sir[j] - '0';
        n = j;
    }

    memset(next, -1, sizeof(next));
    pair<int, int> res = make_pair(0, 0), aux;
    int DP = 0, CP = -1;
    for(;np; --np)
       res = getNext(res, DP, CP);
    
    printf("%d\n", a[res.x][res.y]);
    return 0;
}
