#include <bits/stdc++.h>
using namespace std;
template <class T>
inline T myabs(T x) {
  return (x > 0 ? x : -x);
}
template <class T>
string tostring(T x) {
  ostringstream q;
  q << x;
  return q.str();
}
int strtoint(string x) {
  istringstream q(x);
  int res;
  q >> res;
  return res;
}
const int INF = 0x7FFFFFFF;
int b[1111][1111];
int a[1111][1111];
int md[111111], mu[111111], ml[111111], mr[111111];
int color;
int n, m;
void rec(int x, int y) {
  a[x][y] = color;
  if (x + 1 < n && a[x + 1][y] == 0 && b[x + 1][y] == b[x][y]) rec(x + 1, y);
  if (y + 1 < m && a[x][y + 1] == 0 && b[x][y + 1] == b[x][y]) rec(x, y + 1);
  if (x - 1 >= 0 && a[x - 1][y] == 0 && b[x - 1][y] == b[x][y]) rec(x - 1, y);
  if (y - 1 >= 0 && a[x][y - 1] == 0 && b[x][y - 1] == b[x][y]) rec(x, y - 1);
  ml[color] = min(ml[color], y);
  mr[color] = max(mr[color], y);
  mu[color] = min(mu[color], x);
  md[color] = max(md[color], x);
}
int main() {
  int k;
  cin >> n >> k;
  cin.ignore(256, '\n');
  fill(mu, mu + 111111, INF);
  fill(ml, ml + 111111, INF);
  for (int i = 0; i < n; ++i) {
    string s;
    getline(cin, s);
    m = s.length();
    for (int j = 0; j < s.length(); ++j) {
      b[i][j] = s[j] - '0';
    }
  }
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      if (b[i][j] != 0 && a[i][j] == 0) {
        ++color;
        rec(i, j);
      }
    }
  }
  int bp = b[0][0];
  int dp = 0;
  int cp = 1;
  int x = 0, y = 0;
  for (int w = 0; w < k; ++w) {
    int nx, ny, mx, my;
    if (dp == 0) {
      nx = 0;
      ny = 1;
      y = mr[a[x][y]];
      if (cp == 0) {
        x = md[a[x][y]];
        mx = 1;
        my = 0;
      } else {
        x = mu[a[x][y]];
        mx = -1;
        my = 0;
      }
    } else if (dp == 1) {
      nx = 1;
      ny = 0;
      x = md[a[x][y]];
      if (cp == 0) {
        y = ml[a[x][y]];
        mx = 0;
        my = -1;
      } else {
        y = mr[a[x][y]];
        mx = 0;
        my = 1;
      }
    } else if (dp == 2) {
      nx = 0;
      ny = -1;
      y = ml[a[x][y]];
      if (cp == 0) {
        x = mu[a[x][y]];
        mx = -1;
        my = 0;
      } else {
        x = md[a[x][y]];
        mx = 1;
        my = 0;
      }
    } else if (dp == 3) {
      nx = -1;
      ny = 0;
      x = mu[a[x][y]];
      if (cp == 0) {
        mx = 0;
        my = 1;
        y = mr[a[x][y]];
      } else {
        mx = 0;
        my = -1;
        y = ml[a[x][y]];
      }
    }
    while (x + nx >= 0 && x + nx <= n && y + ny >= 0 && y + ny <= m &&
           b[x + nx][y + ny] == bp) {
      x += nx;
      y += ny;
    }
    while (x + mx >= 0 && x + mx <= n && y + my >= 0 && y + my <= m &&
           b[x + mx][y + my] == bp) {
      x += mx;
      y += my;
    }
    if (x + nx < 0 || x + nx > n || y + ny < 0 || y + ny > m ||
        b[x + nx][y + ny] == 0) {
      if (cp == 1) {
        cp = 0;
      } else {
        cp = 1;
        dp = (dp + 1) % 4;
      }
    } else {
      x += nx;
      y += ny;
      bp = b[x][y];
    }
  }
  cout << bp << endl;
  return 0;
}
