#include #include using namespace std; using namespace atcoder; #define rep(i, n) for(int i=0;i<(n);++i) #define rep1(i, n) for(int i=1;i<=(n);i++) #define ll long long using mint = modint998244353; using P = pair; using lb = long double; using T = tuple; #ifdef LOCAL # include # define dbg(...) debug_print::multi_print(#__VA_ARGS__, __VA_ARGS__) #else # define dbg(...) (static_cast(0)) #endif int main() { int n, m, k; cin >> n >> m >> k; vector> a(n, vector(m)); rep(i,n)rep(j,m) cin >> a[i][j]; ll low = 0; ll high = 2e9; vector dx = {1, 0, -1, 0}; vector dy = {0, 1, 0, -1}; while(high-low>1) { ll mid = (high+low)/2; vector> dp(n+1, vector(m+1, n+m)); if(a[0][0] q; q.emplace_back(0, 0); while(!q.empty()) { auto [x, y] = q.front();q.pop_front(); rep(d, 4) { int nx = x + dx[d]; int ny = y + dy[d]; if(nx<0 || nx>=n || ny<0 || ny>=m) continue; if(a[nx][ny]dp[x][y]+1) { q.emplace_back(nx,ny); dp[nx][ny] = dp[x][y] + 1; } } else{ if(dp[nx][ny]>dp[x][y]) { q.emplace_front(nx, ny); dp[nx][ny] = dp[x][y]; } } } } if(dp[n-1][m-1]<=k) low = mid; else high = mid; } cout << min(low, (ll)1e9) << endl; return 0; }