#include using namespace std; template vector dijkstra(const vector>> &G, T x){ const long long INF = 0x1fffffffffffffff; vector cost((int) G.size(), INF); using P = pair; priority_queue, greater<>> q; cost[x] = 0; q.emplace(0, x); while(q.size()){ auto [c, at] = q.top(); q.pop(); if(c > cost[at]) continue; for(auto& [to, t] : G[at]){ if(cost[to] > c + t){ cost[to] = c + t; q.emplace(cost[to], to); } } } return cost; } vector dx = {0, 1, 0, -1}; vector dy = {1, 0, -1, 0}; int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); 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]; } } int ok = 1, ng = 1000000000 + 1; while(abs(ok - ng) > 1){ int mid = (ok + ng) / 2; vector>> G(n * m + 1); for(int i = 0; i < n; i++){ for(int j = 0; j < m; j++){ for(int dir = 0; dir < 4; dir++){ int nx = i + dx[dir], ny = j + dy[dir]; if(0 <= nx && nx < n && 0 <= ny && ny < m){ if(a[nx][ny] < mid){ G[j * n + i].push_back({ny * n + nx, 1}); }else{ G[j * n + i].push_back({ny * n + nx, 0}); } } } } } vector cost = dijkstra(G, 0); if(cost[n * m - 1] + (a[0][0] < mid) <= k){ ok = mid; }else{ ng = mid; } } cout << ok << endl; }