#include <bits/stdc++.h>
using namespace std;
const int mx[] = {1, -1, 0, 0};
const int my[] = {0, 0, 1, -1};
struct RECT {
  int upb;
  int leb;
  int rib;
  int dob;
  int col;
};
vector<RECT> rectangulos;
int corr[55][55], act = 1, an, al, color[55][55], n, dir = 0, sec = 0, nxt;
bool mrk[55][55];
void exp(int xi, int yi) {
  int up, le, ri, dow;
  queue<int> x, y;
  x.push(xi);
  y.push(yi);
  up = yi;
  dow = yi;
  le = xi;
  ri = xi;
  mrk[xi][yi] = 1;
  corr[xi][yi] = act;
  while (x.size()) {
    if (y.front() < up) {
      up = y.front();
    }
    if (y.front() > dow) {
      dow = y.front();
    }
    if (x.front() > ri) {
      ri = x.front();
    }
    if (x.front() < le) {
      le = x.front();
    }
    for (int i = 0; i < 4; i++) {
      if (x.front() + mx[i] >= 0 && y.front() + my[i] >= 0 &&
          x.front() + mx[i] < an && y.front() + my[i] < al &&
          mrk[x.front() + mx[i]][y.front() + my[i]] == 0 &&
          color[x.front() + mx[i]][y.front() + my[i]] ==
              color[x.front()][y.front()]) {
        mrk[x.front() + mx[i]][y.front() + my[i]] = 1;
        corr[x.front() + mx[i]][y.front() + my[i]] = act;
        x.push(x.front() + mx[i]);
        y.push(y.front() + my[i]);
      }
    }
    x.pop();
    y.pop();
  }
  RECT aux;
  aux.upb = up;
  aux.dob = dow;
  aux.leb = le;
  aux.rib = ri;
  aux.col = color[xi][yi];
  rectangulos.push_back(aux);
}
void gen() {
  for (int i = 0; i < an; i++) {
    for (int j = 0; j < al; j++) {
      if (mrk[i][j] == 0 && color[i][j] != 0) {
        exp(i, j);
        act++;
      }
    }
  }
}
void changedir() {
  sec++;
  sec %= 2;
  if (sec == 0) {
    dir++;
    dir %= 4;
  }
}
int main(void) {
  string line;
  RECT aux;
  rectangulos.push_back(aux);
  scanf("%d%d", &al, &n);
  for (int i = 0; i < al; i++) {
    cin >> line;
    for (int j = 0; j < line.size(); j++) {
      color[j][i] = line[j] - '0';
    }
  }
  an = line.size();
  gen();
  act = 1;
  for (int i = 0; i < n; i++) {
    if (dir == 0) {
      if (rectangulos[act].rib == an - 1) {
        changedir();
        continue;
      }
      if (sec == 0) {
        nxt = corr[rectangulos[act].rib + 1][rectangulos[act].upb];
        if (nxt == 0) {
          changedir();
          continue;
        }
      } else {
        nxt = corr[rectangulos[act].rib + 1][rectangulos[act].dob];
        if (nxt == 0) {
          changedir();
          continue;
        }
      }
    }
    if (dir == 1) {
      if (rectangulos[act].dob == al - 1) {
        changedir();
        continue;
      }
      if (sec == 0) {
        nxt = corr[rectangulos[act].rib][rectangulos[act].dob + 1];
        if (nxt == 0) {
          changedir();
          continue;
        }
      } else {
        nxt = corr[rectangulos[act].leb][rectangulos[act].dob + 1];
        if (nxt == 0) {
          changedir();
          continue;
        }
      }
    }
    if (dir == 2) {
      if (rectangulos[act].leb == 0) {
        changedir();
        continue;
      }
      if (sec == 0) {
        nxt = corr[rectangulos[act].leb - 1][rectangulos[act].dob];
        if (nxt == 0) {
          changedir();
          continue;
        }
      } else {
        nxt = corr[rectangulos[act].leb - 1][rectangulos[act].upb];
        if (nxt == 0) {
          changedir();
          continue;
        }
      }
    }
    if (dir == 3) {
      if (rectangulos[act].upb == 0) {
        changedir();
        continue;
      }
      if (sec == 0) {
        nxt = corr[rectangulos[act].leb][rectangulos[act].upb - 1];
        if (nxt == 0) {
          changedir();
          continue;
        }
      } else {
        nxt = corr[rectangulos[act].rib][rectangulos[act].upb - 1];
        if (nxt == 0) {
          changedir();
          continue;
        }
      }
    }
    act = nxt;
  }
  printf("%d\n", rectangulos[act].col);
}
