#include using namespace std; using ll = long long; template istream& operator >> (istream& is, vector& vec) { for(T& x : vec) is >> x; return is; } template ostream& operator << (ostream& os, const vector& vec) { if(vec.empty()) return os; os << vec[0]; for(auto it = vec.begin(); ++it != vec.end(); ) os << ' ' << *it; return os; } int main(){ ios::sync_with_stdio(false); cin.tie(0); int h, w, k; cin >> h >> w >> k; if(k > h + w){ cout << 1'000'000'000 << '\n'; return 0; } vector A(h, vector(w)); cin >> A; k = min(k, h + w); int ok = 0, ng = 1'000'000'005; deque> dq; vector> dir = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}}; while(ok + 1 < ng){ int mid = (ok + ng) / 2; vector dp(h, vector(w, 1 << 30)); dp[0][0] = A[0][0] < mid; dq.emplace_back(0, 0); while(!dq.empty()){ auto [y, x] = dq.front(); dq.pop_front(); for(auto [ny, nx] : dir){ ny += y, nx += x; if(ny < 0 || nx < 0 || ny >= h || nx >= w) continue; if(A[ny][nx] < mid){ if(dp[y][x] + 1 < dp[ny][nx]){ dp[ny][nx] = dp[y][x] + 1; dq.emplace_back(ny, nx); } }else{ if(dp[y][x] < dp[ny][nx]){ dp[ny][nx] = dp[y][x]; dq.emplace_front(ny, nx); } } } } (dp[h - 1][w - 1] <= k ? ok : ng) = mid; } cout << ok << '\n'; }