#include <iostream>
#include <vector>
#include <cmath>
#include <map>
#include <set>
#include <iomanip>
#include <queue>
#include <algorithm>
#include <numeric>
#include <deque>
#include <complex>
#include <cassert> 

using namespace std;

template<typename T> using pq = priority_queue<T, vector<T>, greater<T>>;
 
int main(){
 
    int H, W, h, w, nh, nw;
    long long alt, d, ans=1e18;
    cin >> H >> W;
    H-=2;
    
    pq<tuple<long long, int, int>> que;
    int dx[8] = {1, 1, 1, 0, -1, -1, -1, 0};
    int dy[8] = {-1, 0, 1, 1, 1, 0, -1, -1};
    vector<vector<long long>> dist(H, vector<long long>(W, 1e18));
    vector<vector<bool>> visit(H, vector<bool>(W));
    vector<vector<int>> field(H, vector<int>(W));
 
    for (int i=0; i < H; i++){
        for (int j=0; j<W; j++) cin >> field[i][j];
    }

    for (int i=0; i<H; i++){
        if (field[i][0] == -1) continue;
        que.push({field[i][0], i, 0});
        dist[i][0] = field[i][0];
    }
 
    while(!que.empty()){
        tie(d, h, w) = que.top();
        que.pop();
        if (visit[h][w]) continue;
        visit[h][w] = 1;
        for (int dir=0; dir < 8; dir++){
            nh = h + dx[dir];
            nw = w + dy[dir];
            if (nh < 0 || nh >= H || nw < 0 || nw >= W) continue;
            if (field[nh][nw] == -1) continue;
            alt = d + field[nh][nw];
            if (alt < dist[nh][nw]){
                dist[nh][nw] = alt;
                que.push({alt, nh, nw});
            }
        }
    }

    for (int i=0; i<H; i++) ans = min(ans, dist[i][W-1]);

    cout << (ans == 1e18 ? -1 : ans) << endl;

    return 0;
}