#include <bits/stdc++.h>
using namespace std;
struct UnionFind {
  vector<int> uf, sz;
  int nc;
  UnionFind(int N) : uf(N), sz(N, 1), nc(N) {
    for (int i = 0; i < N; ++i) uf[i] = i;
  }
  int size() { return nc; }
  int Find(int a) { return uf[a] == a ? a : uf[a] = Find(uf[a]); }
  bool Union(int a, int b) {
    a = Find(a);
    b = Find(b);
    if (a != b) {
      if (sz[a] >= sz[b]) swap(a, b);
      uf[a] = b;
      sz[b] += sz[a];
      --nc;
    }
    return (a != b);
  }
};
static const int dy[] = {0, +1, 0, -1};
static const int dx[] = {+1, 0, -1, 0};
int main() {
  int H, n;
  while (cin >> H >> n) {
    vector<string> field(H);
    for (auto& f : field) cin >> f;
    int W = field[0].size();
    UnionFind uf(H * W);
    for (int y = 0; y < H; ++y)
      for (int x = 0; x < W; ++x)
        if (field[y][x] != '0')
          for (int d = 0; d < 4; ++d) {
            int yy = y + dy[d];
            int xx = x + dx[d];
            if (0 <= yy && yy < H && 0 <= xx && xx < W &&
                field[yy][xx] != '0') {
              if (field[y][x] == field[yy][xx])
                uf.Union(y * W + x, yy * W + xx);
            }
          }
    vector<int> mx(H * W), MX(H * W), my(H * W), MY(H * W);
    for (int y = 0; y < H; ++y)
      for (int x = 0; x < W; ++x)
        if (field[y][x] != '0') {
          mx[y * W + x] = MX[y * W + x] = x;
          my[y * W + x] = MY[y * W + x] = y;
        }
    for (int y = 0; y < H; ++y)
      for (int x = 0; x < W; ++x)
        if (field[y][x] != '0') {
          int rr = uf.Find(y * W + x);
          mx[rr] = min(mx[rr], x);
          MX[rr] = max(MX[rr], x);
          my[rr] = min(my[rr], y);
          MY[rr] = max(MY[rr], y);
        }
    vector<int> next(H * W * 4 * 2);
    for (int y = 0; y < H; ++y)
      for (int x = 0; x < W; ++x)
        if (field[y][x] != '0') {
          int rr = uf.Find(y * W + x);
          if (rr == y * W + x) {
            for (int d = 0; d < 4; ++d)
              for (int c = 0; c < 2; ++c) {
                int me = (rr * 4 + d) * 2 + c;
                int yy = y, xx = x;
                while (field[yy][xx] == field[y][x]) {
                  yy += dy[d], xx += dx[d];
                  if (!(0 <= yy && yy < H && 0 <= xx && xx < W &&
                        field[yy][xx] != '0'))
                    break;
                }
                switch (d) {
                  case 0:
                    yy = (c == 0 ? my[rr] : MY[rr]);
                    break;
                  case 1:
                    xx = (c == 0 ? MX[rr] : mx[rr]);
                    break;
                  case 2:
                    yy = (c == 0 ? MY[rr] : my[rr]);
                    break;
                  case 3:
                    xx = (c == 0 ? mx[rr] : MX[rr]);
                    break;
                }
                if (!(0 <= yy && yy < H && 0 <= xx && xx < W &&
                      field[yy][xx] != '0'))
                  goto run_out;
                next[me] = (uf.Find(yy * W + xx) * 4 + d) * 2 + c;
                goto next_me;
              run_out:;
                next[me] = rr * 8 + (d * 2 + c + 1) % 8;
              next_me:;
              }
          }
        }
    int r = uf.Find(0) * 8;
    while (n-- > 0) r = next[r];
    cout << field[r / 8 / W][r / 8 % W] << endl;
  }
}
