#include <bits/stdc++.h>
using namespace std;
const int MAX_H = 50;
const int MAX_W = 50;
const int MAX_BN = MAX_H * MAX_W;
const int MAX_M = MAX_BN * 4 * 2;
const int dxs[] = {1, 0, -1, 0}, dys[] = {0, 1, 0, -1};
enum { DR, DD, DL, DU };
enum { CL, CR };
struct IP {
  int bp, dp, cp;
  IP() {}
  IP(int _bp, int _dp, int _cp) : bp(_bp), dp(_dp), cp(_cp) {}
  void print() { printf("(%d,%d,%d)\n", bp, dp, cp); }
};
struct Rect {
  int x0, y0, x1, y1;
  Rect() {}
  Rect(int _x0, int _y0, int _x1, int _y1)
      : x0(_x0), y0(_y0), x1(_x1), y1(_y1) {}
  void print() { printf("(%d,%d)-(%d,%d)\n", x0, y0, x1, y1); }
};
char flds[MAX_H][MAX_W + 4], chs[MAX_BN];
int bids[MAX_H][MAX_W], ds[MAX_BN][4][2];
IP ips[MAX_M + 1];
Rect rs[MAX_BN];
int main() {
  int h, n;
  scanf("%d%d", &h, &n);
  for (int y = 0; y < h; y++) scanf("%s", flds[y]);
  int w = strlen(flds[0]);
  memset(bids, -1, sizeof(bids));
  int bn = 0;
  for (int y = 0; y < h; y++)
    for (int x = 0; x < w; x++)
      if (flds[y][x] > '0' && bids[y][x] < 0) {
        char f = flds[y][x];
        int x1 = x, y1 = y;
        for (int vy = y; vy < h && flds[vy][x] == f; vy++)
          for (int vx = x; vx < w && flds[vy][vx] == f; vx++)
            bids[vy][vx] = bn, x1 = vx, y1 = vy;
        rs[bn] = Rect(x, y, x1, y1);
        chs[bn] = f;
        bn++;
      }
  memset(ds, -1, sizeof(ds));
  int m = 0;
  ips[m] = IP(0, DR, CL);
  ds[0][DR][CL] = 0;
  while (m < n) {
    IP &ip = ips[m];
    int bp = ip.bp, dp = ip.dp, cp = ip.cp;
    int dx = dxs[dp], dy = dys[dp], dm = ds[bp][dp][cp];
    Rect &r = rs[bp];
    int bx, by;
    switch (dp) {
      case DR:
        bx = r.x1, by = (cp == CL) ? r.y0 : r.y1;
        break;
      case DD:
        bx = (cp == CL) ? r.x1 : r.x0, by = r.y1;
        break;
      case DL:
        bx = r.x0, by = (cp == CL) ? r.y1 : r.y0;
        break;
      case DU:
        bx = (cp == CL) ? r.x0 : r.x1, by = r.y0;
        break;
    }
    bx += dx, by += dy;
    if (bx >= 0 && bx < w && by >= 0 && by < h && bids[by][bx] >= 0)
      bp = bids[by][bx];
    else if (cp == CL)
      cp = CR;
    else
      dp = (dp + 1) & 3, cp = CL;
    ips[++m] = IP(bp, dp, cp);
    if (ds[bp][dp][cp] >= 0) break;
    ds[bp][dp][cp] = dm + 1;
  }
  if (m == n)
    printf("%c\n", chs[ips[m].bp]);
  else {
    IP &ip = ips[m];
    int a = ds[ip.bp][ip.dp][ip.cp], b = m - a;
    int k = (n - a) % b + a;
    printf("%c\n", chs[ips[k].bp]);
  }
  return 0;
}
