#include <bits/stdc++.h>
using namespace std;
char a[55][55];
int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
int n, T, m;
inline int valid(int x, int y) { return x >= 0 && x < n && y >= 0 && y < m; }
int cyc[55][55][4][2] = {};
int main(void) {
  scanf("%d%d", &n, &T);
  for (int i = 0; i < n; i++) scanf("%s", a[i]);
  m = strlen(a[0]);
  int x = 0, y = 0, DP = 0, CP = 0;
  int Cx = -dy[DP] * (CP ? -1 : 1), Cy = dx[DP] * (CP ? -1 : 1);
  int Dx = dx[DP], Dy = dy[DP];
  for (int step = 1; step <= T; step++) {
    while (valid(x + Dx, y + Dy) && a[x + Dx][y + Dy] == a[x][y])
      x += Dx, y += Dy;
    while (valid(x + Cx, y + Cy) && a[x + Cx][y + Cy] == a[x][y])
      x += Cx, y += Cy;
    if (cyc[x][y][DP][CP]) {
      int ii = step - cyc[x][y][DP][CP];
      while (step + ii <= T) step += ii;
    } else
      cyc[x][y][DP][CP] = step;
    if (!valid(x + Dx, y + Dy) || a[x + Dx][y + Dy] == '0') {
      if (CP == 0)
        CP = 1;
      else if (CP == 1) {
        DP = (DP + 1) % 4;
        CP = 0;
      }
      Cx = -dy[DP] * (CP ? -1 : 1), Cy = dx[DP] * (CP ? -1 : 1);
      Dx = dx[DP], Dy = dy[DP];
    } else
      x += Dx, y += Dy;
  }
  printf("%c\n", a[x][y]);
  return 0;
}
