#include <bits/stdc++.h>
using namespace std;
vector<string> vs;
struct point {
  int x, y;
  bool sign;
};
struct str {
  struct point mp[8];
};
struct str mx[50][50];
int col, row;
int color;
void up(int &xx, int &yy) {
  for (; xx >= 0; xx--) {
    if (color != vs[xx][yy]) {
      break;
    }
  }
  xx++;
}
void down(int &xx, int &yy) {
  for (; xx < row; xx++) {
    if (color != vs[xx][yy]) {
      break;
    }
  }
  xx--;
}
void left(int &xx, int &yy) {
  for (; yy >= 0; yy--) {
    if (color != vs[xx][yy]) {
      break;
    }
  }
  yy++;
}
void right(int &xx, int &yy) {
  for (; yy < col; yy++) {
    if (color != vs[xx][yy]) {
      break;
    }
  }
  yy--;
}
bool cal(int dp, int cp, int &xx, int &yy) {
  color = vs[xx][yy];
  if (dp == 0) {
    right(xx, yy);
  } else if (dp == 1) {
    down(xx, yy);
  } else if (dp == 2) {
    left(xx, yy);
  } else {
    up(xx, yy);
  }
  if (cp == 0) {
    if (dp == 0) {
      up(xx, yy);
    } else if (dp == 1) {
      right(xx, yy);
    } else if (dp == 2) {
      down(xx, yy);
    } else {
      left(xx, yy);
    }
  } else {
    if (dp == 0) {
      down(xx, yy);
    } else if (dp == 1) {
      left(xx, yy);
    } else if (dp == 2) {
      up(xx, yy);
    } else {
      right(xx, yy);
    }
  }
  bool sign = false;
  if (dp == 0) {
    if (yy + 1 < col && vs[xx][yy + 1] > '0') {
      yy++;
      sign = true;
    }
  } else if (dp == 1) {
    if (xx + 1 < row && vs[xx + 1][yy] > '0') {
      xx++;
      sign = true;
    }
  } else if (dp == 2) {
    if (yy - 1 >= 0 && vs[xx][yy - 1] > '0') {
      yy--;
      sign = true;
    }
  } else {
    if (xx - 1 >= 0 && vs[xx - 1][yy] > '0') {
      xx--;
      sign = true;
    }
  }
  return sign;
}
int main() {
  int m, n;
  cin >> m >> n;
  for (int i = 0; i < m; i++) {
    string temp;
    cin >> temp;
    vs.push_back(temp);
  }
  row = vs.size(), col = vs[0].size();
  for (int i = 0; i < (int)vs.size(); i++)
    for (int j = 0; j < (int)vs[0].size(); j++) {
      if (vs[i][j] == '0') continue;
      for (int k = 0; k < 8; k++) {
        int dp = k / 2, cp = k % 2;
        int xx = i, yy = j;
        mx[i][j].mp[k].sign = cal(dp, cp, xx, yy);
        mx[i][j].mp[k].x = xx;
        mx[i][j].mp[k].y = yy;
      }
    }
  int k = 0;
  int x = 0, y = 0;
  while (n--) {
    int xx, yy;
    xx = mx[x][y].mp[k].x;
    yy = mx[x][y].mp[k].y;
    if (mx[x][y].mp[k].sign == 0) k++;
    k %= 8;
    x = xx, y = yy;
  }
  cout << vs[x][y] << endl;
  return 0;
}
