Follow the "right hand rule":
1. Start by going right, as far as you can. Put a unique "have been there" value, into each square of the array, you have visited.
2. When you can no longer move into a new square in the current direction, loop back to #1.