#include <bits/stdc++.h>
using namespace std;
int edge[50 * 50 * 4 * 2][30];
char colour[50][51];
static int const dx[] = {-1, 0, 1, 0};
static int const dy[] = {0, -1, 0, 1};
int main() {
  int n, m;
  scanf("%d %d ", &n, &m);
  for (int i = 0; i < n; i++) scanf("%s ", colour[i]);
  int w = strlen(colour[0]);
  for (int i = 0; i < n; i++)
    for (int j = 0; j < w; j++)
      for (int k = 0; k < 4; k++)
        for (int l = 0; l < 2; l++) {
          int ax = i + dx[k];
          int ay = j + dy[k];
          int k2 = (k + l * 2 + 3) % 4;
          while (ax >= 0 and ay >= 0 and ax < n and ay < w and
                 colour[ax][ay] == colour[i][j])
            ax += dx[k], ay += dy[k];
          ax -= dx[k], ay -= dy[k];
          while (ax >= 0 and ay >= 0 and ax < n and ay < w and
                 colour[ax][ay] == colour[i][j])
            ax += dx[k2], ay += dy[k2];
          ax += dx[k] - dx[k2];
          ay += dy[k] - dy[k2];
          bool ok = (ax >= 0 and ay >= 0 and ax < n and ay < w and
                     colour[ax][ay] != '0');
          int &e = edge[(i * 50 + j) * 8 + k * 2 + l][0];
          if (ok) {
            e = (ax * 50 + ay) * 8 + k * 2 + l;
          } else {
            if (l) {
              e = (i * 50 + j) * 8 + k * 2 + 0;
            } else {
              e = (i * 50 + j) * 8 + ((k + 3) % 4) * 2 + 1;
            }
          }
        }
  int on = 3 * 2 + 1;
  for (int i = 0; i < 29; i++)
    for (int j = 50 * 50 * 4 * 2; j--;) edge[j][i + 1] = edge[edge[j][i]][i];
  for (int i = 0; m; i++, m >>= 1)
    if (m & 1) {
      on = edge[on][i];
    }
  on /= 8;
  printf("%c\n", colour[on / 50][on % 50]);
}
