#include <bits/stdc++.h>
using namespace std;
struct Block {
  int r1, c1, r2, c2;
  int state[8];
  void get(int& r, int& c, int st) {
    switch (st) {
      case 7:
      case 0:
        r = r1;
        c = c2;
        break;
      case 1:
      case 2:
        r = r2;
        c = c2;
        break;
      case 3:
      case 4:
        r = r2;
        c = c1;
        break;
      case 5:
      case 6:
        r = r1;
        c = c1;
        break;
    }
  }
};
string field[100];
int vis[100][100];
int R, C;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
int q[100][100];
void go(int r, int c, int& r1, int& c1, int& r2, int& c2) {
  r1 = min(r1, r);
  r2 = max(r2, r);
  c1 = min(c1, c);
  c2 = max(c2, c);
  vis[r][c] = true;
  for (int i = (0); i < (4); ++i) {
    int rr = r + dx[i];
    int cc = c + dy[i];
    if (rr < 0 || rr >= R || cc < 0 || cc >= C || field[r][c] != field[rr][cc])
      continue;
    if (!vis[rr][cc]) go(rr, cc, r1, c1, r2, c2);
  }
}
int main() {
  int moves;
  cin >> R >> moves;
  for (int i = (0); i < (R); ++i) cin >> field[i];
  C = field[0].size();
  vector<Block> vb;
  memset(vis, 0, sizeof vis);
  memset(q, -1, sizeof q);
  for (int r = (0); r < (R); ++r)
    for (int c = (0); c < (C); ++c)
      if (field[r][c] != '0' && !vis[r][c]) {
        Block b;
        b.r1 = b.r2 = r;
        b.c1 = b.c2 = c;
        go(r, c, b.r1, b.c1, b.r2, b.c2);
        int id = vb.size();
        for (int r = (b.r1); r <= (b.r2); ++r)
          for (int c = (b.c1); c <= (b.c2); ++c) q[r][c] = id;
        vb.push_back(b);
      }
  for (int i = (0); i < (vb.size()); ++i) {
    int r, c;
    for (int j = (0); j < (8); ++j) {
      vb[i].get(r, c, j);
      int rr = r + dy[j / 2];
      int cc = c + dx[j / 2];
      if (rr < 0 || rr >= R || cc < 0 || cc >= C || q[rr][cc] == -1 ||
          q[rr][cc] == q[r][c])
        vb[i].state[j] = -1;
      else
        vb[i].state[j] = q[rr][cc];
    }
  }
  int curs = 0;
  int id = 0;
  for (int i = (0); i < (moves); ++i) {
    if (vb[id].state[curs] == -1)
      curs = (curs + 1) & 7;
    else
      id = vb[id].state[curs];
  }
  cout << field[vb[id].r1][vb[id].c1] << endl;
}
