 <p>It depends when you start from (0,0) or at a random location in the matrix.</p> <p>If it is (0,0) ; use Depth First to go until the end of one branch then the others. Use Breadth First to iterate through all neigbouring nodes at a step. Check only down and right nodes.</p> <p>If it is at a random location in the matrix, you need to trace all 4 neighbouring nodes.</p>