DFS over a grid with mark-and-restore: stamp '#' on the current cell, probe the 4 neighbors for the next character, then put the original back on the way out.