#include <bits/stdc++.h>
using namespace std;
enum { L, R };
struct Item {
  int clr, dp, cp;
} map[55][55];
int dir[4][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
int m, n, l;
bool check(int r, int c) {
  if (r > m || r < 1 || c > l || c < 1) return false;
  if (map[r][c].clr == 0) return false;
  return true;
}
void change(int r, int c) {
  if (map[r][c].cp == L)
    map[r][c].cp = R;
  else {
    map[r][c].cp = L;
    map[r][c].dp = (map[r][c].dp + 1) % 4;
  }
}
void solve(int step, int r, int c) {
  if (step > n) {
    cout << map[r][c].clr << endl;
    return;
  }
  int id = map[r][c].dp;
  int rr = dir[id][0] + r;
  int cc = dir[id][1] + c;
  if (check(rr, cc)) {
    map[rr][cc].dp = map[r][c].dp;
    map[rr][cc].cp = map[r][c].cp;
    solve(step + 1, rr, cc);
  } else {
    change(r, c);
    solve(step + 1, r, c);
  }
}
int main() {
  char temp[100];
  cin >> m >> n;
  cin >> temp + 1;
  l = strlen(temp + 1);
  for (int j = 1; j <= l; j++) map[1][j].clr = temp[j] - '0';
  for (int i = 2; i <= m; i++) {
    cin >> temp + 1;
    for (int j = 1; j <= l; j++) map[i][j].clr = temp[j] - '0';
  }
  map[1][1].dp = 1;
  map[1][1].cp = L;
  solve(0, 1, 1);
  return 0;
}
