'Algorithm for checking if board is connected

I am creating a snake AI and when it runs it often traps itself, so I would like to check if the whole board is connected at all times and AI will move accordingly. I can't come up with a good and short algorithm for that. I am using an A* pathfinding algorithm.

Thanks for the help.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source