#include<bits/stdc++.h>
using namespace std;

const int MAXN = 105;
int a[MAXN][MAXN];
bool vis[MAXN][MAXN];
int n, m, minSum = INT_MAX;

// 方向结构体,用于排序
struct Direction {
    int dx, dy, value;
};

void dfs(int x, int y, int sum) {
    // 剪枝
    if (sum >= minSum) return;
    
    if (x == n && y == m) {
        minSum = sum;
        return;
    }
    
    vis[x][y] = true;
    
    // 收集可走的方向并按危险系数排序
    vector<Direction> dirs;
    int moves[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
    
    for (int i = 0; i < 4; i++) {
        int nx = x + moves[i][0];
        int ny = y + moves[i][1];
        
        if (nx >= 1 && nx <= n && ny >= 1 && ny <= m && !vis[nx][ny]) {
            dirs.push_back({nx, ny, a[nx][ny]});
        }
    }
    
    // 按危险系数从小到大排序,优先走安全的路
    sort(dirs.begin(), dirs.end(), [](const Direction& a, const Direction& b) {
        return a.value < b.value;
    });
    
    // 按排序后的顺序探索
    for (auto& dir : dirs) {
        dfs(dir.dx, dir.dy, sum + dir.value);
    }
    
    vis[x][y] = false;
}

int main() {
    cin >> n >> m;
    
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            cin >> a[i][j];
        }
    }
    
    memset(vis, false, sizeof(vis));
    dfs(1, 1, a[1][1]);
    
    cout << minSum << endl;
    return 0;
}

/**************************************************************
	Problem: 1541
	User: zzz
	Language: C++
	Result: Time Limit Exceed
****************************************************************/