#include <bits/stdc++.h>
using namespace std;
int i, j, d, c, x, y, m, n, k, t, tX, tY, iCount;
char s[64][64];
int f[64][64][4][2];
int dX[4] = {-1, 0, 1, 0};
int dY[4] = {0, 1, 0, -1};
int main() {
  memset(f, 0, sizeof(f));
  scanf("%d %d\n", &m, &k);
  for (i = 0; i < m; ++i) scanf("%s", s[i]);
  n = strlen(s[0]);
  ++k;
  iCount = 1;
  x = 0;
  y = 0;
  d = 1;
  c = 0;
  f[x][y][d][c] = 1;
  do {
    tX = x;
    tY = y;
    i = tX;
    j = tY;
    while (true) {
      i += dX[d];
      j += dY[d];
      if (i >= m || i < 0 || j >= n || j < 0) break;
      if (s[i][j] == s[x][y]) {
        tX = i;
        tY = j;
      }
    }
    if (c == 0)
      t = (d + 3) % 4;
    else
      t = (d + 1) % 4;
    i = tX;
    j = tY;
    while (true) {
      i += dX[d];
      j += dY[d];
      if (i >= m || i < 0 || j >= n || j < 0) break;
      if (s[i][j] == s[x][y]) {
        tX = i;
        tY = j;
      }
    }
    i = tX + dX[d];
    j = tY + dY[d];
    if (i < m && i >= 0 && j < n && j >= 0 && s[i][j] != '0') {
      x = i;
      y = j;
    } else {
      if (c == 0)
        c = 1;
      else {
        c = 0;
        d = (d + 1) % 4;
      }
    }
    ++iCount;
    if (f[x][y][d][c] == 0)
      f[x][y][d][c] = iCount;
    else
      break;
    if (iCount == k) break;
  } while (true);
  if (iCount != k) {
    k -= iCount;
    t = iCount - f[x][y][d][c];
    k %= t;
    k += f[x][y][d][c];
    for (i = 0; i < m; ++i)
      for (j = 0; j < n; ++j)
        for (tX = 0; tX < 4; ++tX)
          for (tY = 0; tY < 2; ++tY)
            if (f[i][j][tX][tY] == k) {
              x = i;
              y = j;
              break;
            }
  }
  cout << s[x][y];
  return 0;
}
