Imagine walls closing in on the matrix: top, bottom, left, and right. Each wall marks a boundary you haven't crossed yet.
You walk along one wall at a time. Go right along the top row, then move top down by . Go down along the right column, then move right left by . Go left along the bottom row, then move bottom up by . Go up along the left column, then move left right by .
After each full cycle, the walls have all moved inward. You keep going until the walls cross each other. At that point, you've visited every element.