#include <bits/stdc++.h>
using namespace std;
vector<vector<int> > v;
int bpX, bpY, dp, cp;
void startFill(int m) {
  for (int i = 0; i < m + 2; i++) {
    v[0][i] = 0;
    v[m + 1][i] = 0;
    v[i][0] = 0;
    v[i][m + 1] = 0;
  }
  return;
}
void findEnd() {
  int curX = bpX;
  int curY = bpY;
  int pointer = dp;
  while (v[curX][curY] == v[bpX][bpY]) {
    if (pointer == 0) {
      curY++;
    } else if (pointer == 1) {
      curX++;
    } else if (pointer == 2) {
      curY--;
    } else {
      curX--;
    }
  }
  if (pointer == 0) {
    curY--;
  } else if (pointer == 1) {
    curX--;
  } else if (pointer == 2) {
    curY++;
  } else {
    curX++;
  }
  pointer += cp;
  if (pointer == -1) {
    pointer = 3;
  }
  while (v[curX][curY] == v[bpX][bpY]) {
    if (pointer == 0) {
      curY++;
    } else if (pointer == 1) {
      curX++;
    } else if (pointer == 2) {
      curY--;
    } else {
      curX--;
    }
  }
  if (pointer == 0) {
    curY--;
  } else if (pointer == 1) {
    curX--;
  } else if (pointer == 2) {
    curY++;
  } else {
    curX++;
  }
  bpX = curX;
  bpY = curY;
  return;
}
int main() {
  int m, n;
  scanf("%d %d\n", &m, &n);
  v.resize(m + 2, vector<int>(m + 2));
  startFill(m);
  for (int i = 1; i <= m; i++) {
    for (int j = 1; j <= m; j++) {
      char c = getchar();
      v[i][j] = c - '0';
    }
    getchar();
  }
  bpX = 1;
  bpY = 1;
  dp = 0;
  cp = -1;
  for (int i = 0; i < n; i++) {
    findEnd();
    int X = bpX, Y = bpY;
    if (dp == 0) {
      Y++;
    } else if (dp == 1) {
      X++;
    } else if (dp == 2) {
      Y--;
    } else {
      X--;
    }
    if (v[X][Y] == 0) {
      if (cp == -1) {
        cp = 1;
      } else {
        cp = -1;
        dp++;
        if (dp == 4) {
          dp = 0;
        }
      }
    } else {
      bpX = X;
      bpY = Y;
    }
  }
  printf("%d", v[bpX][bpY]);
  return 0;
}
