#include <bits/stdc++.h>
using namespace std;
struct CPoint {
  int x, y;
};
struct Point {
  int x, y;
  Point(){};
  Point(int tx, int ty) {
    x = tx;
    y = ty;
  }
};
struct State {
  int dp, cp;
};
const CPoint dt[4] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
const State st[8] = {{0, 3}, {0, 1}, {1, 0}, {1, 2},
                     {2, 1}, {2, 3}, {3, 2}, {3, 0}};
struct Block {
  int color;
  Point corner[8];
};
int m, n;
char f[100][100];
int id[100][100];
int bn;
Block block[10000];
int main() {
  int cid, cst;
  cin >> m >> n;
  gets(f[1] + 1);
  for (int i = 1; i <= m; i++) {
    gets(f[i] + 1);
    for (int j = 1; f[i][j] != 0; j++) f[i][j] -= '0';
  }
  for (int i = 1; i <= m; i++) {
    for (int j = 1; j <= 50; j++) {
      Point p;
      if (f[i][j]) {
        int color = f[i][j];
        bn++;
        block[bn].color = color;
        for (int ii = i; f[ii][j] == color; ii++) {
          for (int jj = j; f[ii][jj] == color; jj++) {
            f[ii][jj] = 0;
            id[ii][jj] = bn;
          }
        }
        p = Point(j, i);
        for (int k = 0; k < 8; k++) {
          Point dp = Point(dt[st[k].dp].x, dt[st[k].dp].y);
          Point cp = Point(dt[st[k].cp].x, dt[st[k].cp].y);
          while (id[p.y + dp.y][p.x + dp.x] == id[p.y][p.x])
            p = Point(p.x + dp.x, p.y + dp.y);
          while (id[p.y + cp.y][p.x + cp.x] == id[p.y][p.x])
            p = Point(p.x + cp.x, p.y + cp.y);
          block[bn].corner[k] = p;
        }
      }
    }
  }
  cid = 1;
  cst = 0;
  for (int i = 0; i < n; i++) {
    Point p, dp;
    p = block[cid].corner[cst];
    dp = Point(dt[st[cst].dp].x, dt[st[cst].dp].y);
    if (id[p.y + dp.y][p.x + dp.x] == 0) {
      cst = (cst + 1) % 8;
    } else {
      cid = id[p.y + dp.y][p.x + dp.x];
    }
  }
  cout << block[cid].color;
  return 0;
}
