tmp
program ministry;
const
maxn=500;
max=1000000000;
var
map:array[1..5,1..5]of longint;
back:array[1..5,1..5]of integer;
p:array[1..5,1..5]of longint;
t:array[1..5]of longint;
m,n:integer;
procedure init;
var
i,j:integer;
begin
readln(m,n);
for i:=1 to m do
begin
for j:=1 to n do
read(map[j,i]);
readln;
end;
end;
procedure solve;
var
i,j,k:integer;
min:longint;
begin
for i:=2 to m do
for j:=0 to n+1 do
p[j,i]:=Max;
for i:=1 to n do
p[i,1]:=map[i,1];
for i:=2 to m do
for j:=1 to n do
begin
p[j,i]:=p[j,i-1]+map[j,i];
back[j,i]:=j;
if j>1 then
begin
t[j-1]:=map[j-1,i]+map[j,i];
if t[j-1]+p[j-1,i-1]<p[j,i] then
begin
p[j,i]:=t[j-1]+p[j-1,i-1];
back[j,i]:=j-1;
end;
end;
if j>2 then
for k:=j-2 to 1 do
begin
t[k]:=t[k+1]+map[k,i];
if t[k]+p[k,i-1]<p[j,i] then
begin
p[j,i]:=t[k]+p[k,i-1];
back[j,i]:=k;
end;
end;
if j<n then
begin
t[j+1]:=map[j,i]+map[j+1,i];
if t[j+1]+p[j+1,i-1]<p[j,i] then
p[j,i]:=t[j+1]+p[j+1,i-1];
end;
for k:=j+2 to n do
begin
t[k]:=t[k-1]+map[k,i];
if t[k]+p[k,i-1]<p[j,i] then
p[j,i]:=t[k]+p[k,i-1];
end;
end;
end;
begin
init;
solve;
end.