#include <bits/stdc++.h>
using namespace std;
enum LR { LEFT, RIGHT };
struct State {
  int x, y;
  int dx, dy;
  LR cp;
};
void Turn(LR lr, int dx, int dy, int* dx2, int* dy2) {
  switch (lr) {
    case LEFT:
      *dx2 = dy;
      *dy2 = -dx;
      break;
    case RIGHT:
      *dx2 = -dy;
      *dy2 = dx;
      break;
  }
}
char getChar(const vector<string>& ls, int x, int y) {
  const int N = ls.size();
  const int M = ls[0].size();
  if (0 <= x && 0 <= y && x < M && y < N)
    return ls[y][x];
  else
    return '0';
}
void movePoint(const vector<string>& ls, int* px, int* py, int dx, int dy) {
  int& x = *px;
  int& y = *py;
  const char c0 = ls[y][x];
  while (1) {
    const char c = getChar(ls, x + dx, y + dy);
    if (c != c0) break;
    x += dx;
    y += dy;
  }
}
State getNext(const vector<string>& ls, const State& s) {
  State s2 = s;
  int x = s.x;
  int y = s.y;
  movePoint(ls, &x, &y, s2.dx, s2.dy);
  int dx2, dy2;
  Turn(s2.cp, s2.dx, s2.dy, &dx2, &dy2);
  movePoint(ls, &x, &y, dx2, dy2);
  {
    const char c = getChar(ls, x + s2.dx, y + s2.dy);
    if (c != '0') {
      x += s2.dx;
      y += s2.dy;
      s2.x = x;
      s2.y = y;
    } else {
      if (s2.cp == LEFT) {
        s2.cp = RIGHT;
      } else {
        s2.cp = LEFT;
        Turn(RIGHT, s2.dx, s2.dy, &s2.dx, &s2.dy);
      }
    }
    return s2;
  }
}
bool operator==(const State& s1, const State& s2) {
  return s1.x == s2.x && s1.y == s2.y && s1.dx == s2.dx && s1.dy == s2.dy &&
         s1.cp == s2.cp;
}
bool operator<(const State& s1, const State& s2) {
  if (s1.x < s2.x) return true;
  if (s1.x > s2.x) return false;
  if (s1.y < s2.y) return true;
  if (s1.y > s2.y) return false;
  if (s1.dx < s2.dx) return true;
  if (s1.dx > s2.dx) return false;
  if (s1.dy < s2.dy) return true;
  if (s1.dy > s2.dy) return false;
  if (s1.cp < s2.cp) return true;
  return false;
}
int main(int argc, char* argv[]) {
  std::string l;
  getline(cin, l);
  istringstream iss(l);
  int m, n;
  iss >> m >> n;
  vector<string> ls;
  for (int i = 0; i != m; ++i) {
    getline(cin, l);
    ls.push_back(l);
  }
  State s;
  s.x = 0;
  s.y = 0;
  s.dx = 1;
  s.dy = 0;
  s.cp = LEFT;
  map<State, int> ss;
  int step = 0;
  State s0 = s;
  for (; step != n; ++step) {
    ss[s] = step;
    s = getNext(ls, s);
    if (ss.count(s)) {
      ++step;
      s0 = s;
      break;
    }
  }
  const int T0 = ss[s0];
  const int T = step - T0;
  step = n - ((n - T0) % T);
  for (; step != n; ++step) {
    s = getNext(ls, s);
  }
  cout << ls[s.y][s.x] << endl;
  return 0;
}
