#include <bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:20000000")
string problem_name = "a";
void init() { freopen((problem_name + ".in").c_str(), "rt", stdin); }
struct tpoint {
  int x, y;
};
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
tpoint bp;
int dp, cp;
char mas[55][55];
tpoint last[55][55][4];
tpoint inf;
int n, m;
int cmp(tpoint &a, tpoint &b) { return (a.x == b.x && a.y == b.y); }
tpoint go(tpoint p, int d, char col) {
  if (p.x < 0 || p.y < 0 || p.x >= n || p.y >= m) return inf;
  if (mas[p.x][p.y] != col) return inf;
  if (!cmp(last[p.x][p.y][d], inf)) return last[p.x][p.y][d];
  tpoint t = p;
  t.x += dx[d];
  t.y += dy[d];
  tpoint res = go(t, d, col);
  if (cmp(res, inf))
    return last[p.x][p.y][d] = p;
  else
    return last[p.x][p.y][d] = res;
}
int check(tpoint p) {
  if (p.x >= n || p.y >= m || p.x < 0 || p.y < 0 || mas[p.x][p.y] == '0')
    return 0;
  return 1;
}
int main() {
  inf.x = 111;
  inf.y = 111;
  for (int i = 0; i < 50; i++)
    for (int j = 0; j < 50; j++)
      for (int k = 0; k < 4; k++) last[i][j][k] = inf;
  int steps;
  scanf("%d %d\n", &n, &steps);
  for (int i = 0; i < n; i++) gets(mas[i]);
  m = strlen(mas[0]);
  bp.x = 0;
  bp.y = 0;
  dp = 0;
  cp = -1;
  while (steps--) {
    bp = go(bp, dp, mas[bp.x][bp.y]);
    bp = go(bp, (dp + cp + 4) % 4, mas[bp.x][bp.y]);
    tpoint t = bp;
    t.x += dx[dp];
    t.y += dy[dp];
    if (check(t)) {
      bp = t;
    } else {
      if (cp < 0)
        cp = 1;
      else {
        cp = -1;
        dp++;
        if (dp >= 4) dp = 0;
      }
    }
  }
  printf("%c\n", mas[bp.x][bp.y]);
  return 0;
}
