#include <bits/stdc++.h>
using namespace std;
const int MAXS = 50;
const int MAXD = 4;
const int MAXC = 2;
int n, R, C;
int color[MAXS][MAXS];
int nxRow[MAXS][MAXS][MAXD][MAXC];
int nxCol[MAXS][MAXS][MAXD][MAXC];
int drPtr[MAXS][MAXS][MAXD][MAXC];
int codel[MAXS][MAXS][MAXD][MAXC];
void read();
void init();
bool valid(int, int);
int getdr(int);
int getdc(int);
int getdr(int, int);
int getdc(int, int);
int getColor();
int main() {
  read();
  init();
  cout << getColor() << endl;
  return 0;
}
void read() {
  cin >> R >> n;
  for (int r = 0; r < R; r++) {
    string row;
    cin >> row;
    C = row.size();
    for (int c = 0; c < C; c++) color[r][c] = row[c] - '0';
  }
}
void init() {
  for (int r = 0; r < R; r++) {
    for (int c = 0; c < C; c++) {
      int val = color[r][c];
      for (int dir = 0; dir < MAXD; dir++) {
        for (int cod = 0; cod < MAXC; cod++) {
          int row = r;
          int col = c;
          int dr = getdr(dir);
          int dc = getdc(dir);
          while (true) {
            if (valid(row, col) && color[row][col] == val) {
              row += dr;
              col += dc;
            } else
              break;
          }
          row -= dr;
          col -= dc;
          dr = getdr(dir, cod);
          dc = getdc(dir, cod);
          while (true) {
            if (valid(row, col) && color[row][col] == val) {
              row += dr;
              col += dc;
            } else
              break;
          }
          row -= dr;
          col -= dc;
          dr = getdr(dir);
          dc = getdc(dir);
          row += dr;
          col += dc;
          if (valid(row, col) && color[row][col] > 0) {
            nxRow[r][c][dir][cod] = row;
            nxCol[r][c][dir][cod] = col;
            drPtr[r][c][dir][cod] = dir;
            codel[r][c][dir][cod] = cod;
          } else {
            nxRow[r][c][dir][cod] = r;
            nxCol[r][c][dir][cod] = c;
            if (cod == 0) {
              codel[r][c][dir][cod] = 1;
              drPtr[r][c][dir][cod] = dir;
            } else {
              codel[r][c][dir][cod] = 0;
              drPtr[r][c][dir][cod] = (dir + 1) % MAXD;
            }
          }
        }
      }
    }
  }
}
bool valid(int row, int col) {
  return (row >= 0 && col >= 0 && row < R && col < C);
}
int getdr(int dir) {
  if (dir == 1)
    return +1;
  else if (dir == 3)
    return -1;
  else
    return 0;
}
int getdc(int dir) {
  if (dir == 0)
    return +1;
  else if (dir == 2)
    return -1;
  else
    return 0;
}
int getdr(int dir, int cod) {
  if (dir == 0) {
    if (cod == 0)
      return -1;
    else
      return +1;
  } else if (dir == 2) {
    if (cod == 0)
      return +1;
    else
      return -1;
  } else
    return 0;
}
int getdc(int dir, int cod) {
  if (dir == 1) {
    if (cod == 0)
      return +1;
    else
      return -1;
  } else if (dir == 3) {
    if (cod == 0)
      return -1;
    else
      return +1;
  } else
    return 0;
}
int getColor() {
  int row = 0;
  int col = 0;
  int dir = 0;
  int cod = 0;
  while (n > 0) {
    int r = nxRow[row][col][dir][cod];
    int c = nxCol[row][col][dir][cod];
    int d = drPtr[row][col][dir][cod];
    cod = codel[row][col][dir][cod];
    row = r;
    col = c;
    dir = d;
    n--;
  }
  return color[row][col];
}
