procedure flood_fill;
var
i:integer;
temp:node;
begin
while front<tail do
begin
front:=front+1;
for i:=1 to 4 do
begin
temp:=queue[front];
inc(temp[1],d[i,1]);
inc(temp[2],d[i,2]);
if (fence[temp[1],temp[2]])and(not visited[temp[1],temp[2]]) then
begin
inc(tail);
queue[tail]:=temp;
count[tail]:=count[front]+1;
visited[temp[1],temp[2]]:=true;
end;
end;
end;
end;
procedure flood_fill;
var
i:integer;
temp:node;
begin
while front<=tail do
begin
front:=front+1;
for i:=1 to 4 do
begin
temp:=queue[front];
inc(temp[1],d[i,1]);
inc(temp[2],d[i,2]);
if (fence[temp[1],temp[2]])and(not visited[temp[1],temp[2]]) then
begin
inc(tail);
queue[tail]:=temp;
count[tail]:=count[front]+1;
visited[temp[1],temp[2]]:=true;
end;
end;
end;
end;