#include <bits/stdc++.h>
using namespace std;
int n, m, nrSteps;
int di[4] = {-1, 0, 1, 0}, dj[4] = {0, 1, 0, -1};
pair<int, int> corner[55][55][4][2];
char a[55][55];
bool used[55][55];
pair<int, int> move(pair<int, int> pos, int d) {
  pos.first += di[d];
  pos.second += dj[d];
  return pos;
}
bool valid(pair<int, int> pos) {
  int i = pos.first, j = pos.second;
  return i >= 1 && i <= n && j >= 1 && j <= m && a[i][j] > '0';
}
int main() {
  scanf("%d %d\n", &n, &nrSteps);
  for (int i = 1; i <= n; ++i) scanf("%s\n", a[i] + 1);
  m = strlen(a[1] + 1);
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j) {
      if (used[i][j] || a[i][j] == '0') continue;
      int i1 = i, j1 = j, i2, j2;
      for (i2 = i + 1; i2 <= n && a[i2][j] == a[i][j]; ++i2)
        ;
      --i2;
      for (j2 = j + 1; j2 <= m && a[i][j2] == a[i][j]; ++j2)
        ;
      --j2;
      for (int ii = i1; ii <= i2; ++ii)
        for (int jj = j1; jj <= j2; ++jj) {
          assert(a[ii][jj] == a[i][j]);
          used[ii][jj] = 1;
          corner[ii][jj][0][0] = corner[ii][jj][3][1] = make_pair(i1, j1);
          corner[ii][jj][3][0] = corner[ii][jj][2][1] = make_pair(i2, j1);
          corner[ii][jj][2][0] = corner[ii][jj][1][1] = make_pair(i2, j2);
          corner[ii][jj][1][0] = corner[ii][jj][0][1] = make_pair(i1, j2);
        }
    }
  pair<int, int> pos = make_pair(1, 1);
  int dp = 1, cp = 0;
  while (nrSteps--) {
    pos = corner[pos.first][pos.second][dp][cp];
    if (valid(move(pos, dp)))
      pos = move(pos, dp);
    else {
      if (cp == 0)
        cp = 1;
      else {
        cp = 0;
        ++dp;
        if (dp == 4) dp = 0;
      }
    }
  }
  printf("%c\n", a[pos.first][pos.second]);
  return 0;
}
