#include <bits/stdc++.h>
using namespace std;
char m[55][55];
struct T {
  int i, j, k, l;
  T(int i = 0, int j = 0, int k = 0, int l = 0) : i(i), j(j), k(k), l(l) {}
};
T g[55][55][4][2];
int di[] = {-1, 0, 1, 0}, dj[] = {0, 1, 0, -1};
int r, c;
bool in(int i, int j) { return i >= 0 && i < r && j >= 0 && j < c; }
int main() {
  int k, i, j, l, p;
  scanf("%d%d", &r, &p);
  for (i = 0; i < r; scanf("%s", m[i]), i++)
    ;
  c = strlen(m[0]);
  for (i = 0; i < r; i++)
    for (j = 0; j < c; j++)
      if (m[i][j] != '0')
        for (k = 0; k < 4; k++) {
          for (l = 0; l < 2; l++) {
            int ii = i, jj = j;
            for (;;) {
              ii += di[k];
              jj += dj[k];
              if (!in(ii, jj) || m[ii][jj] != m[i][j]) break;
            }
            ii -= di[k];
            jj -= dj[k];
            int h;
            if (l == 0)
              h = (k + 3) % 4;
            else
              h = (k + 1) % 4;
            for (;;) {
              ii += di[h];
              jj += dj[h];
              if (!in(ii, jj) || m[ii][jj] != m[i][j]) break;
            }
            ii -= di[h];
            jj -= dj[h];
            ii += di[k];
            jj += dj[k];
            if (in(ii, jj) && m[ii][jj] != '0') {
              g[i][j][k][l] = T(ii, jj, k, l);
            } else {
              ii -= di[k];
              jj -= dj[k];
              if (l == 0) {
                g[i][j][k][l] = T(ii, jj, k, 1);
              } else {
                g[i][j][k][l] = T(ii, jj, (k + 1) % 4, 0);
              }
            }
          }
        }
  T t;
  t = T(0, 0, 1, 0);
  for (; p--;) t = g[t.i][t.j][t.k][t.l];
  printf("%c\n", m[t.i][t.j]);
  return 0;
}
