#include <bits/stdc++.h>
using namespace std;
struct pointer;
string board[60];
int alr[60][60][10];
int m, n;
int c;
struct pointer {
  int cx, cy;
  int w;
  pointer() {
    cx = 0, cy = 0;
    w = 0;
  }
  pointer(int x, int y, int wa) {
    cx = x;
    cy = y;
    w = wa;
  }
  pointer next(pointer nx[60][60][10]) {
    if (alr[cx][cy][w]) {
      alr[cx][cy][w]++;
      return nx[cx][cy][w];
    }
    alr[cx][cy][w]++;
    if (w == 0) {
      int y = cy;
      while (y + 1 < c && board[cx][y + 1] == board[cx][cy]) y++;
      int x = cx;
      while (x > 0 && board[x - 1][cy] == board[cx][cy]) x--;
      if (y + 1 < c && board[x][y + 1] > '0') {
        nx[cx][cy][w] = pointer(x, y + 1, w);
        return nx[cx][cy][w];
      } else {
        nx[cx][cy][w] = pointer(cx, cy, w + 1);
        return nx[cx][cy][w];
      }
    } else if (w == 1) {
      int y = cy;
      while (y + 1 < c && board[cx][y + 1] == board[cx][cy]) y++;
      int x = cx;
      while (x + 1 < m && board[x + 1][cy] == board[cx][cy]) x++;
      if (y + 1 < c && board[x][y + 1] > '0') {
        nx[cx][cy][w] = pointer(x, y + 1, w);
        return nx[cx][cy][w];
      } else {
        nx[cx][cy][w] = pointer(cx, cy, w + 1);
        return nx[cx][cy][w];
      }
    } else if (w == 2) {
      int y = cy;
      while (y + 1 < c && board[cx][y + 1] == board[cx][cy]) y++;
      int x = cx;
      while (x + 1 < m && board[x + 1][cy] == board[cx][cy]) x++;
      if (x + 1 < m && board[x + 1][y] > '0') {
        nx[cx][cy][w] = pointer(x + 1, y, w);
        return nx[cx][cy][w];
      } else {
        nx[cx][cy][w] = pointer(cx, cy, w + 1);
        return nx[cx][cy][w];
      }
    } else if (w == 3) {
      int y = cy;
      while (y > 0 && board[cx][y - 1] == board[cx][cy]) y--;
      int x = cx;
      while (x + 1 < m && board[x + 1][cy] == board[cx][cy]) x++;
      if (x + 1 < m && board[x + 1][y] > '0') {
        nx[cx][cy][w] = pointer(x + 1, y, w);
        return nx[cx][cy][w];
      } else {
        nx[cx][cy][w] = pointer(cx, cy, w + 1);
        return nx[cx][cy][w];
      }
    } else if (w == 4) {
      int y = cy;
      while (y > 0 && board[cx][y - 1] == board[cx][cy]) y--;
      int x = cx;
      while (x + 1 < m && board[x + 1][cy] == board[cx][cy]) x++;
      if (y > 0 && board[x][y - 1] > '0') {
        nx[cx][cy][w] = pointer(x, y - 1, w);
        return nx[cx][cy][w];
      } else {
        nx[cx][cy][w] = pointer(cx, cy, w + 1);
        return nx[cx][cy][w];
      }
    } else if (w == 5) {
      int y = cy;
      while (y > 0 && board[cx][y - 1] == board[cx][cy]) y--;
      int x = cx;
      while (x > 0 && board[x - 1][cy] == board[cx][cy]) x--;
      if (y > 0 && board[x][y - 1] > '0') {
        nx[cx][cy][w] = pointer(x, y - 1, w);
        return nx[cx][cy][w];
      } else {
        nx[cx][cy][w] = pointer(cx, cy, w + 1);
        return nx[cx][cy][w];
      }
    } else if (w == 6) {
      int y = cy;
      while (y > 0 && board[cx][y - 1] == board[cx][cy]) y--;
      int x = cx;
      while (x > 0 && board[x - 1][cy] == board[cx][cy]) x--;
      if (x > 0 && board[x - 1][y] > '0') {
        nx[cx][cy][w] = pointer(x - 1, y, w);
        return nx[cx][cy][w];
      } else {
        nx[cx][cy][w] = pointer(cx, cy, w + 1);
        return nx[cx][cy][w];
      }
    } else if (w == 7) {
      int y = cy;
      while (y + 1 < c && board[cx][y + 1] == board[cx][cy]) y++;
      int x = cx;
      while (x > 0 && board[x - 1][cy] == board[cx][cy]) x--;
      if (x > 0 && board[x - 1][y] > '0') {
        nx[cx][cy][w] = pointer(x - 1, y, w);
        return nx[cx][cy][w];
      } else {
        nx[cx][cy][w] = pointer(cx, cy, (w + 1) % 8);
        return nx[cx][cy][w];
      }
    }
  }
};
pointer nx[60][60][10];
int main() {
  cin >> m >> n;
  for (int i = 0; i < m; i++) cin >> board[i];
  c = board[0].size();
  int cyclen = 0;
  pointer cur;
  while (n > 0) {
    pointer prev = cur;
    if (alr[prev.cx][prev.cy][prev.w]) {
      while (1) {
        prev = cur;
        if (alr[prev.cx][prev.cy][prev.w] == 2) break;
        cur = cur.next(nx);
        cyclen++;
      }
      n %= cyclen;
      while (n > 0) {
        n--;
        cur = cur.next(nx);
      }
      printf("%d\n", board[cur.cx][cur.cy] - '0');
      return 0;
    }
    cur = cur.next(nx);
    n--;
  }
  printf("%d\n", board[cur.cx][cur.cy] - '0');
}
