#pragma GCC target("avx2") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; using namespace atcoder; typedef long long ll; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define repr(i, n) for (int i = (int)(n) - 1; i >= 0; i--) #define repk(i, k, n) for (int i = k; i < (int)(n); i++) #define all(v) v.begin(), v.end() #define mod1 1000000007 #define mod2 998244353 #define mod3 100000007 #define vi vector #define vs vector #define vc vector #define vl vector #define vb vector #define vvi vector> #define vvc vector> #define vvl vector> #define vvb vector> #define vvvi vector>> #define vvvl vector>> #define pii pair #define pil pair #define pli pair #define pll pair #define vpii vector> #define vpll vector> #define vvpii vector>> #define vvpll vector>> template void debug(T e){ cerr << e << endl; } template void debug(vector &v){ rep(i, v.size()){ cerr << v[i] << " "; } cerr << endl; } template void debug(vector> &v){ rep(i, v.size()){ rep(j, v[i].size()){ cerr << v[i][j] << " "; } cerr << endl; } } template void debug(vector> &v){ rep(i,v.size()){ cerr << v[i].first << " " << v[i].second << endl; } } template void debug(set &st){ for (auto itr = st.begin(); itr != st.end(); itr++){ cerr << *itr << " "; } cerr << endl; } template void debug(multiset &ms){ for (auto itr = ms.begin(); itr != ms.end(); itr++){ cerr << *itr << " "; } cerr << endl; } template void debug(map &mp){ for (auto itr = mp.begin(); itr != mp.end(); itr++){ cerr << itr->first << " " << itr->second << endl; } } void debug_out(){ cerr << endl; } template void debug_out(Head H, Tail... T){ cerr << H << " "; debug_out(T...); } ll my_pow(ll x, ll n, ll mod){ // 繰り返し二乗法.x^nをmodで割った余り. ll ret; if (n == 0){ ret = 1; } else if (n % 2 == 1){ ret = (x * my_pow((x * x) % mod, n / 2, mod)) % mod; } else{ ret = my_pow((x * x) % mod, n / 2, mod); } return ret; } ll inv(ll x, ll mod){ return my_pow(x, mod - 2, mod); } void dijkstra(vvpll &graph, ll v, vl &dist){ priority_queue> pque; pque.push(pll(0, v)); dist[v] = 0; while(pque.size()){ pll p = pque.top(); pque.pop(); if (dist[p.second] != p.first){ continue; } for (auto x: graph[p.second]){ if (dist[x.first] > dist[p.second] + x.second){ dist[x.first] = dist[p.second] + x.second; pque.push(pll(dist[x.first], x.first)); } } } } int main(){ ll H, W; cin >> H >> W; vvl A(H, vl(W)); for (ll i = 1; i < H - 1; i++){ for (ll j = 0; j < W; j++){ cin >> A[i][j]; } } ll INF = 100000000000000; // 最短経路問題? vector dist(H * W + 2, INF); dist[H * W] = 0; vector> graph(H * W + 2, vpll(0)); for (ll i = 1; i < H - 1; i++){ if (A[i][0] != -1){ graph[H * W].push_back(pll(i * W, A[i][0])); } } for (ll i = 1; i < H - 1; i++){ if (A[i][W - 1] != -1){ graph[i * W + (W - 1)].push_back(pll(H * W + 1, 0)); } } for (ll i = 1; i < H - 1; i++){ for (ll j = 0; j < W; j++){ if (A[i][j] == -1){ continue; } if (A[i - 1][j] != -1){ graph[i * W + j].push_back(pll((i - 1) * W + j, A[i - 1][j])); } if (A[i + 1][j] != -1){ graph[i * W + j].push_back(pll((i + 1) * W + j, A[i + 1][j])); } if (j > 0){ if (A[i][j - 1] != -1){ graph[i * W + j].push_back(pll(i * W + j - 1, A[i][j - 1])); } } if (j < W - 1){ if (A[i][j + 1] != -1){ graph[i * W + j].push_back(pll(i * W + j + 1, A[i][j + 1])); } } if (j > 0){ if (A[i - 1][j - 1] != -1){ graph[i * W + j].push_back(pll((i - 1) * W + j - 1, A[i - 1][j - 1])); } if (A[i + 1][j - 1] != -1){ graph[i * W + j].push_back(pll((i + 1) * W + j - 1, A[i + 1][j - 1])); } } if (j < W - 1){ if (A[i - 1][j + 1] != -1){ graph[i * W + j].push_back(pll((i - 1) * W + j + 1, A[i - 1][j + 1])); } if (A[i + 1][j + 1] != -1){ graph[i * W + j].push_back(pll((i + 1) * W + j + 1, A[i + 1][j + 1])); } } } } //dijkstra(graph, H * W, dist); debug(dist); if (dist[H * W + 1] == INF){ cout << -1 << endl; } else{ cout << dist[H * W + 1] << endl; } }