#include <bits/stdc++.h>
using namespace std;
const int inf = 1 << 28;
const double INF = 1e12, EPS = 1e-9;
const int dy[] = {-1, 0, 1, 0}, 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;
  for (;;) {
    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;
  }
  for (;;) {
    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] =
               make_pair(make_pair(cy, cx), make_pair(cdp, ccp));
  }
  return memo[y][x][dp][cp] = make_pair(make_pair(ny, nx), make_pair(dp, cp));
}
void run() {
  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;
}
int main() { run(); }
