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; 
int main(){
    cin>>n>>m; 
    for(int i = 1;i <= n;i++){
        for(int j = 1;j <= m;j++){
            cin>>a[i][j];
            s[i][j] = INT_MAX;
        }
    }
    int head = 1,tail = 1,tx,ty;
    q[head][1] = 1;
    q[head][2] = 1;
    s[1][1] = a[1][1];
    while(head <= tail)
    {
        for(int i = 0;i < 4;i++)
        {
            tx = q[head][1] + fx[i];
            ty = q[head][2] + fy[i];
            if(s[q[head][1]][q[head][2]] + a[tx][ty] < s[tx][ty] && ty <= m && ty>=1 && tx>=1 && tx <= n)
            {
                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
****************************************************************/