source comment...

first of all: nottings wrong with your program.

your way of programming is with the mind.
you must program 'recursive'>>the way that the computer must use his head(cpu).

analyse:

there are 4 ways the 'person' can go.
starts in a point and ends in a point.
the grid has a max x&y value.


for example a procedure:

Code:
PathFinding(thisPiont,EndPoint:TPoint;Matrix:grid);

var .  ..  . ;
begin

//ending.
  if thisPoint=EndPoint then
    Solution(Matrax)
  else
//moving to all 4 places. (left, right, top, bottom)
  for i:=0 to 3 do
//check if that place if free.
  if OK(thisPoint,i,Matrix) then
  begin
// everything went fine, mark the place where you are (thisPoint) and move on.
//mark
    Matrix[thisPoint.x,thisPoint.y]:=False;
//move on
    newPoint:=thisPoint;
    case i of
      1:Dec(newPoint.x);
      2:Inc(newPoint.x);
      3:Dec(newPoint.y);
      4:Inc(newPoint.y);
    end;
    PathFinding&#40;newPoint,endPoint,Matrix&#41;;//<<< this is recursive.
//unmark
    Matrix&#91;thisPoint.x,thisPoint.y&#93;&#58;=True;
  end;
end;
now there are 2 other procedures to write.
Solution(Matrix:grid);

and
OK(thisPoint,i,Matrix);

the solution procedure is simple this is calling when the computer found A way to the end, <<ps. there is often more then one!!!!

the OK procedure is the ai for the computer.
return false when:
1:the newPoint is already taken.
2:there was already found a shorter way.
etc. ect.
otherwise return true.

the second point i recommend is not nessesery. but slows the computer down. becauce the Matrix that was found is not the Matrix where we are looking for.

the more false returning in the OK procedure the smatrer the computer.

this way of programming is called 'BACKTRACKING'.