#include <bits/stdc++.h>
using namespace std;
int n, m, i, j, l, k;
int h[4][2] = {0, 1, 1, 0, 0, -1, -1, 0}, dp, cp, bp;
int u[59][59];
char a[59][59];
int r[2509][5];
void dfs(int i, int j, char c) {
  if (i >= n || i < 0 || j >= m || j < 0) return;
  if (a[i][j] != c || u[i][j]) return;
  u[i][j] = l + 1;
  if (a[i + 1][j] != c) r[l][1] = i;
  if (a[i - 1][j] != c) r[l][3] = i;
  if (a[i][j + 1] != c) r[l][0] = j;
  if (a[i][j - 1] != c) r[l][2] = j;
  dfs(i + 1, j, c);
  dfs(i - 1, j, c);
  dfs(i, j + 1, c);
  dfs(i, j - 1, c);
}
void go(int &dp, int &cp, int &bp, int i) {
  int x, y;
  x = r[i][dp], y = r[i][cp];
  if (dp == 0 || dp == 2) swap(x, y);
  int d = 0;
  int dx = x + h[dp][0], dy = y + h[dp][1];
  if (dx >= 0 && dx < n && dy >= 0 && dy < m) d = u[dx][dy];
  if (d == 0) {
    if (cp == (dp + 3) % 4)
      cp = (dp + 1) % 4;
    else if (cp == (dp + 1) % 4) {
      dp = (dp + 1) % 4;
      cp = (dp + 3) % 4;
    }
  } else {
    bp = d;
  }
}
int main() {
  scanf("%d%d", &n, &k);
  for (i = 0; i < n; i++) cin >> a[i];
  m = strlen(a[0]);
  for (i = 0; i < n; i++) {
    for (j = 0; j < m; j++) {
      if (a[i][j] == '0' || u[i][j]) continue;
      dfs(i, j, a[i][j]);
      r[l++][4] = a[i][j];
    }
  }
  bp = 1;
  dp = 0;
  cp = 3;
  for (i = 0; i < k; i++) go(dp, cp, bp, bp - 1);
  printf("%d", r[bp - 1][4] - 48);
}
