#include <bits/stdc++.h>
using namespace std;
int a[40][40];//地图
int q[10000][3];//队列,存储走过的点
int s[40][40];//存放到每个点的最小危险系数
//方向数组
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];
//要去的点在棋盘内,且从head点去该点的危险系数更小,则过去
if(tx >= 1 && tx <= n && ty >= 1 && ty <= m && s[q[head][1]][q[head][2]] + 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: admin
Language: C++
Result: Accepted
Time:73 ms
Memory:2204 kb
****************************************************************/