#include <bits/stdc++.h>
using namespace std;
struct IP {
  int BP, DP, CP;
  IP() {}
  IP(int _a, int _b, int _c) { BP = _a, DP = _b, CP = _c; }
};
const int _N = 55;
const int _LEFT = 0, _UP = 1, _RIGHT = 2, _DOWN = 3;
const int _CHOOSEL = 0, _CHOOSER = 1;
const int _D[4][2] = {{0, -1}, {-1, 0}, {0, 1}, {1, 0}};
int n, m, s, g[_N][_N], prtN, prt[_N][_N];
pair<int, int> prtMst[_N * _N][4];
IP mv[_N * _N][4][2];
bool legalMove(int i, int j) {
  return (0 <= i) && (i < n) && (0 <= j) && (j < m);
}
void updMst(int i, int j, int p) {
  if ((prtMst[p][_LEFT] == pair<int, int>(-1, -1)) ||
      (j < prtMst[p][_LEFT].second))
    prtMst[p][_LEFT] = pair<int, int>(i, j);
  if ((prtMst[p][_UP] == pair<int, int>(-1, -1)) || (i < prtMst[p][_UP].first))
    prtMst[p][_UP] = pair<int, int>(i, j);
  if ((prtMst[p][_RIGHT] == pair<int, int>(-1, -1)) ||
      (j > prtMst[p][_RIGHT].second))
    prtMst[p][_RIGHT] = pair<int, int>(i, j);
  if ((prtMst[p][_DOWN] == pair<int, int>(-1, -1)) ||
      (i > prtMst[p][_DOWN].first))
    prtMst[p][_DOWN] = pair<int, int>(i, j);
}
void dfs(int i, int j) {
  prt[i][j] = prtN;
  updMst(i, j, prtN);
  for (int d = 0; d < 4; d++) {
    int _i = i + _D[d][0], _j = j + _D[d][1];
    if (legalMove(_i, _j) && (g[_i][_j] == g[i][j]) && (prt[_i][_j] == -1))
      dfs(_i, _j);
  }
}
pair<int, int> moveFurt(int i, int j, int d) {
  while (true) {
    int _i = i + _D[d][0], _j = j + _D[d][1];
    if (legalMove(_i, _j) && (g[_i][_j] == g[i][j]))
      i = _i, j = _j;
    else
      break;
  }
  return pair<int, int>(i, j);
}
int main() {
  ios::sync_with_stdio(false);
  cin >> n >> s;
  for (int i = 0; i < n; i++) {
    string buf;
    cin >> buf;
    if (!i) m = buf.length();
    for (int j = 0; j < m; j++) g[i][j] = buf[j] - '0';
  }
  memset(prt, -1, sizeof(prt));
  prtN = 0;
  memset(prtMst, -1, sizeof(prtMst));
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      if ((prt[i][j] == -1) && g[i][j]) {
        dfs(i, j);
        prtN++;
      }
  for (int i = 0; i < prtN; i++)
    for (int j = 0; j < 4; j++)
      for (int k = 0; k < 2; k++) {
        pair<int, int> p =
            moveFurt(prtMst[i][j].first, prtMst[i][j].second,
                     (k == _CHOOSEL) ? ((j + 3) % 4) : ((j + 1) % 4));
        int _i = p.first + _D[j][0], _j = p.second + _D[j][1];
        if (legalMove(_i, _j) && (g[_i][_j] != 0))
          mv[i][j][k] = IP(prt[_i][_j], j, k);
        else {
          if (k == _CHOOSEL)
            mv[i][j][k] = IP(i, j, _CHOOSER);
          else
            mv[i][j][k] = IP(i, (j + 1) % 4, _CHOOSEL);
        }
      }
  IP cur = IP(0, _RIGHT, _CHOOSEL);
  for (int i = 0; i < s; i++) cur = mv[cur.BP][cur.DP][cur.CP];
  cout << g[prtMst[cur.BP][_LEFT].first][prtMst[cur.BP][_LEFT].second] << endl;
  return 0;
}
