What's wrong here? Plz!
const floors=100;
rooms=500;
var a,b:array[1..floors,1..rooms] of longint;
c1,c2:array[1..floors,1..rooms] of integer;
e1,e2,ne1,ne2,min,f,i,j,n,m:integer;
begin
read(n);
read(m);
for i:=1 to n do
for j:=1 to m do
begin
read(a[n-i+1,j]);
if i=n then
b[n-i+1,j]:=a[n-i+1,j]
else b[n-i+1,j]:=-1;
end;
f:=1;
while f=1 do begin
f:=0;
for i:=2 to n do for j:=1 to m do begin
if ((b[I-1,J]+a[i,j])<b[i,j]) or ((b[i,j]=-1) and (b[i-1,j]<>-1))
then begin
f:=1;
b[i,j]:=b[I-1,J]+a[i,j];
c1[i,j]:=i-1;
c2[i,j]:=j;
end;
if (i<n) and (
(((b[I+1,J]+a[i,j])<b[i,j]) and (b[i+1,j]<>-1))
or ((b[i,j]=-1) and (b[i+1,j]<>-1))
) then begin
f:=1;
b[i,j]:=b[I+1,J]+a[i,j];
c1[i,j]:=i+1;
c2[i,j]:=j;
end;
if (j<m) and (
(((b[I,J+1]+a[i,j])<b[i,j]) and (b[i,j+1]<>-1))
or ((b[i,j]=-1) and (b[i,j+1]<>-1))
) then begin
f:=1;
b[i,j]:=b[I,J+1]+a[i,j];
c1[i,j]:=i;
c2[i,j]:=j+1;
end;
if (j>1) and (
(((b[I,J-1]+a[i,j])<b[i,j]) and (b[i,j-1]<>-1))
or ((b[i,j]=-1) and (b[i,j-1]<>-1))
) then begin
f:=1;
b[i,j]:=b[I,J-1]+a[i,j];
c1[i,j]:=i;
c2[i,j]:=j-1;
end;
end;
end;
min:=1;
for i:=2 to m do if b[n,i]<b[n,min] then min:=i;
e1:=n;
e2:=min;
writeln(e2);
repeat
writeln(c2[e1,e2]);
ne2:=c2[e1,e2];
ne1:=c1[e1,e2];
e2:=ne2;
e1:=ne1;
until e1=1;
end.