#include <bits/stdc++.h>
using namespace std;
vector<string> x;
void en(int state, int &dx, int &dy) {
  if (state / 2 % 2 == 0) {
    if (state % 2 == 0) {
      dx = 0;
    } else {
      dy = 0;
    }
  } else {
    if (state % 2 != 0) {
      dx = 0;
    } else {
      dy = 0;
    }
  }
}
void par(int state, int &dx, int &dy) {
  dx = -1;
  dy = -1;
  if (state / 2 == 0) {
    dx = 1;
    dy = 1;
  } else if (state / 2 == 1) {
    dx = 1;
  } else if (state / 2 == 3) {
    dy = 1;
  }
}
void step(pair<int, int> &k, int &state) {
  int dx, dy;
  par(state, dx, dy);
  while (x[k.first][k.second + dx] == x[k.first][k.second]) {
    k.second += dx;
  }
  while (x[k.first + dy][k.second] == x[k.first][k.second]) {
    k.first += dy;
  }
  en(state, dx, dy);
  if (x[k.first + dy][k.second + dx] == '0') {
    state++;
    if (state == 8) {
      state = 0;
    }
  } else {
    k.first += dy;
    k.second += dx;
  }
}
int main() {
  int n, m;
  cin >> m >> n;
  x = vector<string>(m + 2, "0");
  pair<int, int> bp(1, 1);
  int state = 1;
  getchar();
  for (int i = 1; i <= m; i++) {
    char c = getchar();
    while (c != '\n' && c != EOF) {
      x[i] += c;
      c = getchar();
    }
    x[i] += '0';
  }
  for (int i = 1; i < x[1].size(); i++) {
    x[0] += '0';
    x[m + 1] += '0';
  }
  for (int i = 0; i < n; i++) {
    step(bp, state);
  }
  cout << x[bp.first][bp.second];
  return 0;
}
