// I SELL YOU...! #include #include #include #include #include #include #include #include #include using namespace std; using ll = long long; using P = pair; using TP = tuple; void init_io(){ cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(18); } const ll INF = 1e9; signed main(){ init_io(); ll h,w; cin >> h >> w; vector> a(h, vector(w, -1)); vector> dp(h, vector(w, INF)); priority_queue, greater> que; for(int i=1;i> a[i][j]; if (j==0 && a[i][j] != -1) { dp[i][j] = a[i][j]; que.push(TP(a[i][j], i, j)); } } } while(!que.empty()) { auto [v, i, j] = que.top(); que.pop(); if (dp[i][j] < v) continue; ll nx[] = {i-1, i+1, i-1, i, i+1}; ll ny[] = {j, j, j+1, j+1, j+1}; for (int t=0;t<5;t++) { ll ni = nx[t]; ll nj = ny[t]; if (ni>0 && ni nv) { que.push(TP(nv, ni, nj)); dp[ni][nj] = nv; } } } } ll ans = INF; for (int i=0;i