#include <bits/stdc++.h>
using namespace std;
const int MAXN = 64;
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};
char s[MAXN][MAXN];
int m, n;
int visit[MAXN][MAXN][4][4];
bool in(const int x, const int y) { return 0 <= x && x < n && 0 <= y && y < m; }
struct node {
  int x, y;
  int DP, CP;
  int step;
  node() {}
  node(int x, int y, int DP, int CP, int step)
      : x(x), y(y), DP(DP), CP(CP), step(step) {}
  void setc() {
    char c(s[x][y]);
    while (in(x + dx[DP], y + dy[DP]) && s[x + dx[DP]][y + dy[DP]] == c)
      x += dx[DP], y += dy[DP];
    while (in(x + dx[CP], y + dy[CP]) && s[x + dx[CP]][y + dy[CP]] == c)
      x += dx[CP], y += dy[CP];
  }
  void next() {
    int i, j, k;
    int tx(x + dx[DP]), ty(y + dy[DP]);
    if (in(tx, ty) && s[tx][ty] != '0') {
      x = tx, y = ty;
    } else {
      if ((DP - CP + 4) % 4 == 1) {
        CP = (DP + 1) % 4;
      } else {
        DP = (DP + 1) % 4;
        CP = (DP + 3) % 4;
      }
    }
    ++step;
    setc();
  }
} cc(0, 0, 0, 3, 1);
vector<node> tt;
int main() {
  int i, j, k;
  int x, y, DP, CP, step;
  int src, dst, T, rem(-1);
  int re;
  tt.push_back(node(-1, -1, -1, -1, -1));
  scanf("%d %d", &n, &re);
  for (i = 0; i < n; ++i) scanf("%s", s[i]);
  m = strlen(s[0]);
  memset(visit, -1, sizeof(visit));
  cc.setc();
  for (i = 1; i <= re; ++i) {
    x = cc.x, y = cc.y, DP = cc.DP, CP = cc.CP, step = cc.step;
    if (visit[x][y][DP][CP] != -1) {
      src = visit[x][y][DP][CP], dst = cc.step - 1;
      T = dst - src + 1;
      rem = re - dst + 1;
      break;
    }
    tt.push_back(cc);
    visit[cc.x][cc.y][cc.DP][cc.CP] = i;
    cc.next();
  }
  if (rem == -1) {
    printf("%c\n", s[cc.x][cc.y]);
  } else {
    --rem;
    rem %= T;
    if (rem == 0) rem = T - 1;
    printf("%c\n", s[tt[src + rem].x][tt[src + rem].y]);
  }
  return 0;
}
