#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1.0);
const int di[] = {0, 1, 0, -1};
const int dj[] = {1, 0, -1, 0};
char a[111][111];
int nu[111][111][4][2], nv[111][111][4][2], m, n, q;
void init() {
  for (int i = (1), _b = (m); i <= _b; i++)
    for (int j = (1), _b = (n); j <= _b; j++)
      if (a[i][j] != '0')
        for (int dir = 0, _a = (4); dir < _a; dir++)
          for (int cp = 0, _a = (2); cp < _a; cp++) {
            nu[i][j][dir][cp] = i;
            nv[i][j][dir][cp] = j;
            int ii = i + di[dir], jj = j + dj[dir];
            while (ii > 0 && jj > 0 && ii <= m && jj <= n &&
                   a[ii][jj] == a[i][j]) {
              nu[i][j][dir][cp] = ii;
              nv[i][j][dir][cp] = jj;
              ii = ii + di[dir], jj = jj + dj[dir];
            }
            ii -= di[dir];
            jj -= dj[dir];
            int dir2 = dir;
            if (cp == 0)
              dir2--;
            else
              dir2++;
            if (dir2 < 0)
              dir2 = 3;
            else if (dir2 > 3)
              dir2 = 0;
            ii = ii + di[dir2], jj = jj + dj[dir2];
            while (ii > 0 && jj > 0 && ii <= m && jj <= n &&
                   a[ii][jj] == a[i][j]) {
              nu[i][j][dir][cp] = ii;
              nv[i][j][dir][cp] = jj;
              ii = ii + di[dir2], jj = jj + dj[dir2];
            }
          }
}
int main() {
  scanf("%d%d\n", &m, &q);
  for (int i = (1), _b = (m); i <= _b; i++) scanf("%s\n", &a[i][1]);
  n = 1;
  while (a[1][n + 1] >= '0' && a[1][n + 1] <= '9') n++;
  init();
  int u = 1, v = 1, dp = 0, cp = 0;
  while (q--) {
    int uu = nu[u][v][dp][cp], vv = nv[u][v][dp][cp];
    uu = uu + di[dp], vv = vv + dj[dp];
    if (uu < 1 || vv < 1 || uu > m || vv > n || a[uu][vv] == '0') {
      if (cp == 0)
        cp = 1;
      else {
        cp = 0;
        dp = (dp + 1) % 4;
      }
    } else {
      u = uu;
      v = vv;
    }
  }
  printf("%d\n", a[u][v] - '0');
  return 0;
}
