#include <bits/stdc++.h>
using namespace std;
const int maxs = 51;
char _s[maxs][maxs];
int s[maxs][maxs];
int go[4][2] = {{1, 0}, {0, -1}, {-1, 0}, {0, 1}};
int come[maxs][maxs][4][2][2];
int h, w;
inline bool nicecoord(int y, int x) {
  return y >= 0 && x >= 0 && y < h && x < w;
}
int main() {
  int k;
  while (scanf("%d%d", &h, &k) >= 2) {
    for (int i = 0; i < h; i++) {
      scanf("%s", _s[i]);
      w = strlen(_s[i]);
      for (int j = 0; j < w; j++) s[h - 1 - i][j] = _s[i][j] - '0';
    }
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        if (!s[i][j]) continue;
        int col = s[i][j];
        for (int g = 0; g < 4; g++) {
          for (int dg = 0; dg < 2; dg++) {
            int g2 = dg ? ((g + 1) & 3) : ((g + 3) & 3);
            int y = i, x = j;
            for (int gn = g, it = 0; it < 2; it++, gn = g2) {
              while (1) {
                int xn = x + go[gn][0], yn = y + go[gn][1];
                if (!nicecoord(yn, xn) || col != s[yn][xn]) break;
                y = yn, x = xn;
              }
            }
            assert(s[y][x] == col);
            come[i][j][g][dg][0] = y, come[i][j][g][dg][1] = x;
          }
        }
      }
    }
    int y, x, dir, dg, it;
    for (y = h - 1, x = 0, dir = 0, dg = 0, it = 0; it < k; it++) {
      int newy = come[y][x][dir][dg][0], newx = come[y][x][dir][dg][1];
      y = newy, x = newx;
      newx = x + go[dir][0], newy = y + go[dir][1];
      if (nicecoord(newy, newx) && s[newy][newx]) {
        x = newx, y = newy;
        continue;
      }
      if (dg == 0)
        dg = 1;
      else
        dg = 0, dir = (dir + 1) & 3;
    }
    printf("%d\n", s[y][x]);
  }
  return 0;
}
