#include <bits/stdc++.h>
using namespace std;
template <typename A, typename B>
ostream& operator<<(ostream& os, const pair<A, B>& p) {
  return os << '(' << p.first << ", " << p.second << ')';
}
const int inf = 0x3f3f3f3f;
const long long inf64 = 0x3f3f3f3f3f3f3f3fLL;
const double eps = 1e-5;
const int maxm = 55;
int history[maxm][maxm][4][4];
tuple<int, int, int, int> prenext[maxm][maxm][4][4];
int dir[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
string field[maxm];
int m, mm;
tuple<int, int, int, int> getnext(int row, int col, int dp, int cp) {
  int nrow = row + dir[dp][0];
  int ncol = col + dir[dp][1];
  if (0 <= nrow && nrow < m && 0 <= ncol && ncol < mm &&
      field[nrow][ncol] != '0')
    return tuple<int, int, int, int>(nrow, ncol, dp, cp);
  if ((cp + 1) % 4 == dp)
    return tuple<int, int, int, int>(row, col, dp, (dp + 1) % 4);
  dp++;
  dp %= 4;
  cp = (dp + 3) % 4;
  return tuple<int, int, int, int>(row, col, dp, cp);
}
tuple<int, int, int, int> getnext(tuple<int, int, int, int> ss) {
  return getnext(get<0>(ss), get<1>(ss), get<2>(ss), get<3>(ss));
}
int main() {
  ios_base::sync_with_stdio(false);
  memset(history, 0xc0, sizeof(history));
  int n;
  cin >> m >> n;
  for (int i = 0; i < m; i++) cin >> field[i];
  mm = (int((field[0]).size()));
  tuple<int, int, int, int> cur(0, 0, 0, 3);
  try {
    for (int iter = 0; iter < n;) {
      set<tuple<int, int, int, int> > localhist;
      int row = get<0>(cur);
      int col = get<1>(cur);
      int dd = get<2>(cur);
      int cc = get<3>(cur);
      int ololo = dd;
      for (int fuck = 0; fuck < 2; fuck++) {
        while (true) {
          int nrow = row + dir[ololo][0];
          int ncol = col + dir[ololo][1];
          if (nrow < 0 || nrow >= m || ncol < 0 || ncol >= mm) break;
          if (field[row][col] != field[nrow][ncol]) break;
          row = nrow;
          col = ncol;
        }
        ololo = cc;
      }
      if (history[row][col][dd][cc] >= 0) {
        int del = iter - history[row][col][dd][cc];
        if (iter == history[row][col][dd][cc]) throw(0);
        int add = max(0, (n - iter) / del - 2);
        iter += del * add;
        assert(iter < n);
      } else
        history[row][col][dd][cc] = iter;
      for (const tuple<int, int, int, int>& i : localhist) {
        history[get<0>(i)][get<1>(i)][get<2>(i)][get<3>(i)] = iter;
        prenext[get<0>(i)][get<1>(i)][get<2>(i)][get<3>(i)] = cur;
      }
      cur = getnext(row, col, dd, cc);
      if (field[row][col] == field[get<0>(cur)][get<1>(cur)] &&
          get<2>(cur) == dd && get<3>(cur) == cc)
        ;
      else
        iter++;
    }
  } catch (...) {
    {
      cerr << "1"
           << " = " << (1) << endl;
    };
  }
  cout << field[get<0>(cur)][get<1>(cur)] << endl;
  return 0;
}
