Spiral Order — Solution & Editorial
Peel layer by layer with four boundary indices; guard the single-row and single-column cases inside a layer.
Complexity: O(NM)
Watch out: A layer that is a single row or column gets traversed twice without the two inner guards.