simulation...
/*
* tyvj-P1027
* mike-w
* 2012-8-26
*/
#include<stdio.h>
#define MAX_SIZE 45
int s[MAX_SIZE][MAX_SIZE];
int R, C;
int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
int go(int *x, int *y)
{
int x0=*x, y0=*y, x1, y1, xx=x0, yy=y0;
int i;
s[x0][y0]=-1;
for(i=0; i<4; i++)
{
x1=x0+dir[i][0];
y1=y0+dir[i][1];
if(x1>=0 && x1<R && y1>=0 && y1<C && s[x1][y1]>s[xx][yy])
xx=x1, yy=y1;
}
*x=xx, *y=yy;
return s[xx][yy];
}
int main(void)
{
#ifndef ONLINE_JUDGE
freopen("in", "r", stdin);
#endif
int i, j, count=0;
scanf("%d%d", &R, &C);
for(i=0; i<R; i++)
for(j=0; j<C; j++)
scanf("%d", s[i]+j);
i=0,j=0;
count=s[0][0];
while(i!=R-1 || j!=C-1)
count+=go(&i, &j);
printf("%d\n", count);
return 0;
}