#include <bits/stdc++.h>
using namespace std;
struct node {
  int x, y, dp, cp;
  void init(int _x, int _y, int _dp, int _cp) {
    x = _x, y = _y, dp = _dp, cp = _cp;
  }
} data[59][59][4][2];
bool state[59][59][4][2];
int dx[] = {0, -1, 0, 1}, dy[] = {1, 0, -1, 0};
char maze[59][59];
int tran(int dp, int cp) {
  if (dp == 0) {
    if (cp == 0) return 1;
    return 3;
  }
  if (dp == 1) {
    if (cp == 0) return 2;
    return 0;
  }
  if (dp == 2) {
    if (cp == 0) return 3;
    return 1;
  }
  if (cp == 0) return 0;
  return 2;
}
void next(int &x, int &y, int &dp, int &cp) {
  int sx = x, sy = y, sdp = dp, scp = cp;
  if (!state[x][y][dp][cp]) {
    state[x][y][dp][cp] = true;
    for (int temp = maze[x][y]; maze[x][y] && maze[x][y] == temp;
         x += dx[dp], y += dy[dp])
      ;
    x -= dx[dp], y -= dy[dp];
    int dd = tran(dp, cp);
    for (int temp = maze[x][y]; maze[x][y] && maze[x][y] == temp;
         x += dx[dd], y += dy[dd])
      ;
    x -= dx[dd], y -= dy[dd];
    if (!maze[x + dx[dp]][y + dy[dp]] || maze[x + dx[dp]][y + dy[dp]] == '0') {
      if (cp == 0) {
        cp = 1;
      } else {
        cp = 0;
        dp = ((dp - 1) % 4 + 4) % 4;
      }
    } else {
      x += dx[dp], y += dy[dp];
    }
    data[sx][sy][sdp][scp].init(x, y, dp, cp);
  }
  x = data[sx][sy][sdp][scp].x;
  y = data[sx][sy][sdp][scp].y;
  dp = data[sx][sy][sdp][scp].dp;
  cp = data[sx][sy][sdp][scp].cp;
}
int main(void) {
  int n, m;
  while (scanf("%d%d", &n, &m) + 1) {
    memset(maze, 0, sizeof(char));
    memset(state, false, sizeof(bool));
    for (int i = 1; i <= n; i++) scanf("%s", maze[i] + 1);
    int x = 1, y = 1, dp = 0, cp = 0;
    for (; m; m--) next(x, y, dp, cp);
    printf("%c\n", maze[x][y]);
  }
  return 0;
}
