help why i got WA
var
i,j,k,m,n,min,minn,x,y,x1,y1:longint;
ans:array[0..50000] of integer;
money:Array[0..500,0..100] of longint;
a:Array[0..500,0..100] of longint;
cx,cy:Array[0..500,0..100] of integer;
begin
fillchar(money,sizeof(money),255);
fillchar(cx,sizeof(cx),0);
fillchar(cy,sizeof(cy),0);
fillchar(a,sizeof(a),0);
readln(m,n);
for i:=1 to m do
begin
for j:=1 to n do
read(a[i,j]);
readln;
end;
for i:=1 to n do
money[1,i]:=a[1,i];
for i:=2 to m do
begin
for j:=1 to n do
begin
money[i,j]:=money[i-1,j]+a[i,j];
cx[i,j]:=i-1;
cy[i,j]:=j;
end;
for j:=2 to n do
if ((money[i,j]>money[i,j-1]+a[i,j]) or (money[i,j]=-1)) and
(money[i,j-1]<>-1) then
begin
money[i,j]:=money[i,j-1]+a[i,j];
cx[i,j]:=i;
cy[i,j]:=j-1;
end;
for j:=n-1 downto 1 do
if ((money[i,j]>money[i,j+1]+a[i,j]) or (money[i,j]=-1)) and
(money[i,j+1]<>-1) then
begin
money[i,j]:=money[i,j+1]+a[i,j];
cx[i,j]:=i;
cy[i,j]:=j+1;
end;
end;
min:=money[m,1];
minn:=1;
for i:=2 to n do
if min>money[m,i] then
begin
min:=money[m,i];
minn:=i;
end;
y:=minn;
x:=m;
k:=1;
ans[k]:=y;
while cx[x,y]<>0 do
begin
k:=k+1;
x1:=x;
y1:=y;
x:=cx[x1,y1];
y:=cy[x1,y1];
ans[k]:=y;
end;
for i:=k downto 1 do
write(ans[i],' ');
writeln;
end.