#include <bits/stdc++.h>
using namespace std;
int dir[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
int vis[55][55][5][5];
int map[55][55];
int m, n, l;
struct Item {
  int dp, cp, r, c, tr, tc;
  void next() {
    while (1) {
      tr = r + dir[dp][0];
      tc = c + dir[dp][1];
      if (tr < 0 || tr >= m || tc < 0 || tc >= l) break;
      if (map[tr][tc] != map[r][c]) break;
      r = tr;
      c = tc;
    }
    int t = dp;
    dp = cp == 0 ? (dp + 3) % 4 : (dp + 1) % 4;
    while (1) {
      tr = r + dir[dp][0];
      tc = c + dir[dp][1];
      if (tr < 0 || tr >= m || tc < 0 || tc >= l) break;
      if (map[tr][tc] != map[r][c]) break;
      r = tr;
      c = tc;
    }
    dp = t;
    tr = r + dir[dp][0];
    tc = c + dir[dp][1];
    if (tr < 0 || tr >= m || tc < 0 || tc >= l || map[tr][tc] == 0) {
      if (cp == 0)
        cp = 1;
      else {
        cp = 0;
        dp = (dp + 1) % 4;
      }
    } else {
      r = tr;
      c = tc;
    }
  }
  int visId() { return vis[r][c][dp][cp]; }
  void setVis(int id) { vis[r][c][dp][cp] = id; }
};
int main() {
  char tmp[55];
  cin >> m >> n;
  for (int i = 0; i < m; i++) {
    cin >> tmp;
    if (i == 0) l = strlen(tmp);
    for (int j = 0; j < l; j++) map[i][j] = tmp[j] - '0';
  }
  Item step;
  int id = 1;
  memset(vis, -1, sizeof(vis));
  step.cp = step.dp = 0;
  step.r = step.c = 0;
  step.setVis(id);
  while (n--) {
    step.next();
    if (step.visId() == -1) {
      step.setVis(id++);
      continue;
    }
    n = n % (id - step.visId());
    break;
  }
  n = max(n, 0);
  while (n--) step.next();
  cout << map[step.r][step.c] << endl;
  return 0;
}
