#include <bits/stdc++.h>
using namespace std;
static const double EPS = 1e-8;
int memo[4][52][52][4][2];
void update(vector<string> &pix, int &BPr, int &BPc, int &DP, int &CP) {
  int nBPr, nBPc, nDP, nCP;
  if (memo[0][BPr][BPc][DP][CP] >= 0) {
    nBPr = memo[0][BPr][BPc][DP][CP];
    nBPc = memo[1][BPr][BPc][DP][CP];
    nDP = memo[2][BPr][BPc][DP][CP];
    nCP = memo[3][BPr][BPc][DP][CP];
    BPr = nBPr;
    BPc = nBPc;
    DP = nDP;
    CP = nCP;
    return;
  }
  int h = pix.size(), w = pix[0].size();
  int dr[4] = {0, 1, 0, -1}, dc[4] = {1, 0, -1, 0};
  nBPr = BPr + dr[DP];
  nBPc = BPc + dc[DP];
  nDP = DP;
  nCP = CP;
  if (nBPr >= 0 && nBPr < h && nBPc >= 0 && nBPc < w &&
      pix[nBPr][nBPc] != '0') {
    int r = nBPr, c = nBPc;
    while (1) {
      int nr = r + dr[DP], nc = c + dc[DP];
      if (nr < 0 || nr >= h || nc < 0 || nc >= w) break;
      if (pix[r][c] != pix[nr][nc]) break;
      r = nr;
      c = nc;
      nBPr = r;
      nBPc = c;
    }
    r = nBPr;
    c = nBPc;
    int dir;
    if (CP == 0)
      dir = (DP + 3) % 4;
    else
      dir = (DP + 1) % 4;
    while (1) {
      int nr = r + dr[dir], nc = c + dc[dir];
      if (nr < 0 || nr >= h || nc < 0 || nc >= w) break;
      if (pix[r][c] != pix[nr][nc]) break;
      r = nr;
      c = nc;
      nBPr = r;
      nBPc = c;
    }
    memo[0][BPr][BPc][DP][CP] = nBPr;
    memo[1][BPr][BPc][DP][CP] = nBPc;
    memo[2][BPr][BPc][DP][CP] = nDP;
    memo[3][BPr][BPc][DP][CP] = nCP;
    BPr = nBPr;
    BPc = nBPc;
    DP = nDP;
    CP = nCP;
    return;
  }
  if (CP == 1) {
    nBPr = BPr;
    nBPc = BPc;
    nDP = (DP + 1) % 4;
    nCP = 0;
    memo[0][BPr][BPc][DP][CP] = nBPr;
    memo[1][BPr][BPc][DP][CP] = nBPc;
    memo[2][BPr][BPc][DP][CP] = nDP;
    memo[3][BPr][BPc][DP][CP] = nCP;
    BPr = nBPr;
    BPc = nBPc;
    DP = nDP;
    CP = nCP;
    return;
  }
  int dir = (DP + 1) % 4;
  nBPr = BPr;
  nBPc = BPc;
  nDP = DP;
  nCP = 1;
  int r = BPr, c = BPc;
  while (1) {
    int nr = r + dr[dir], nc = c + dc[dir];
    if (nr < 0 || nr >= h || nc < 0 || nc >= w) break;
    if (pix[r][c] != pix[nr][nc]) break;
    r = nr;
    c = nc;
    nBPr = r;
    nBPc = c;
  }
  memo[0][BPr][BPc][DP][CP] = nBPr;
  memo[1][BPr][BPc][DP][CP] = nBPc;
  memo[2][BPr][BPc][DP][CP] = nDP;
  memo[3][BPr][BPc][DP][CP] = nCP;
  BPr = nBPr;
  BPc = nBPc;
  DP = nDP;
  CP = nCP;
}
int main() {
  istream &fin = cin;
  ostream &fout = cout;
  FILE *fpin = stdin;
  FILE *fpout = stdout;
  int m, n;
  fin >> m >> n;
  vector<string> pix(m);
  for (int i = 0; i < m; i++) fin >> pix[i];
  int w = pix[0].size();
  int BPr = 0, BPc = 0, DP = 0, CP = 0;
  for (int i = 1; i < w; i++) {
    if (pix[0][i] != pix[0][i - 1]) break;
    BPc = i;
  }
  memset(memo, -1, sizeof(memo));
  for (int i = 0; i < n; i++) {
    update(pix, BPr, BPc, DP, CP);
  }
  fout << pix[BPr][BPc] << endl;
}
