When you build the graph correctly, this should not happen.

before the "floodfill" initialize every grid with a very high number. (20000 for example)
The targetpoint has the lowest number (0 for example)

After the floodfill, every gridpoint must then have a neighbor with at least one "lower" count than itself.
If the graph is build correcly you cant get stuck (not finding
a lower neighbor) unless you already reached the target, or
you are on an impassable grid (wich should not be a valid action).
Or if you are on an isolated island. (indicated by you grid
beeing "20000")

Best is if you make a visualization routine, to show
the assigned number of each grid suqare (or a colorvalue)
So you can manually check if the graph is build correctly.

Also: when creating the grid, make the cost of moving diagonally
1.41 times the cost of moving left or right.
(1.4142 -> sqrt(2) )