#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int> > minCorners;
vector<pair<int, int> > maxCorners;
void dfs(int i, int j, vector<string> const& prg, vector<vector<int> >& blocks,
         int numBlock, char color) {
  if (i < 0 || j < 0 || i >= (int)prg.size() || j >= (int)prg[i].size()) return;
  if (prg[i][j] != color || blocks[i][j] != -1) return;
  pair<int, int> const newCorner = make_pair(i, j);
  {
    {
      pair<int, int>& oldCorner = minCorners[numBlock];
      if (newCorner < oldCorner) oldCorner = newCorner;
    }
    {
      pair<int, int>& oldCorner = maxCorners[numBlock];
      if (newCorner > oldCorner) oldCorner = newCorner;
    }
  }
  blocks[i][j] = numBlock;
  dfs(i - 1, j, prg, blocks, numBlock, color);
  dfs(i + 1, j, prg, blocks, numBlock, color);
  dfs(i, j - 1, prg, blocks, numBlock, color);
  dfs(i, j + 1, prg, blocks, numBlock, color);
}
pair<int, int> getCorner(int bp, int dp, int cp,
                         vector<vector<int> > const& blocks) {
  int dirCode = 2 * dp + cp;
  if (dirCode == 0 || dirCode == 7) {
    return minCorners[bp];
  } else if (dirCode == 1 || dirCode == 2) {
    int i = minCorners[bp].first;
    int j = maxCorners[bp].second;
    return make_pair(i, j);
  } else if (dirCode == 5 || dirCode == 6) {
    int i = maxCorners[bp].first;
    int j = minCorners[bp].second;
    return make_pair(i, j);
  } else if (dirCode == 3 || dirCode == 4) {
    return maxCorners[bp];
  }
  throw "fuck";
}
int main() {
  ios_base::sync_with_stdio(false);
  int m, n;
  cin >> m >> n;
  vector<string> prg(m);
  for (int i = (0); i < ((m)); ++i) cin >> prg[i];
  maxCorners.resize(2500);
  minCorners.resize(2500);
  int numBlocks = 0;
  vector<vector<int> > blocks(m, vector<int>((int)prg[0].size(), -1));
  for (int i = (0); i < (((int)prg.size())); ++i)
    for (int j = (0); j < (((int)prg[i].size())); ++j)
      if (prg[i][j] != '0') {
        minCorners[numBlocks] = pair<int, int>(1e9, 1e9);
        maxCorners[numBlocks] = pair<int, int>(-1, -1);
        dfs(i, j, prg, blocks, numBlocks, prg[i][j]);
        ++numBlocks;
      }
  int const DI[] = {-1, 0, 1, 0};
  int const DJ[] = {0, 1, 0, -1};
  int bp = 0, dp = 1, cp = 0;
  for (int i = (0); i < ((n)); ++i) {
    pair<int, int> nextCorner = getCorner(bp, dp, cp, blocks);
    nextCorner.first += DI[dp];
    nextCorner.second += DJ[dp];
    if (nextCorner.first < 0 || nextCorner.first >= (int)prg.size() ||
        nextCorner.second < 0 || nextCorner.second >= (int)prg[0].size() ||
        prg[nextCorner.first][nextCorner.second] == '0') {
      if (cp == 0)
        cp = 1;
      else {
        cp = 0;
        dp = (dp + 1) % 4;
      }
    } else {
      bp = blocks[nextCorner.first][nextCorner.second];
    }
  }
  pair<int, int> const& corner = minCorners[bp];
  cout << prg[corner.first][corner.second] << endl;
  return 0;
}
