#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
double PI = 4 * atan(1);
char grid[50][50];
int xdir[4] = {1, 0, -1, 0}, ydir[4] = {0, 1, 0, -1};
map<pair<pair<int, int>, pair<int, int> >, int> tim;
pair<int, int> cpos;
int d = 0, c = 1, m, n, m1;
bool first = 0;
void move() {
  while (1) {
    pair<int, int> k = {cpos.first + xdir[d], cpos.second + ydir[d]};
    if (k.first < 0 || k.first >= m1 || k.second < 0 || k.second >= m) break;
    if (grid[k.first][k.second] != grid[cpos.first][cpos.second]) break;
    cpos = k;
  }
  while (1) {
    pair<int, int> k = {cpos.first + xdir[c], cpos.second + ydir[c]};
    if (k.first < 0 || k.first >= m1 || k.second < 0 || k.second >= m) break;
    if (grid[k.first][k.second] != grid[cpos.first][cpos.second]) break;
    cpos = k;
  }
  pair<int, int> k = {cpos.first + xdir[d], cpos.second + ydir[d]};
  bool p = 1;
  if (k.first < 0 || k.first >= m1 || k.second < 0 || k.second >= m) p = 0;
  if (grid[k.first][k.second] == '0') p = 0;
  if (p)
    cpos = k;
  else {
    if ((c - d - 1) % 4 == 0)
      c = (d + 3) % 4;
    else {
      d = (d + 3) % 4;
      c = (d + 1) % 4;
    }
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> m >> n;
  cpos = {0, m - 1};
  for (int j = (m)-1; j >= 0; j--) {
    string x;
    cin >> x;
    m1 = x.length();
    for (int i = 0; i < m1; i++) grid[i][j] = x[i];
  }
  while (1) {
    if (!first && tim[{cpos, {d, c}}]) {
      int dif = tim[{cpos, {d, c}}] - n;
      while (n >= dif) n -= dif;
      first = 1;
    }
    if (n == 0) {
      cout << grid[cpos.first][cpos.second];
      return 0;
    }
    if (!first) tim[{cpos, {d, c}}] = n;
    move();
    n--;
  }
}
