#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1.0);
struct bound {
  int l, r, t, b;
  bound(int l, int r, int t, int b) : l(l), r(r), t(t), b(b) {}
  bound() {}
};
int n, m, w;
int s[100][100];
int dir[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
void solve() {
  bound B[100][100];
  char v[100][100];
  memset(v, 0, sizeof(v));
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      if (!v[i][j] && s[i][j] != 0) {
        int l = 1000, r = 0, t = 1000, b = 0;
        vector<pair<int, int> > p;
        queue<pair<int, int> > q;
        q.push(make_pair(i, j));
        v[i][j] = 1;
        while (!q.empty()) {
          int x = q.front().first, y = q.front().second;
          l = min(l, x);
          r = max(r, x);
          t = min(t, y);
          b = max(b, y);
          p.push_back(q.front());
          q.pop();
          for (int k = 0; k < 4; k++) {
            int a = x + dir[k][0], b = y + dir[k][1];
            if (a > 0 && a <= n && b > 0 && b <= m && s[a][b] == s[x][y] &&
                !v[a][b]) {
              q.push(make_pair(a, b));
              v[a][b] = 1;
            }
          }
        }
        for (int i = 0; i < (int)p.size(); i++)
          B[p[i].first][p[i].second] = bound(t, b, l, r);
      }
  int x = 1, y = 1, dp = 1, cp = 3;
  for (int T = 0; T < w; T++) {
    int xx, yy;
    if (dp == 1) {
      xx = B[y][x].r + 1;
      if (cp == 3)
        yy = B[y][x].t;
      else
        yy = B[y][x].b;
    } else if (dp == 3) {
      xx = B[y][x].l - 1;
      if (cp == 3)
        yy = B[y][x].b;
      else
        yy = B[y][x].t;
    } else if (dp == 0) {
      yy = B[y][x].t - 1;
      if (cp == 3)
        xx = B[y][x].l;
      else
        xx = B[y][x].r;
    } else {
      yy = B[y][x].b + 1;
      if (cp == 3)
        xx = B[y][x].r;
      else
        xx = B[y][x].l;
    }
    if (s[yy][xx] != 0) {
      x = xx;
      y = yy;
    } else {
      if (cp == 3)
        cp = 1;
      else {
        cp = 3;
        dp = (dp + 1) % 4;
      }
    }
  }
  cout << s[y][x] << endl;
}
int main() {
  while (cin >> n >> w) {
    memset(s, 0, sizeof(s));
    for (int i = 1; i <= n; i++) {
      string t;
      cin >> t;
      m = t.size();
      for (int j = 1; j <= m; j++) s[i][j] = (t[j - 1] - '0');
    }
    solve();
  }
}
