#include <bits/stdc++.h>
using namespace std;
vector<string> x;
int main() {
  int n, m;
  cin >> m >> n;
  x = vector<string>(m + 2, "0");
  pair<int, int> bp(1, 1);
  int dp = 2;
  bool cp = false;
  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++) {
    if (dp % 2 == 0) {
      while (x[bp.first][bp.second + dp - 1] == x[bp.first][bp.second]) {
        bp.second += dp - 1;
      }
      if (x[bp.first][bp.second + dp - 1] == '0') {
        if (cp) {
          dp++;
          dp %= 4;
        }
        cp = !cp;
      } else {
        bp.second += dp - 1;
      }
    } else {
      while (x[bp.first + dp - 2][bp.second] == x[bp.first][bp.second]) {
        bp.first += dp - 2;
      }
      if (x[bp.first + dp - 2][bp.second] == '0') {
        if (cp) {
          dp++;
          dp %= 4;
        }
        cp = !cp;
      } else {
        bp.first += dp - 2;
      }
    }
  }
  cout << x[bp.first][bp.second];
  return 0;
}
