#include <bits/stdc++.h>
using namespace std;
int n,m;
int a[40][40],q[100000][3],s[45][45];
int fx[4] = {0,1,0,-1};
int fy[4] = {1,0,-1,0};
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(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: yangwanning
Language: C++
Result: Accepted
Time:44 ms
Memory:3260 kb
****************************************************************/