#include <cstdio>
#include <cstring>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <algorithm>

#define mod(x) (((x) + 4) % 4)

using namespace std;

struct state {
    int nx, ny, nd, nc;
    state(int ny, int nx, int nd, int nc) :
        ny(ny), nx(nx), nd(nd), nc(nc) {}
    state(){}
} next[64][64][4][2];

char board[64][64];

int mx, my, n;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};

bool can(int y, int x)
{
    return (0 <= y && y < my && 0 <= x && x < mx);
}

bool ok(int y, int dy, int x, int dx)
{
    return (can(y + dy, x + dx) && (board[y][x] == board[y + dy][x + dx]));
}


int main()
{
    scanf("%d %d", &my, &n);
    
    for (int i = 0; i < my; i++){
        scanf("%s", board[i]);
    }
    
    mx = strlen(board[0]);
    
    for (int i = 0; i < my; i++){
        for (int j = 0; j < mx; j++){
            for (int dir = 0; dir < 4; dir++){
                for (int d2 = 0; d2 < 2; d2++){
                    int px = j, py = i, pd1 = dir, pd2 = d2;
                    //iterate by dir
                    while (ok(py, dy[dir], px, dx[dir])){
                        py += dy[dir];
                        px += dx[dir];
                    }
                    
                    int v = !d2 ? -1 : 1;
                    
                    while (ok(py, dy[mod(dir + v)], px, dx[(dir + v)])){
                        py += dy[mod(dir + v)];
                        px += dx[mod(dir + v)];
                    }
                    
                    if (can(py + dy[dir], px + dx[dir]) && board[py + dy[dir]][px + dx[dir]] != '0'){
                        py += dy[dir];
                        px += dx[dir];
                    }
                    else {
                        pd2 ^= 1;
                        if (d2) pd1 = mod(pd1 + 1);
                    }
                    next[i][j][dir][d2] = state(py, px, pd1, pd2);
                }
            }
        }
    }
    state ns = state(0, 0, 0, 0);
    
    for (int i = 0; i < n; i++){
        ns = next[ns.ny][ns.nx][ns.nd][ns.nc];
    }
    
    printf("%c\n", board[ns.ny][ns.nx]);
    
    return (0);
}
