program number;
var
table:array [1..10,1..10] of integer;
min,i,j,m,n:integer;
procedure go(x,y,sum:integer);
begin
if (x=1) and (y=1) then begin
if (sum>0) and (sum<min) then min:=sum;
end
else begin
if x-1>=1 then go(x-1,y,sum+table[x,y]);
if y-1>=1 then go(x,y-1,sum+table[x,y]);
end;
end;
begin
assign (input,'number.in');
reset (input);
readln (m,n);
min:=maxint;
for i:=1 to m do for j:=1 to n do read (table[i,j]);
close (input);
assign (output,'number.out');
rewrite (output);
go(m,n,0);
if min=maxint then min:=-1;
writeln (min);
close (output);
end.