include <bits/stdc++.h>
using namespace std;
int a[40][40],q[10000][3]s[45][45];
int fx[4] = {0,1,0,-1};
int fy[4] = {1,0,-1,0};
int n,m,i,j; 
int main(){
    cin>>n>>m; 
    for(i = 1;i <= n;i++){
        for(j = 1;j <= m;j++){
            cin>>a[i][j];
            s[i][j] = INT_MAX;
        }
    }
    int head = 1;
    int tail = 1;
    int tx,ty;
    q[head][1] = 1;
    q[head][2] = 1;
    s[1][1] = a[1][1]; 
    while(head <= tail){
        for(i = 0;i < 4;i++){
            tx = q[head][1] + fx[i];
            ty = q[head][2] + fy[i];
            if(tx >= 1 && tx <= n && ty >= 1 && ty <= m && s[q[head]][q[head]] + a[tx][ty] < s[tx][ty]){
                tail++;
                q[tail][1] = tx;
                q[tail][2] = ty; 
                s[tx][ty] = s[q[head][1]][q[head][2]] + a[tx][ty];
            } 
        }
        head++;
    }
    cout<<s[n][m]<<endl;
}
/**************************************************************
	Problem: 1541
	User: yangwanning
	Language: C++
	Result: Compile Error
****************************************************************/