#include #include #include #include #include using namespace std; class Di { public: vector distance; vector>> G; long long inf = LLONG_MAX; Di(int n) { distance.assign(n + 1, inf); G.resize(n + 1); } void connect(int from, int to, long long cost) { G[from].emplace_back(to, cost); } void solve(int from) { priority_queue, vector>, greater>> pq; distance[from] = 0; pq.emplace(0, from); while (!pq.empty()) { auto [dist, f] = pq.top(); pq.pop(); if (dist > distance[f]) continue; for (auto &[t, cost] : G[f]) { long long new_dist = distance[f] + cost; if (distance[t] > new_dist) { distance[t] = new_dist; pq.emplace(new_dist, t); } } } } }; int main() { int H, W, K; cin >> H >> W >> K; vector> map(H, vector(W)); for (int y = 0; y < H; ++y) { for (int x = 0; x < W; ++x) { cin >> map[y][x]; } } long long ng = 1LL << 30; long long ok = 0; while (ng - ok > 1) { long long mid = (ng + ok) / 2; Di di(H * W); for (int y = 0; y < H; ++y) { for (int x = 0; x < W; ++x) { int current = y * W + x + 1; if (x > 0) di.connect(current - 1, current, map[y][x] < mid); if (y > 0) di.connect((y - 1) * W + x + 1, current, map[y][x] < mid); if (x < W - 1) di.connect(current + 1, current, map[y][x] < mid); if (y < H - 1) di.connect((y + 1) * W + x + 1, current, map[y][x] < mid); } } di.solve(1); if (di.distance[H * W] + (map[0][0] < mid) <= K) { ok = mid; } else { ng = mid; } } cout << min(ok, 1000000000LL) << endl; return 0; }