#include <bits/stdc++.h>
using namespace std;
struct point {
  int x, y;
  point(int xv, int yv) {
    x = xv;
    y = yv;
  }
};
char s[55];
int gr[55][55];
int dir[4][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
int lr[4][4] = {{0, 2, 0, 1}, {1, 0, 2, 0}, {0, 1, 0, 2}, {2, 0, 1, 0}};
int main() {
  int n, k, i, j, cp, dp, gx, gy, m;
  point bp(1, 1);
  scanf("%d%d", &n, &k);
  memset(gr, 0, sizeof(gr));
  for (i = 1; i <= n; i++) {
    scanf("%s", s);
    m = strlen(s);
    for (j = 1; j <= m; j++) gr[i][j] = s[j - 1] - '0';
  }
  cp = 0;
  dp = 1;
  while (k--) {
    gx = bp.x + dir[dp][0];
    gy = bp.y + dir[dp][1];
    if (gr[gx][gy] == 0) {
      if (lr[dp][cp] == 1) {
        cp = (dp + 1) % 4;
      } else {
        dp = (dp + 1) % 4;
        cp = (dp - 1 + 4) % 4;
      }
    } else {
      bp.x = gx;
      bp.y = gy;
    }
  }
  printf("%d\n", gr[bp.x][bp.y]);
  return 0;
}
