#include <bits/stdc++.h>
using namespace std;
int mr[] = {0, 1, 0, -1};
int mc[] = {1, 0, -1, 0};
char a[52][52];
int id[52][52];
int ar[2500], br[2500], ac[2500], bc[2500];
void flood(int r, int c, char color, int tag) {
  ar[tag] = min(ar[tag], r);
  br[tag] = max(br[tag], r);
  ac[tag] = min(ac[tag], c);
  bc[tag] = max(bc[tag], c);
  id[r][c] = tag;
  for (int o = 0; o < 4; o++) {
    int dr = r + mr[o];
    int dc = c + mc[o];
    if (a[dr][dc] == color && id[dr][dc] < 0) flood(dr, dc, color, tag);
  }
}
int main() {
  int n, m, s, cnt = 0;
  scanf("%d%d", &n, &s);
  for (int i = 1; i <= n; i++) scanf("%s", a[i] + 1);
  m = strlen(a[1] + 1);
  memset(id, 255, sizeof(id));
  memset(ar, 127, sizeof(ar));
  memset(br, 128, sizeof(br));
  memset(ac, 127, sizeof(ac));
  memset(bc, 128, sizeof(bc));
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      if (id[i][j] < 0) flood(i, j, a[i][j], cnt++);
  int r = 1, c = 1, dp = 0, cp = 0;
  while (s--) {
    int tr = r, tc = c, tag = id[r][c];
    if (dp == 0 && cp == 0) tr = ar[tag], tc = bc[tag] + 1;
    if (dp == 0 && cp == 1) tr = br[tag], tc = bc[tag] + 1;
    if (dp == 1 && cp == 0) tr = br[tag] + 1, tc = bc[tag];
    if (dp == 1 && cp == 1) tr = br[tag] + 1, tc = ac[tag];
    if (dp == 2 && cp == 0) tr = br[tag], tc = ac[tag] - 1;
    if (dp == 2 && cp == 1) tr = ar[tag], tc = ac[tag] - 1;
    if (dp == 3 && cp == 0) tr = ar[tag] - 1, tc = ac[tag];
    if (dp == 3 && cp == 1) tr = ar[tag] - 1, tc = bc[tag];
    if (!a[tr][tc] || a[tr][tc] == '0') {
      cp ^= 1;
      if (!cp) dp = (dp + 1) & 3;
    } else {
      r = tr;
      c = tc;
    }
  }
  printf("%c\n", a[r][c]);
}
