Following from David Richerby answer and your comment, you are right, for this particular problem it is possible to not have to run on every node. But there can be worst cases that force you to visit every node, for example:
0000000
0111111
0000000
1111110
0000000
$x$111111
An algorithm that will prevent visiting every node is to always choose the free space closest to the stopping space. The straight-line distance can be used, which is $\sqrt{(x2-x1)^2 + (y2-y1)^2}$ for two spaces $(x1,y1)$ and $(x2,y2)$. If there are more than one of them then any one is chosen. With this we have the example scenario below on the left:
00089 000000
00670 011111
04500 00000d
23000 101011
10000 $x$0100d
However this algorithm will have an issue with the matrix above on the right. The d means a dead-end. To resolve this issue the algorithm will need to go back to where there was more than one free space, and choose the next non-visited free space closest to the stopping point. A stack can be used to keep track of the path taken, thereby allowing for going back. The travel complexity is O(2nm).
Each time a node is visited it is first checked for whether it was visited before. If the first time, then it is marked as visited and processed. The node (or space) can be marked with a 1 (so is a visited or occupied space). For an array storing the matrix, both marking and checking for if marked will take constant time O(1). For a graph with linked nodes, performance can be improved by using a self-balancing search tree such as the AVL tree. This will enable O(log(nm)) searches and insertions of nodes.
You only know it is a dead-end until you get there (the fun of playing Maze). Notice that the approach of a depth-first-search is used - as deep and close to the stopping point as possible. Performance can be improved if every node in the stack stores a pointer to the last node with a non-visited free space, so that it can directly be jumped onto on going back (violating the pain in playing Maze!)
0 0 0 0 0 ; 0 0 1 0 0 ; 0 1 0 1 0 ; 0 0 1 0 0 ; x 0 0 0 0
, I would say that we can get to the other side despite the isolated free space. $\endgroup$