#include <bits/stdc++.h>
using namespace std;
const int dy[] = {-1, 0, 1, 0};
const int dx[] = {0, 1, 0, -1};
int h, w, n;
string in[50];
pair<pair<int, int>, pair<int, int> > memo[50][50][4][2];
pair<pair<int, int>, pair<int, int> > next(int y, int x, int dp, int cp) {
  if (memo[y][x][dp][cp].first.first >= 0) return memo[y][x][dp][cp];
  int cy = y, cx = x, cdp = dp, ccp = cp,
      d = (cp ? (dp + 1) & 3 : (dp + 3) & 3);
  while (1) {
    int ny = cy + dy[dp], nx = cx + dx[dp];
    if (ny < 0 || nx < 0 || nx >= w || ny >= h || in[ny][nx] != in[cy][cx])
      break;
    cy = ny, cx = nx;
  }
  while (1) {
    int ny = cy + dy[d], nx = cx + dx[d];
    if (ny < 0 || nx < 0 || nx >= w || ny >= h || in[ny][nx] != in[cy][cx])
      break;
    cy = ny, cx = nx;
  }
  int ny = cy + dy[dp], nx = cx + dx[dp];
  if (ny < 0 || nx < 0 || nx >= w || ny >= h || in[ny][nx] == '0') {
    if (cp)
      ccp = 0, cdp = ((dp + 1) & 3);
    else
      ccp = 1;
    return memo[y][x][dp][cp] = pair<pair<int, int>, pair<int, int> >(
               pair<int, int>(cy, cx), pair<int, int>(cdp, ccp));
  }
  return memo[y][x][dp][cp] = pair<pair<int, int>, pair<int, int> >(
             pair<int, int>(ny, nx), pair<int, int>(dp, cp));
}
int main() {
  cin >> h >> n;
  for (int i = 0; i < (h); i++) cin >> in[i];
  w = in[0].size();
  memset(memo, -1, sizeof(memo));
  int y = 0, x = 0, dp = 1, cp = 0;
  for (int i = 0; i < (n); i++) {
    pair<pair<int, int>, pair<int, int> > nxt = next(y, x, dp, cp);
    y = nxt.first.first, x = nxt.first.second;
    dp = nxt.second.first, cp = nxt.second.second;
  }
  cout << in[y][x] << endl;
}
