#include using namespace std; using ll = long long; int n,m,k; vector> a; bool func(int X) { // 01bfs deque> dq; vector> dp(n,vector(m,INT_MAX)); dq.push_back({0,0,a[0][0] < X}); int dx[4] = {1,-1,0,0}; int dy[4] = {0,0,1,-1}; while (!dq.empty()) { auto [x,y,z] = dq.front(); dq.pop_front(); if (dp[x][y] != INT_MAX) continue; dp[x][y] = z; for (int l = 0; l < 4; l++) { int nx = x + dx[l]; int ny = y + dy[l]; if (nx < 0 || nx >= n) continue; if (ny < 0 || ny >= m) continue; if (dp[nx][ny] != INT_MAX) continue; int t = a[nx][ny] < X; if (t) dq.push_back({nx,ny,z+1}); else dq.push_front({nx,ny,z}); } } return dp[n-1][m-1] <= k; } int main(void) { cin >> n >> m >> k; // k >= n+mなら、経路全てを1e9に置き換えればよい a.resize(n,vector(m)); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cin >> a[i][j]; } } if (k >= n+m) { int ans = 1e9; cout << ans << endl; return 0; } // k個1e9に置き換えて良い // 最短距離で良いのか? // 二分探索 // x以上に出来るか? int ng = 0, ok = 1e9+1; while (ng+1 != ok) { int md = (ng+ok)/2; if (!func(md)) { ok = md; } else { ng = md; } } cout << ng << endl; return 0; }