#include #include #define rep(i,n) for(int i=0;i vi; typedef vector vl; typedef vector> vvi; typedef vector> vvl; typedef long double ld; typedef pair P; ostream& operator<<(ostream& os, const modint& a) {os << a.val(); return os;} template ostream& operator<<(ostream& os, const static_modint& a) {os << a.val(); return os;} template ostream& operator<<(ostream& os, const dynamic_modint& a) {os << a.val(); return os;} template istream& operator>>(istream& is, vector& v){int n = v.size(); assert(n > 0); rep(i, n) is >> v[i]; return is;} template ostream& operator<<(ostream& os, const pair& p){os << p.first << ' ' << p.second; return os;} template ostream& operator<<(ostream& os, const vector& v){int n = v.size(); rep(i, n) os << v[i] << (i == n - 1 ? "\n" : " "); return os;} template ostream& operator<<(ostream& os, const vector>& v){int n = v.size(); rep(i, n) os << v[i] << (i == n - 1 ? "\n" : ""); return os;} template ostream& operator<<(ostream& os, const set& se){for(T x : se) os << x << " "; os << "\n"; return os;} template ostream& operator<<(ostream& os, const unordered_set& se){for(T x : se) os << x << " "; os << "\n"; return os;} template ostream& operator<<(ostream& os, const atcoder::segtree& seg){int n = seg.max_right(0, [](S){return true;}); rep(i, n) os << seg.get(i) << (i == n - 1 ? "\n" : " "); return os;} template ostream& operator<<(ostream& os, const atcoder::lazy_segtree& seg){int n = seg.max_right(0, [](S){return true;}); rep(i, n) os << seg.get(i) << (i == n - 1 ? "\n" : " "); return os;} template void chmin(T& a, T b){a = min(a, b);} template void chmax(T& a, T b){a = max(a, b);} const int INF = 1000000000; struct Edge_BFS01{ int from, to; int cost; Edge_BFS01(int from, int to, int cost) : from(from), to(to), cost(cost) {}; }; struct BFS01{ int n, m; vector initialized; vector E; vector> G; map> dist; map> idx; BFS01(int _n) : n(_n), m(0), initialized(n, false), G(n){} void add_edge(int from, int to, int cost){ assert(cost == 0 or cost == 1); Edge_BFS01 e(from, to, cost); E.push_back(e); G[from].emplace_back(m); m++; } void add_grid(int h, int w, vector s){ assert(int(s.size()) == h); assert(int(s[0].size()) == w); rep(y, h){ rep(x, w - 1){ if(s[y][x] != '#' and s[y][x + 1] != '#'){ add_edge(y * w + x, y * w + (x + 1), 1); add_edge(y * w + (x + 1), y * w + x, 1); } } } rep(x, w){ rep(y, h - 1){ if(s[y][x] != '#' and s[y + 1][x] != '#'){ add_edge(y * w + x, (y + 1) * w + x, 1); add_edge((y + 1) * w + x, y * w + x, 1); } } } } void calc(int s){ initialized[s] = true; dist[s] = vector(n, INF); idx[s] = vector(n, -1); deque> de; de.emplace_back(0, s, -1); while(de.size()){ auto [cost, from, index] = de.front(); de.pop_front(); if(dist[s][from] <= cost) continue; dist[s][from] = cost; idx[s][from] = index; for(int index : G[from]){ int to = E[index].to; int cost_plus = E[index].cost; if(dist[s][to] <= cost + cost_plus) continue; if(cost_plus == 0) de.emplace_front(cost + cost_plus, to, index); if(cost_plus == 1) de.emplace_back(cost + cost_plus, to, index); } } } int farthest(int s){ if(!initialized[s]) calc(s); int idx = 0; rep(i, n) if(dist[s][i] > dist[s][idx]) idx = i; return idx; } int get_dist(int s, int t){ if(!initialized[s]) calc(s); return dist[s][t]; } vi restore(int s, int t){ if(!initialized[s]) calc(s); if(dist[s][t] == INF) return vi(0); vi res; while(idx[s][t] != -1){ auto e = E[idx[s][t]]; res.push_back(idx[s][t]); t = e.from; } reverse(res.begin(), res.end()); return res; } }; int main(){ int n, m, k; cin >> n >> m >> k; vector> a(n, vector(m)); cin >> a; int ok = 0, ng = INF + 1; while(ng - ok > 1){ int mid = (ok + ng) / 2; BFS01 graph(n * m + 1); int sv = n * m; rep(y, n) rep(x, m - 1){ { int cost = 0; if(a[y][x + 1] < mid) cost = 1; graph.add_edge(y * m + x, y * m + (x + 1), cost); } { int cost = 0; if(a[y][x] < mid) cost = 1; graph.add_edge(y * m + (x + 1), y * m + x, cost); } } rep(y, n - 1) rep(x, m){ { int cost = 0; if(a[y + 1][x] < mid) cost = 1; graph.add_edge(y * m + x, (y + 1) * m + x, cost); } { int cost = 0; if(a[y][x] < mid) cost = 1; graph.add_edge((y + 1) * m + x, y * m + x, cost); } } { { int cost = 0; if(a[0][0] < mid) cost = 1; graph.add_edge(sv, 0, cost); } } int tot = graph.get_dist(sv, n * m - 1); if(tot <= k) ok = mid; else ng = mid; } cout << ok << "\n"; return 0; }