#include <bits/stdc++.h>
int main() {
  int map[50][50];
  int m;
  int n;
  int rowCurr = 0;
  int colCurr = 0;
  int iCol, iRow;
  int iRun;
  int DP = 0;
  int CP = 0;
  scanf("%d %d\n", &m, &n);
  for (iRow = 0; iRow < m; ++iRow) {
    char line[128];
    scanf("%s\n", line);
    for (iCol = 0; iCol < m; ++iCol) {
      map[iRow][iCol] = (line[iCol] - '0');
    }
  }
  for (iRun = 0; iRun < n; ++iRun) {
    switch (DP) {
      case 0:
        if (colCurr < m - 1 && 0 != map[rowCurr][colCurr + 1]) {
          ++colCurr;
        } else {
          if (0 == CP)
            CP = 1;
          else {
            CP = 0;
            DP = 1;
          }
        }
        break;
      case 1:
        if (rowCurr < m - 1 && 0 != map[rowCurr + 1][colCurr]) {
          ++rowCurr;
        } else {
          if (0 == CP)
            CP = 1;
          else {
            CP = 0;
            DP = 2;
          }
        }
        break;
      case 2:
        if (colCurr > 0 && 0 != map[rowCurr][colCurr - 1]) {
          --colCurr;
        } else {
          if (0 == CP)
            CP = 1;
          else {
            CP = 0;
            DP = 3;
          }
        }
        break;
      case 3:
        if (rowCurr > 0 && 0 != map[rowCurr - 1][colCurr]) {
          --rowCurr;
        } else {
          if (0 == CP)
            CP = 1;
          else {
            CP = 0;
            DP = 0;
          }
        }
        break;
    }
  }
  printf("%d\n", map[rowCurr][colCurr]);
  return (0);
}
