#include <bits/stdc++.h>
using namespace std;
const int INF = 1000000000;
const double eps = 1e-9;
void prepare() {}
int n, m, num;
char a[55][55];
bool used[55][55];
struct Block {
  int color, lx, ly, rx, ry;
  vector<int> neig;
  Block(int c, int lx, int ly, int rx, int ry)
      : color(c), lx(lx), ly(ly), rx(rx), ry(ry) {}
};
vector<Block> blocks;
int blockNum[55][55];
void goNums() {
  memset((blockNum), (-1), sizeof(blockNum));
  int cnt = 0;
  for (int i = (1); i < (n + 1); i++) {
    for (int j = (1); j < (m + 1); j++) {
      if (blockNum[i][j] >= 0 || a[i][j] == '0') continue;
      int x = i, y = j;
      while (a[x + 1][j] == a[i][j]) x++;
      while (a[i][y + 1] == a[i][j]) y++;
      for (int xx = (i); xx < (x + 1); xx++) {
        for (int yy = (j); yy < (y + 1); yy++) blockNum[xx][yy] = cnt;
      }
      cnt++;
    }
  }
}
void goBlocks() {
  blocks.clear();
  for (int i = (1); i < (n + 1); i++) {
    for (int j = (1); j < (m + 1); j++) {
      if (used[i][j] || a[i][j] == '0') continue;
      int x = i, y = j;
      while (a[x + 1][j] == a[i][j]) x++;
      while (a[i][y + 1] == a[i][j]) y++;
      for (int xx = (i); xx < (x + 1); xx++) {
        for (int yy = (j); yy < (y + 1); yy++) used[xx][yy] = true;
      }
      Block tmp = Block(a[i][j] - '0', i, j, x, y);
      tmp.neig.push_back(blockNum[i][y + 1]);
      tmp.neig.push_back(blockNum[x][y + 1]);
      tmp.neig.push_back(blockNum[x + 1][y]);
      tmp.neig.push_back(blockNum[x + 1][j]);
      tmp.neig.push_back(blockNum[x][j - 1]);
      tmp.neig.push_back(blockNum[i][j - 1]);
      tmp.neig.push_back(blockNum[i - 1][j]);
      tmp.neig.push_back(blockNum[i - 1][y]);
      blocks.push_back(tmp);
    }
  }
}
int getNeigNum(int dp, int cp) {
  if (dp == 0) {
    if (cp == 3) return 0;
    return 1;
  }
  if (dp == 1) {
    if (cp == 0) return 2;
    return 3;
  }
  if (dp == 2) {
    if (cp == 1) return 4;
    return 5;
  }
  if (cp == 2) return 6;
  return 7;
}
int len[2505][4][4];
bool solve() {
  memset((used), (0), sizeof(used));
  if (scanf("%d", &n) <= 0) return false;
  scanf("%d", &num);
  for (int i = (0); i < ((n)); i++) scanf("%s", a[i + 1] + 1);
  m = strlen(a[1] + 1);
  goNums();
  goBlocks();
  int bp = 0, dp = 0, cp = 3;
  while (num > 0) {
    num--;
    int nn = getNeigNum(dp, cp);
    nn = blocks[bp].neig[nn];
    if (nn >= 0)
      bp = nn;
    else {
      if (((cp + 1) & 3) == dp)
        (cp += 2) &= 3;
      else {
        (cp += 3) &= 3;
        (++dp) &= 3;
      }
    }
  }
  printf("%d\n", blocks[bp].color);
  return true;
}
int main() {
  prepare();
  while (solve())
    ;
  return 0;
}
