#include <bits/stdc++.h>
using namespace std;
struct _nl {
} nl;
bool _ns = 0;
template <typename T>
void _p(const T& _) {
  if constexpr (is_same<T, _nl>()) {
    cout << "\n";
    _ns = 0;
  } else {
    if (_ns) cout << " ";
    cout << _;
    _ns = 1;
  }
}
template <typename... T>
void out(const T&... _) {
  (_p(_), ...);
}
int ii() {
  int _;
  cin >> _;
  return _;
}
string is() {
  string _;
  cin >> _;
  return _;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int h = ii();
  int n = ii();
  vector<string> grid;
  for (int i = 0; i < h; i++) grid.emplace_back(is());
  int w = grid[0].size();
  struct ptr {
    int x;
    int y;
    int dir;
  };
  vector<vector<vector<ptr>>> d(
      h, vector<vector<ptr>>(w, vector<ptr>(8, ptr{-1, -1})));
  int dx[] = {1, 1, 0, 0, -1, -1, 0, 0};
  int dy[] = {0, 0, 1, 1, 0, 0, -1, -1};
  int dx0[] = {0, 0, 1, -1, 0, 0, -1, 1};
  int dy0[] = {-1, 1, 0, 0, 1, -1, 0, 0};
  function<void(int, int, int)> dfs = [&](int x, int y, int dir) {
    if (d[y][x][dir].x != -1) return;
    int nx = x + dx[dir];
    int ny = y + dy[dir];
    if (nx < 0 || nx >= w || ny < 0 || ny >= h)
      d[y][x][dir] = ptr{x, y, (dir + 1) % 8};
    else {
      if (grid[y][x] == grid[ny][nx]) {
        dfs(nx, ny, dir);
        d[y][x][dir] = d[ny][nx][dir];
      } else {
        int ax = x + dx0[dir];
        int ay = y + dy0[dir];
        if (ax < 0 || ax >= w || ay < 0 || ay >= h) {
          if (grid[ny][nx] == '0') {
            d[y][x][dir] = ptr{x, y, (dir + 1) % 8};
          } else {
            d[y][x][dir] = ptr{nx, ny, dir};
          }
        } else {
          if (grid[y][x] == grid[ay][ax]) {
            dfs(ax, ay, dir);
            d[y][x][dir] = d[ay][ax][dir];
          } else {
            if (grid[ny][nx] == '0') {
              d[y][x][dir] = ptr{x, y, (dir + 1) % 8};
            } else
              d[y][x][dir] = ptr{nx, ny, dir};
          }
        }
      }
    }
  };
  for (int x = 0; x < w; x++) {
    for (int y = 0; y < h; y++) {
      for (int dir = 0; dir < 8; dir++) {
        dfs(x, y, dir);
      }
    }
  }
  auto p = ptr{0, 0, 0};
  for (int i = 0; i < n; i++) {
    p = d[p.y][p.x][p.dir];
  }
  out(grid[p.y][p.x], nl);
  return 0;
}
