#include <bits/stdc++.h>
using namespace std;
const int INF = 100000;
int n, m, k;
int p[51][51], clr[51][51];
int ti[2600][4], tj[2600][4];
int dxy[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
void dfs(int x, int y, int tclr) {
  clr[x][y] = tclr;
  int nx, ny;
  ti[tclr][0] = min(x, ti[tclr][0]);
  ti[tclr][1] = min(x, ti[tclr][1]);
  ti[tclr][2] = max(x, ti[tclr][2]);
  ti[tclr][3] = max(x, ti[tclr][3]);
  tj[tclr][0] = min(y, tj[tclr][0]);
  tj[tclr][1] = max(y, tj[tclr][1]);
  tj[tclr][2] = max(y, tj[tclr][2]);
  tj[tclr][3] = min(y, tj[tclr][3]);
  for (int i = 0; i < 4; ++i) {
    nx = x + dxy[i][0];
    ny = y + dxy[i][1];
    if (nx >= 0 && ny >= 0 && nx < n && ny < m && p[x][y] == p[nx][ny] &&
        clr[nx][ny] == 0)
      dfs(nx, ny, tclr);
  }
}
int nap[4][2] = {{1, 2}, {2, 3}, {3, 0}, {0, 1}};
int main() {
  for (int i = 0; i < 2600; ++i) {
    ti[i][0] = INF;
    ti[i][1] = INF;
    ti[i][2] = -INF;
    ti[i][3] = -INF;
    tj[i][0] = INF;
    tj[i][1] = -INF;
    tj[i][2] = -INF;
    tj[i][3] = INF;
  }
  string s;
  char tch;
  cin >> n >> k;
  getline(cin, s);
  getline(cin, s);
  m = s.size();
  for (int j = 0; j < m; ++j) p[0][j] = s[j] - '0';
  for (int i = 1; i < n; ++i) {
    getline(cin, s);
    for (int j = 0; j < m; ++j) p[i][j] = s[j] - '0';
  }
  int tclr = 1;
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < m; ++j)
      if (p[i][j] > 0 && clr[i][j] == 0) {
        dfs(i, j, tclr);
        tclr++;
      }
  int dp = 0, cp = 0, x = 0, y = 0, tx, ty, nx, ny;
  for (int i = 0; i < k; ++i) {
    tx = ti[clr[x][y]][nap[dp][cp]];
    ty = tj[clr[x][y]][nap[dp][cp]];
    nx = tx + dxy[dp][0];
    ny = ty + dxy[dp][1];
    if (nx >= 0 && ny >= 0 && nx < n && ny < m && clr[nx][ny] != 0)
      x = nx, y = ny;
    else {
      if (cp == 0)
        cp = 1;
      else {
        cp = 0;
        dp = (dp + 1) % 4;
      }
    }
  }
  cout << p[x][y];
  return 0;
}
