#include <bits/stdc++.h>
using namespace std;
int m, n;
char mp[54][54];
int memo[54][54][4][3];
int main() {
  while (cin >> m >> n) {
    memset(mp, 0, sizeof(mp));
    for (int i = 1; i <= m; ++i) {
      cin >> (mp[i] + 1);
    }
    int W = strlen(mp[1] + 1);
    for (int i = 0; i <= m + 1; ++i) {
      for (int j = 0; j <= W + 1; ++j) {
        if (mp[i][j] == '0') {
          mp[i][j] = 0;
        }
      }
    }
    memset(memo, -1, sizeof(memo));
    int r, c, dp, cp;
    r = c = 1;
    dp = 2;
    cp = -1;
    const int dx[] = {-1, 0, 1, 0};
    const int dy[] = {0, -1, 0, 1};
    bool jump = false;
    for (int i = 1; i <= n;) {
      char cur = mp[r][c];
      while (mp[r + dy[dp]][c + dx[dp]] == cur) {
        r += dy[dp];
        c += dx[dp];
      }
      while (mp[r + dy[(dp + cp + 4) % 4]][c + dx[(dp + cp + 4) % 4]] == cur) {
        r += dy[(dp + cp + 4) % 4];
        c += dx[(dp + cp + 4) % 4];
      }
      if (mp[r + dy[dp]][c + dx[dp]] == 0) {
        if (cp == -1)
          cp = 1;
        else {
          cp = -1;
          dp = (dp + 1) % 4;
        }
      } else {
        r += dy[dp];
        c += dx[dp];
      }
      if (jump) {
        i++;
        continue;
      }
      int& res = memo[r][c][dp][cp + 1];
      if (res == -1)
        res = i;
      else {
        jump = true;
        i = n - (n - i) % (i - res);
      }
      i++;
    }
    cout << mp[r][c] << endl;
  }
}
