#include <bits/stdc++.h>
using namespace std;
int n, m, K, br;
int V[60][60];
char buf[60];
int ID[60][60];
map<int, pair<int, int> > rep;
int dir[4][2] = {{0, 1}, {-1, 0}, {0, -1}, {1, 0}};
int n_c(int p) { return (p - 1 + 4) % 4; }
int n_cc(int p) { return (p + 1) % 4; }
void dfs(int x, int y) {
  for (int i = 0; i < 4; ++i) {
    int nx = x + dir[i][0], ny = y + dir[i][1];
    if (V[nx][ny] == V[x][y] && ID[nx][ny] == -1)
      ID[nx][ny] = ID[x][y], dfs(nx, ny);
  }
}
pair<int, pair<int, int> > go[2600][4][2];
pair<int, pair<int, int> > get_next(int x, int y, int d, int t) {
  while (ID[x][y] == ID[x + dir[d][0]][y + dir[d][1]])
    x += dir[d][0], y += dir[d][1];
  int d1 = (t == 0) ? n_cc(d) : n_c(d);
  while (ID[x][y] == ID[x + dir[d1][0]][y + dir[d1][1]])
    x += dir[d1][0], y += dir[d1][1];
  if (ID[x + dir[d][0]][y + dir[d][1]] != -1)
    return pair<int, pair<int, int> >(ID[x + dir[d][0]][y + dir[d][1]],
                                      pair<int, int>(d, t));
  if (t == 0) return pair<int, pair<int, int> >(ID[x][y], pair<int, int>(d, 1));
  return pair<int, pair<int, int> >(ID[x][y], pair<int, int>(n_c(d), 0));
}
int main() {
  scanf("%d %d\n", &n, &K);
  for (int i = 0; i < n; ++i) {
    gets(buf);
    m = strlen(buf);
    for (int j = 0; j < m; ++j) V[i + 1][j + 1] = buf[j] - '0';
  }
  memset(ID, -1, sizeof ID);
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j) {
      if (V[i][j] > 0 && ID[i][j] == -1) {
        ID[i][j] = br;
        rep[br++] = pair<int, int>(i, j);
        dfs(i, j);
      }
    }
  for (int i = 0; i < br; ++i)
    for (int j = 0; j < 4; ++j)
      for (int k = 0; k < 2; ++k)
        go[i][j][k] = get_next(rep[i].first, rep[i].second, j, k);
  int p = ID[1][1], d = 0, t = 0;
  while (K--) {
    pair<int, pair<int, int> > next = go[p][d][t];
    p = next.first;
    d = next.second.first;
    t = next.second.second;
  }
  cout << (char)('0' + V[rep[p].first][rep[p].second]) << '\n';
  return 0;
}
