#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:65777216")
using namespace std;
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
int n, m, t;
char s[55][55];
int c[55][55];
char color[55 * 55];
int per[55 * 55][5][5];
void dfs(int x, int y, int comp) {
  if (c[x][y] != -1) return;
  c[x][y] = comp;
  for (int dx(-1); dx < (2); dx++)
    for (int dy(-1); dy < (2); dy++)
      if (abs(dx) + abs(dy) == 1) {
        int nx = x + dx;
        int ny = y + dy;
        if (nx >= 0 && nx < n && ny >= 0 && ny < m && s[nx][ny] == s[x][y])
          dfs(nx, ny, comp);
      }
}
bool parsed[55 * 55];
int main() {
  cin >> n >> t;
  for (int i(0); i < (n); i++) cin >> s[i], m = strlen(s[i]);
  int comp = 0;
  memset(c, -1, sizeof(c));
  for (int i(0); i < (n); i++)
    for (int j(0); j < (m); j++)
      if (s[i][j] != '0' && c[i][j] == -1) {
        color[comp] = s[i][j];
        dfs(i, j, comp);
        comp++;
      }
  memset(per, -1, sizeof(per));
  for (int i(0); i < (n); i++)
    for (int j(0); j < (m); j++)
      if (c[i][j] != -1 && !parsed[c[i][j]]) {
        parsed[c[i][j]] = 1;
        for (int dp(0); dp < (4); dp++)
          for (int bc(0); bc < (4); bc++) {
            int x = i, y = j;
            while (1) {
              int nx = x + dx[dp];
              int ny = y + dy[dp];
              if (nx < 0 || nx >= n || ny < 0 || ny >= m ||
                  s[nx][ny] != s[x][y])
                break;
              x = nx, y = ny;
            }
            while (1) {
              int nx = x + dx[bc];
              int ny = y + dy[bc];
              if (nx < 0 || nx >= n || ny < 0 || ny >= m ||
                  s[nx][ny] != s[x][y])
                break;
              x = nx, y = ny;
            }
            x = x + dx[dp];
            y = y + dy[dp];
            if (x >= 0 && x < n && y >= 0 && y < m && s[x][y] != '0') {
              per[c[i][j]][dp][bc] = c[x][y];
            }
          }
      }
  int bpointer = 0;
  int dpointer = 0;
  int bchooser = 3;
  for (; t; t--) {
    int val = per[bpointer][dpointer][bchooser];
    if (val == -1) {
      if (bchooser == (dpointer + 3) % 4)
        bchooser = (dpointer + 1) % 4;
      else {
        dpointer = (dpointer + 1) % 4;
        bchooser = (dpointer + 3) % 4;
      }
    } else {
      bpointer = val;
    }
  }
  cout << color[bpointer] << endl;
  return 0;
}
