#include <bits/stdc++.h>
using namespace std;
char M[(55)][(55)];
int m, c, n, H[(1 << 15)];
vector<int> Q;
int mov[4][2] = {0, 1, 1, 0, 0, -1, -1, 0};
bool jud(int x, int y) { return -1 < x && x < m && -1 < y && y < c; }
int eco(int x, int y, int bp, int dp, int cp) {
  return cp | (dp << 1) | (y << 3) | (x << 9);
}
int dco(int cod) { return M[(cod >> 9) & 15][(cod >> 3) & 15] - '0'; }
void fin(int &x, int &y, int p) {
  while (true) {
    if (!jud(x + mov[p][0], y + mov[p][1]) ||
        M[x][y] != M[x + mov[p][0]][y + mov[p][1]])
      break;
    x += mov[p][0];
    y += mov[p][1];
  }
}
int main() {
  int l, x, y, bp, dp, cp, tp, cod, loop;
  while (~scanf("%d%d", &m, &n)) {
    for (int i = 0; i < m; i++) scanf("%s", M[i]);
    c = strlen(M[0]);
    bp = M[0][0] - '0';
    memset(H, 0, sizeof H);
    cp = 1;
    Q.clear();
    x = y = dp = loop = 0;
    while (n--) {
      cod = eco(x, y, bp, dp, cp);
      if (H[cod]) {
        loop = Q.size() - H[cod] + 1;
        n = n % loop;
        memset(H, 0, sizeof H);
      }
      Q.push_back(cod);
      H[cod] = Q.size();
      tp = (cp * (-2) + 5 + dp) % 4;
      fin(x, y, dp);
      fin(x, y, tp);
      if (!jud(x + mov[dp][0], y + mov[dp][1]) ||
          M[x + mov[dp][0]][y + mov[dp][1]] == '0') {
        if (!cp) dp = (dp + 1) % 4;
        cp = 1 - cp;
      } else {
        x += mov[dp][0];
        y += mov[dp][1];
        bp = M[x][y] - '0';
      }
    }
    printf("%d\n", bp);
  }
  return 0;
}
