#include <bits/stdc++.h>
using namespace std;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
struct Point {
  int x, y;
};
struct Rect {
  int t, l, b, r, color;
};
int main() {
  int n, h, w;
  scanf("%d%d ", &h, &n);
  char B[50][51];
  for (int i = 0; i < (h); i++) gets(B[i]);
  w = strlen(B[0]);
  int id[50][50];
  vector<Rect> R;
  for (int i = 0; i < (h); i++)
    for (int j = 0; j < (w); j++)
      if (B[i][j] != '0' && B[i][j] != '*') {
        char dgt = B[i][j];
        int t = i, l = j, b = i, r = j;
        B[i][j] = '*';
        queue<Point> qu;
        qu.push((Point){j, i});
        while (!qu.empty()) {
          Point p = qu.front();
          qu.pop();
          t = min(t, p.y);
          l = min(l, p.x);
          b = max(b, p.y);
          r = max(r, p.x);
          id[p.y][p.x] = R.size();
          for (int k = 0; k < (4); k++) {
            int x = p.x + dx[k], y = p.y + dy[k];
            if (0 <= x && x < w && 0 <= y && y < h && B[y][x] == dgt) {
              B[y][x] = '*';
              qu.push((Point){x, y});
            }
          }
        }
        R.push_back((Rect){t, l, b, r, dgt});
      }
  int bp = 0, dp = 0, cp = 0;
  for (int _ = 0; _ < (n); _++) {
    int x, y;
    if (dp == 0 && cp == 0) x = R[bp].r + 1, y = R[bp].t;
    if (dp == 0 && cp == 1) x = R[bp].r + 1, y = R[bp].b;
    if (dp == 1 && cp == 0) x = R[bp].r, y = R[bp].b + 1;
    if (dp == 1 && cp == 1) x = R[bp].l, y = R[bp].b + 1;
    if (dp == 2 && cp == 0) x = R[bp].l - 1, y = R[bp].b;
    if (dp == 2 && cp == 1) x = R[bp].l - 1, y = R[bp].t;
    if (dp == 3 && cp == 0) x = R[bp].l, y = R[bp].t - 1;
    if (dp == 3 && cp == 1) x = R[bp].r, y = R[bp].t - 1;
    if (0 <= x && x < w && 0 <= y && y < h && B[y][x] != '0') {
      bp = id[y][x];
    } else {
      if (cp == 0)
        cp = 1;
      else
        cp = 0, dp = (dp + 1) % 4;
    }
  }
  printf("%c\n", R[bp].color);
  return 0;
}
