#include <cstdio>
#include <vector>
#include <queue>
using namespace std;
const int MAX = 100;
typedef pair<int,int> pi;
int main()
{
int M,N,table[MAX+1][MAX+1]={0},dis[MAX+1][MAX+1]={0};
priority_queue<pi,vector<pi>,greater<pi>> pq;
scanf("%d %d",&M,&N);
for(int i =0 ; i < N ; i ++){
for(int j =0 ; j < M ; j++){
scanf("%1d",&table[i][j]);
dis[i][j]=1<<30; //Maximum 값
}
}
printf("%d",1<<30);
pq.push(make_pair(0,0));
dis[0][0]=0;
int dy[4]={0,0,-1,1}; // x, y
int dx[4]={-1,1,0,0}; //상(0,-1) ,하(0,1) , 좌(-1,0) , 우(1,0);
while ( !pq.empty() )
{
int w = pq.top().first;
int x = pq.top().second/MAX;
int y = pq.top().second%MAX;
pq.pop();
if(w>dis[y][x])continue;
for(int i =0 ; i < 4 ; i ++)
{
int next_x = x + dx[i];
int next_y = y + dy[i];
if(next_x<0 || next_y<0 || next_x>=M || next_y>=N) continue;
if( dis[next_y][next_x] > table[next_y][next_x]+dis[y][x] )
{
dis[next_y][next_x] = table[next_y][next_x]+dis[y][x];
pq.push( pi( dis[next_y][next_x],next_x * MAX + next_y ) ); //x,y 좌표의 값을 위해 100을 곱함
}
}
}
printf("%d",dis[N-1][M-1]);
}