#include <bits/stdc++.h>
using namespace std;
void file_open() {
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
}
const int di[] = {-1, 0, 1, 0}, dj[] = {0, 1, 0, -1},
          dk[4][2] = {3, 1, 0, 2, 1, 3, 2, 0};
const int MAX = 60;
typedef int kieu[MAX][MAX];
struct point {
  int x, y;
};
point tinh(kieu a, int i, int j, int dir) {
  int t = a[i][j];
  int u = i, v = j;
  while (a[u][v] == t) {
    u += di[dir];
    v += dj[dir];
  }
  u -= di[dir];
  v -= dj[dir];
  point tmp = {u, v};
  return tmp;
}
int main() {
  int m, n, u, v;
  kieu a = {};
  point d[MAX][MAX][4] = {};
  int tr[MAX][MAX][8] = {};
  scanf("%d%d\n", &m, &n);
  u = v = 0;
  for (int i = 1; i <= m; ++i) {
    for (int j = 1; j <= m; ++j) {
      scanf("%c", &a[i][j]);
      a[i][j] -= '0';
      if (a[i][j] && u == 0) {
        u = i;
        v = j;
      }
    }
    scanf("\n");
  }
  for (int i = 1; i <= m; ++i)
    for (int j = 1; j <= m; ++j)
      for (int k = 0; k < 4; ++k) d[i][j][k] = tinh(a, i, j, k);
  int bp, dp, cp, cost, cycle = 0;
  bp = a[u][v];
  dp = 1;
  cp = 0;
  tr[u][v][dp * 2 + cp] = cost = 1;
  while (n) {
    int r1, r2, c1, c2, k;
    k = dk[dp][cp];
    r1 = d[u][v][dp].x;
    c1 = d[u][v][dp].y;
    r2 = d[r1][c1][k].x;
    c2 = d[r1][c1][k].y;
    r1 = r2 + di[dp];
    c1 = c2 + dj[dp];
    if (a[r1][c1] == 0) {
      r1 -= di[dp];
      c1 -= dj[dp];
      dp = (dp + cp) % 4;
      cp = (cp + 1) % 2;
    }
    u = r1;
    v = c1;
    bp = a[u][v];
    --n;
    if (!cycle) {
      ++cost;
      if (tr[u][v][dp * 2 + cp]) {
        n = n % (cost - tr[u][v][dp * 2 + cp]);
        cycle = 1;
      } else {
        tr[u][v][dp * 2 + cp] = cost;
      }
    }
  }
  printf("%d", bp);
  return 0;
}
