#include <bits/stdc++.h>
using namespace std;
const double EPS = 1e-10;
int enc(int x, int y, int dp, int cp) {
  int res = 0;
  res = x;
  res = res * 100 + y;
  res = res * 100 + dp;
  res = res * 100 + cp;
  return res;
}
int main() {
  char buf[128][128];
  int h, n;
  scanf("%d%d", &h, &n);
  for (int i = 0; i < h; ++i) scanf("%s", buf[i]);
  int w = strlen(buf[0]);
  int pix[128][128];
  memset(pix, 0, sizeof(pix));
  for (int i = 0; i < h; ++i)
    for (int j = 0; j < w; ++j) pix[i + 1][j + 1] = buf[i][j] - '0';
  const int dx[] = {1, 0, -1, 0};
  const int dy[] = {0, 1, 0, -1};
  pair<int, int> dd[128][128][4];
  for (int y = 1; y <= h; ++y) {
    for (int x = 1; x <= w; ++x) {
      if (pix[y][x] == 0) continue;
      for (int d = 0; d < 4; ++d) {
        int tx = x, ty = y;
        while (true) {
          int nx = tx + dx[d], ny = ty + dy[d];
          if (pix[ny][nx] != pix[ty][tx]) break;
          tx = nx, ty = ny;
        }
        dd[y][x][d] = pair<int, int>(tx, ty);
      }
    }
  }
  int x, y, dp, cp;
  x = y = 1;
  dp = 0;
  cp = -1;
  map<int, int> memo;
  memo.insert(pair<int, int>(enc(x, y, dp, cp), 0));
  bool skip = false;
  for (int move = 1; move <= n; ++move) {
    int xe, ye;
    pair<int, int> t = dd[y][x][dp];
    xe = t.first, ye = t.second;
    int nx, ny;
    t = dd[ye][xe][(dp + cp + 4) % 4];
    nx = t.first, ny = t.second;
    int tx, ty;
    tx = nx + dx[dp], ty = ny + dy[dp];
    if (pix[ty][tx]) {
      x = tx, y = ty;
    } else {
      if (cp == 1) dp = (dp + 1) % 4;
      cp *= -1;
    }
    int en = enc(x, y, dp, cp);
    if (!skip) {
      if (!memo.count(en))
        memo[en] = move;
      else {
        int around = move - memo[en];
        move += around * ((n - move) / around);
        skip = true;
      }
    }
  }
  printf("%d\n", pix[y][x]);
}
