#include <bits/stdc++.h>
using namespace std;
enum direction { R, D, L, U };
int dr[] = {+0, +1, +0, -1};
int dc[] = {+1, +0, -1, +0};
int f[55][55];
bool v[55][55];
int l[55][55][4];
vector<pair<pair<int, int>, pair<int, int> > > bs;
void examine_next(int r, int c) {
  for (int k = 0; k < int(4); ++k) {
    int r1 = r + dr[k], c1 = c + dc[k];
    for (__typeof__((bs).begin()) it = ((bs).begin()); (it) != (bs).end();
         ++(it)) {
      if (it->first.first <= r1 && r1 <= it->second.first &&
          it->first.second <= c1 && c1 <= it->second.second)
        l[r][c][k] = it - bs.begin();
    }
  }
}
int main() {
  memset(l, 255, sizeof l);
  int m, m1, n;
  cin >> m >> n;
  for (int i = 0; i < int(m); ++i) {
    string s;
    cin >> s;
    m1 = s.size();
    for (int j = 0; j < int(m1); ++j) f[i][j] = s[j] - '0';
  }
  for (int i = 0; i < int(m); ++i)
    for (int j = 0; j < int(m1); ++j) {
      if (!v[i][j]) {
        if (!f[i][j]) continue;
        int i0 = i, j0 = j;
        while (i0 + 1 < m && f[i0 + 1][j] == f[i][j]) ++i0;
        while (j0 + 1 < m1 && f[i][j0 + 1] == f[i][j]) ++j0;
        for (int i1 = i; i1 <= i0; ++i1)
          for (int j1 = j; j1 <= j0; ++j1) v[i1][j1] = true;
        bs.push_back(pair<pair<int, int>, pair<int, int> >(
            pair<int, int>(i, j), pair<int, int>(i0, j0)));
      }
    }
  for (__typeof__((bs).begin()) it = ((bs).begin()); (it) != (bs).end();
       ++(it)) {
    examine_next(it->first.first, it->first.second);
    examine_next(it->first.first, it->second.second);
    examine_next(it->second.first, it->second.second);
    examine_next(it->second.first, it->first.second);
  }
  int bp = -1;
  direction dp = R, cp = L;
  for (__typeof__((bs).begin()) it = ((bs).begin()); (it) != (bs).end();
       ++(it)) {
    if (it->first.first <= 0 && 0 <= it->second.first &&
        it->first.second <= 0 && 0 <= it->second.second)
      bp = it - bs.begin();
  }
  assert(bp >= 0);
  for (int _ = 0; _ < int(n); ++_) {
    int r = -1, c = -1;
    if ((dp == L && cp == R) || (dp == U && cp == L))
      r = bs[bp].first.first, c = bs[bp].first.second;
    else if ((dp == U && cp == R) || (dp == R && cp == L))
      r = bs[bp].first.first, c = bs[bp].second.second;
    else if ((dp == R && cp == R) || (dp == D && cp == L))
      r = bs[bp].second.first, c = bs[bp].second.second;
    else if ((dp == D && cp == R) || (dp == L && cp == L))
      r = bs[bp].second.first, c = bs[bp].first.second;
    assert(r >= 0 && c >= 0);
    int bp1 = l[r][c][int(dp)];
    if (bp1 >= 0)
      bp = bp1;
    else if (cp == L)
      cp = R;
    else
      cp = L, dp = direction((int(dp) + 1) % 4);
  }
  cout << f[bs[bp].first.first][bs[bp].first.second] << endl;
}
