#include <bits/stdc++.h>
using namespace std;
int m, n, l, b[55][55], v[2600][4][2], dy[] = {0, 1, 0, -1},
                                       dx[] = {1, 0, -1, 0};
int L[2600], D[2600], R[2600], U[2600];
string s[55];
void f(int y, int x, int c, int id) {
  if (b[y][x]) return;
  b[y][x] = id;
  for (int i = 0; i < 4; i++) {
    int ny = y + dy[i], nx = x + dx[i];
    if (0 <= ny && ny < m && 0 <= nx && nx < l && s[ny][nx] == c)
      f(ny, nx, c, id);
  }
}
int main() {
  cin >> m >> n;
  for (int i = 0; i < m; i++) cin >> s[i];
  l = s[0].size();
  int id = 1;
  for (int i = 0; i < m; i++)
    for (int j = 0; j < l; j++)
      if (s[i][j] != '0' && !b[i][j]) {
        f(i, j, s[i][j], id++);
      }
  fill(U, U + 2600, 1000000007LL);
  fill(L, L + 2600, 1000000007LL);
  for (int i = 0; i < m; i++)
    for (int j = 0; j < l; j++) {
      U[b[i][j]] = min(U[b[i][j]], i);
      D[b[i][j]] = max(D[b[i][j]], i);
      L[b[i][j]] = min(L[b[i][j]], j);
      R[b[i][j]] = max(R[b[i][j]], j);
    }
  int bp = 1, dp = 0, cp = 0;
  v[1][0][0] = n;
  while (1) {
    if (!n) {
      cout << s[U[bp]][L[bp]];
      return 0;
    }
    int ny, nx;
    if (dp == 0) {
      if (cp == 0)
        ny = U[bp], nx = R[bp] + 1;
      else
        ny = D[bp], nx = R[bp] + 1;
    }
    if (dp == 1) {
      if (cp == 0)
        ny = D[bp] + 1, nx = R[bp];
      else
        ny = D[bp] + 1, nx = L[bp];
    }
    if (dp == 2) {
      if (cp == 0)
        ny = D[bp], nx = L[bp] - 1;
      else
        ny = U[bp], nx = L[bp] - 1;
    }
    if (dp == 3) {
      if (cp == 0)
        ny = U[bp] - 1, nx = L[bp];
      else
        ny = U[bp] - 1, nx = R[bp];
    }
    if (ny < 0 || m <= ny || nx < 0 || l <= nx || s[ny][nx] == '0') {
      cp++;
      if (cp == 2) cp = 0, dp++;
      if (dp == 4) dp = 0;
    } else
      bp = b[ny][nx];
    n--;
    if (v[bp][dp][cp]) {
      int cy = v[bp][dp][cp] - n;
      n %= cy;
    }
    v[bp][dp][cp] = n;
  }
}
