#include <bits/stdc++.h>
using namespace std;
const int MAXN = (1 << 6) + 1;
const int NIL = -1;
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};
char board[MAXN][MAXN];
int bcount[4][2][MAXN][MAXN];
int bfirst[4][2][MAXN][MAXN];
int n, m;
pair<int, int> goSameColor(pair<int, int> f, int dir, char c) {
  int first = f.first, second = f.second;
  while (c == board[first][second]) {
    first += dx[dir];
    second += dy[dir];
  }
  first -= dx[dir];
  second -= dy[dir];
  if (0)
    cout << "goSameColor(" << f.first << ", " << f.second << ", " << dir << ", "
         << c << "): "
         << "x"
         << ":" << (first) << " "
         << "y"
         << ":" << (second) << " " << endl;
  return pair<int, int>(first, second);
}
pair<int, int> go(pair<int, int> f, int dir, int choose, int steps) {
  if (0)
    cout << "go: " << f.first << "," << f.second << " "
         << "steps"
         << ":" << (steps) << " "
         << "dir"
         << ":" << (dir) << " "
         << "choose"
         << ":" << (choose) << " "
         << " f:" << (bfirst[dir][choose][f.first][f.second])
         << " c:" << (bcount[dir][choose][f.first][f.second]) << endl;
  char c = board[f.first][f.second];
  f = goSameColor(f, 0, c);
  f = goSameColor(f, 1, c);
  int first = f.first, second = f.second;
  if (0)
    cout << "go: " << f.first << "," << f.second << " "
         << "steps"
         << ":" << (steps) << " "
         << "dir"
         << ":" << (dir) << " "
         << "choose"
         << ":" << (choose) << " "
         << " f:" << (bfirst[dir][choose][f.first][f.second])
         << " c:" << (bcount[dir][choose][f.first][f.second]) << endl;
  if (steps == 0) return f;
  if (bfirst[dir][choose][first][second] == NIL) {
    bfirst[dir][choose][first][second] = steps;
  } else if (bcount[dir][choose][first][second] == NIL) {
    bcount[dir][choose][first][second] =
        bfirst[dir][choose][first][second] - steps;
  } else {
    if (0) cout << "haha" << endl;
    steps %= bcount[dir][choose][first][second];
    if (steps == 0) return f;
  }
  f = goSameColor(f, dir, c);
  int d = (dir + ((choose == 0) ? 3 : 1)) % 4;
  f = goSameColor(f, d, c);
  first = f.first + dx[dir], second = f.second + dy[dir];
  if (board[first][second] != '0') {
    return go(pair<int, int>(first, second), dir, choose, steps - 1);
  } else {
    if (choose == 0) {
      return go(pair<int, int>(f.first, f.second), dir, 1, steps - 1);
    } else {
      return go(pair<int, int>(f.first, f.second), (dir + 1) % 4, 0, steps - 1);
    }
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  int k;
  scanf("%d%d", &n, &k);
  for (int i = (0); i < (n); ++i) {
    scanf("%s", board[i + 1] + 1);
    m = strlen(board[i + 1] + 1);
    board[i + 1][0] = board[i + 1][m + 1] = '0';
    board[i + 1][m + 2] = 0;
  }
  for (int i = (0); i < (m + 2); ++i) board[0][i] = board[n + 1][i] = '0';
  for (int d = (0); d < (4); ++d)
    for (int c = (0); c < (2); ++c)
      for (int i = (0); i < (n + 2); ++i)
        for (int j = (0); j < (m + 2); ++j)
          bcount[d][c][i][j] = bfirst[d][c][i][j] = NIL;
  pair<int, int> res = go(pair<int, int>(1, 1), 0, 0, k);
  printf("%c\n", board[res.first][res.second]);
  return 0;
}
