#include <bits/stdc++.h>
using namespace std;
struct Node {
  int x, y;
};
string a[50];
int m, M, n, ans;
int used[50][50][4][2];
Node precalc[50][50][4];
int main() {
  cin >> m >> n;
  for (int i = 0; i < m; i++) cin >> a[i];
  M = a[0].size();
  for (int i = 0; i < m; i++)
    for (int j = 0; j < M; j++)
      if (a[i][j] != '0') {
        int l = j;
        while (l >= 0 && a[i][l] == a[i][j]) l--;
        l++;
        int r = j;
        while (r < M && a[i][r] == a[i][j]) r++;
        r--;
        int u = i;
        while (u >= 0 && a[u][j] == a[i][j]) u--;
        u++;
        int d = i;
        while (d < m && a[d][j] == a[i][j]) d++;
        d--;
        precalc[i][j][0].x = u;
        precalc[i][j][0].y = l;
        precalc[i][j][1].x = u;
        precalc[i][j][1].y = r;
        precalc[i][j][2].x = d;
        precalc[i][j][2].y = l;
        precalc[i][j][3].x = d;
        precalc[i][j][3].y = r;
      }
  memset(used, -1, sizeof(used));
  used[0][0][1][0] = 0;
  Node cur;
  int curx = 0, cury = 0, curDP = 1, curCP = 0;
  ans = n;
  for (int i = 1; i <= n; i++) {
    int nextx, nexty, nextDP, nextCP;
    if (curDP == 0 && curCP == 0) {
      nextx = precalc[curx][cury][0].x - 1;
      nexty = precalc[curx][cury][0].y;
      nextDP = curDP;
      nextCP = curCP;
      if (nextx < 0 || a[nextx][nexty] == '0') {
        nextx = curx;
        nexty = cury;
        nextDP = curDP;
        nextCP = 1 - curCP;
      }
    } else if (curDP == 0 && curCP == 1) {
      nextx = precalc[curx][cury][1].x - 1;
      nexty = precalc[curx][cury][1].y;
      nextDP = curDP;
      nextCP = curCP;
      if (nextx < 0 || a[nextx][nexty] == '0') {
        nextx = curx;
        nexty = cury;
        nextDP = (curDP + 1) % 4;
        nextCP = 1 - curCP;
      }
    } else if (curDP == 1 && curCP == 0) {
      nextx = precalc[curx][cury][1].x;
      nexty = precalc[curx][cury][1].y + 1;
      nextDP = curDP;
      nextCP = curCP;
      if (nexty >= M || a[nextx][nexty] == '0') {
        nextx = curx;
        nexty = cury;
        nextDP = curDP;
        nextCP = 1 - curCP;
      }
    } else if (curDP == 1 && curCP == 1) {
      nextx = precalc[curx][cury][3].x;
      nexty = precalc[curx][cury][3].y + 1;
      nextDP = curDP;
      nextCP = curCP;
      if (nexty >= M || a[nextx][nexty] == '0') {
        nextx = curx;
        nexty = cury;
        nextDP = (curDP + 1) % 4;
        nextCP = 1 - curCP;
      }
    } else if (curDP == 2 && curCP == 0) {
      nextx = precalc[curx][cury][3].x + 1;
      nexty = precalc[curx][cury][3].y;
      nextDP = curDP;
      nextCP = curCP;
      if (nextx >= m || a[nextx][nexty] == '0') {
        nextx = curx;
        nexty = cury;
        nextDP = curDP;
        nextCP = 1 - curCP;
      }
    } else if (curDP == 2 && curCP == 1) {
      nextx = precalc[curx][cury][2].x + 1;
      nexty = precalc[curx][cury][2].y;
      nextDP = curDP;
      nextCP = curCP;
      if (nextx >= m || a[nextx][nexty] == '0') {
        nextx = curx;
        nexty = cury;
        nextDP = (curDP + 1) % 4;
        nextCP = 1 - curCP;
      }
    } else if (curDP == 3 && curCP == 0) {
      nextx = precalc[curx][cury][2].x;
      nexty = precalc[curx][cury][2].y - 1;
      nextDP = curDP;
      nextCP = curCP;
      if (nexty < 0 || a[nextx][nexty] == '0') {
        nextx = curx;
        nexty = cury;
        nextDP = curDP;
        nextCP = 1 - curCP;
      }
    } else if (curDP == 3 && curCP == 1) {
      nextx = precalc[curx][cury][0].x;
      nexty = precalc[curx][cury][0].y - 1;
      nextDP = curDP;
      nextCP = curCP;
      if (nexty < 0 || a[nextx][nexty] == '0') {
        nextx = curx;
        nexty = cury;
        nextDP = (curDP + 1) % 4;
        nextCP = 1 - curCP;
      }
    }
    curx = nextx;
    cury = nexty;
    curDP = nextDP;
    curCP = nextCP;
    if (used[curx][cury][curDP][curCP] == -1)
      used[curx][cury][curDP][curCP] = i;
    else {
      int delta = i - used[curx][cury][curDP][curCP];
      ans = used[curx][cury][curDP][curCP] +
            (n - used[curx][cury][curDP][curCP]) % delta;
      break;
    }
  }
  for (int i = 0; i < m; i++)
    for (int j = 0; j < M; j++)
      for (int k = 0; k < 4; k++)
        for (int l = 0; l < 2; l++)
          if (used[i][j][k][l] == ans) cout << a[i][j] << endl;
  return 0;
}
