#include <bits/stdc++.h>
int comp(const void *a, const void *b) { return *(int *)a - *(int *)b; }
int min(int a, int b) { return a < b ? a : b; }
int max(int a, int b) { return a > b ? a : b; }
struct B {
  bool u;
  char x;
  char y;
  char dp;
  char cp;
  B(int _x = 0, int _y = 0, int _d = 0, int _c = 0, bool _u = false) {
    u = _u;
    x = _x;
    y = _y;
    dp = _d;
    cp = _c;
  }
};
B b[55][55][4][2];
char a[55][55], c;
int m, n, ans, x, y, DP, CP;
int dpmx[] = {1, 0, -1, 0};
int dpmy[] = {0, 1, 0, -1};
B move(int x, int y, int DP, int CP) {
  if (b[y][x][DP][CP].u) {
    return b[y][x][DP][CP];
  }
  if (a[y + dpmy[DP]][x + dpmx[DP]] == a[y][x]) {
    return b[y][x][DP][CP] = move(x + dpmx[DP], y + dpmy[DP], DP, CP);
  }
  int z = CP ? 1 : -1;
  if (a[y + dpmy[(4 + DP + z) % 4]][x + dpmx[(4 + DP + z) % 4]] == a[y][x]) {
    return b[y][x][DP][CP] = move(x + dpmx[(4 + DP + z) % 4],
                                  y + dpmy[(4 + DP + z) % 4], DP, CP);
  }
  if (a[y + dpmy[DP]][x + dpmx[DP]] != 0) {
    return b[y][x][DP][CP] = B(x + dpmx[DP], y + dpmy[DP], DP, CP, 1);
  } else {
    if (CP == 0)
      return b[y][x][DP][CP] = B(x, y, DP, 1, 1);
    else
      return b[y][x][DP][CP] = B(x, y, (DP + 1) % 4, 0, 1);
  }
}
int main() {
  scanf("%d%d\n", &m, &n);
  for (int i = 0; i < m; i++) {
    int j = 1;
    while (1) {
      scanf("%c", &c);
      if (c < '0' || c > '9') break;
      a[i + 1][j++] = c - '0';
    }
    scanf("\n");
  }
  x = y = 1;
  DP = CP = 0;
  for (int i = 0; i < n; i++) {
    if (!b[y][x][DP][CP].u) move(x, y, DP, CP);
    int tx = b[y][x][DP][CP].x;
    int ty = b[y][x][DP][CP].y;
    int tDP = b[y][x][DP][CP].dp;
    int tCP = b[y][x][DP][CP].cp;
    x = tx;
    y = ty;
    DP = tDP;
    CP = tCP;
    x = tx;
  }
  printf("%c", a[y][x] + '0');
  return 0;
}
