#pragma GCC optimize("Ofast") #include using namespace std; typedef long long int ll; typedef unsigned long long int ull; mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); ll myRand(ll B) { return (ull)rng() % B; } inline double time() { return static_cast(chrono::duration_cast(chrono::steady_clock::now().time_since_epoch()).count()) * 1e-9; } int main(){ cin.tie(nullptr); ios::sync_with_stdio(false); int h,w; cin >> h >> w; vector> a(h-2,vector(w)); for (int i = 0; i < h - 2; ++i) { for (int j = 0; j < w; ++j) { cin >> a[i][j]; } } // vector>> dp(w, vector> (h-2, vector(4, 1e18))); vector> dp(w,vector(h-2,1e18)); // auto f = [&](int i) -> int { // int res = 0; // if (i == 0) res |= 1; // if (i == h-3) res |= 2; // return res; // }; for (int i = 0; i < h - 2; ++i) { if(a[i][0] != -1) dp[0][i] = a[i][0]; // dp[0][i][f(i)]= a[i][0]; } for (int i = 1; i < w; ++i) { for (int j = 0; j < h - 2; ++j) { // for (int k = 0; k < 4; ++k) { // if (a[j][i] != -1) { // dp[i][j][k] = min(dp[i][j][k], dp[i-1][j][k]+a[j][i]); // } // if (j and a[j-1][i] != -1) { // dp[i][j-1][k|f(j-1)] = min(dp[i][j-1][k|f(j-1)], dp[i-1][j][k]+a[j-1][i]); // } // if (j+1 < h-2 and a[j+1][i] != -1) { // dp[i][j+1][k|f(j+1)] = min(dp[i][j+1][k|f(j+1)], dp[i-1][j][k]+a[j+1][i]); // } // } ll mi = dp[i-1][j]; for (int k = -1; k <= 1; ++k) { if (0 <= j+k and j+k < h-2) { mi = min(mi, dp[i-1][j+k]); } } if(a[j][i] != -1 and mi != 1e18) dp[i][j] = mi + a[j][i]; if (j and a[j][i] != -1) dp[i][j] = min(dp[i][j], dp[i][j-1] + a[j][i]); } for (int j = h - 4; j >= 0; --j) { if (a[j][i] != -1) { dp[i][j] = min(dp[i][j], dp[i][j+1]+a[j][i]); } } } ll res = 1e18; for (int i = 0; i < h-2; ++i) { res = min(res, dp[w-1][i]); } if (res == 1e18) res = -1; cout << res << endl; }