#include <bits/stdc++.h>
using namespace std;
template <class T>
inline T checkmin(T &a, T b) {
  return (a < b) ? a : a = b;
}
template <class T>
inline T checkmax(T &a, T b) {
  return (a > b) ? a : a = b;
}
template <class T>
T GCD(T a, T b) {
  if (a < 0) return GCD(-a, b);
  if (b < 0) return GCD(a, -b);
  return (a == 0) ? b : GCD(b % a, a);
}
template <class T>
T LCM(T a, T b) {
  if (a < 0) return LCM(-a, b);
  if (b < 0) return LCM(a, -b);
  return (a == 0 || b == 0) ? 0 : a / GCD(a, b) * b;
}
template <class T>
T ExtGCD(T a, T b, T &x, T &y) {
  if (a < 0) {
    T c = ExtGCD(-a, b, x, y);
    x = -x;
    return c;
  }
  if (b < 0) {
    T c = ExtGCD(a, -b, x, y);
    y = -y;
    return c;
  }
  if (a == 0) {
    x = 0, y = 1;
    return b;
  } else {
    T c = ExtGCD(b % a, a, y, x);
    x -= b / a * y;
    return c;
  }
}
template <class T>
inline T sqr(T X) {
  return X * X;
}
namespace Poor {
const int MaxiN = 55;
const int TotDir = 4;
const int Dx[TotDir] = {0, 1, 0, -1};
const int Dy[TotDir] = {1, 0, -1, 0};
int N, M, K;
string Map[MaxiN];
map<pair<pair<int, int>, pair<int, int> >, int> Memo;
bool InRange(int X, int Y) { return X >= 0 && X < N && Y >= 0 && Y < M; }
void Run() {
  cin >> N >> K;
  for (int i = 0; i < N; ++i) cin >> Map[i];
  M = Map[0].size();
  int X = 0, Y = 0;
  int DP = 0, CP = -1;
  for (int i = 1; i <= K; ++i) {
    int p1 = DP, p2 = (DP + CP + TotDir) % TotDir;
    int NX = X, NY = Y;
    while (InRange(NX + Dx[p1], NY + Dy[p1]) &&
           Map[X][Y] == Map[NX + Dx[p1]][NY + Dy[p1]])
      NX += Dx[p1], NY += Dy[p1];
    while (InRange(NX + Dx[p2], NY + Dy[p2]) &&
           Map[X][Y] == Map[NX + Dx[p2]][NY + Dy[p2]])
      NX += Dx[p2], NY += Dy[p2];
    NX += Dx[p1], NY += Dy[p1];
    if (InRange(NX, NY) && Map[NX][NY] != '0') {
      X = NX, Y = NY;
    } else {
      if (CP == -1)
        CP = 1;
      else {
        CP = -1;
        DP = (DP + 1) % TotDir;
      }
    }
    if (Memo.count(make_pair(make_pair(X, Y), make_pair(DP, CP)))) {
      int t = i - Memo[make_pair(make_pair(X, Y), make_pair(DP, CP))];
      int k = (K - i) / t;
      i += k * t;
    } else
      Memo[make_pair(make_pair(X, Y), make_pair(DP, CP))] = i;
  }
  cout << Map[X][Y] << endl;
}
}  // namespace Poor
int main() {
  Poor::Run();
  return 0;
}
