#include using namespace std; int dir[4][2] = {1, 0, -1, 0, 0, 1, 0, -1}; int main() { int n, m, k; cin >> n >> m >> k; vector> a(n, vector(m)); for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) cin >> a[i][j]; auto check = [&](int x) { vector> d(n, vector(m, -1)); deque> q; // 双端队列 q.push_back({0, 0}); d[0][0] = a[0][0] < x; while (!q.empty()) { auto p = q.front(); q.pop_front(); for (int i = 0; i < 4; i++) { int r = p.first + dir[i][0]; int c = p.second + dir[i][1]; if (0 <= r && r < n && 0 <= c && c < m && d[r][c] == -1) { d[r][c] = d[p.first][p.second] + (a[r][c] < x); if (a[r][c] < x) { q.push_back({r, c}); } else { q.push_front({r, c}); } } } } return d[n - 1][m - 1] <= k; }; int ok = 1, ng = 1e9 + 1; while (ng - ok > 1) { int x = (ok + ng) / 2; if (check(x)) ok = x; else ng = x; } cout << ok << '\n'; }