1. Walk Each Edge, Then Pull It In Optimal
Intuition
Keep four edges: the top and bottom rows, and the left and right columns, starting on the grid's outer rows and columns. Each pass walks one edge clockwise and then pulls that edge in a step: the top row left to right, then top moves down; the right column downward, then right moves in; the bottom row right to left, then bottom moves up; the left column upward, then left moves in. Each edge moves in straight after its pass, so no corner is read twice. The loop runs while neither pair of opposite edges has crossed, but they can cross inside a lap: after the top pass over a single row, top is below bottom, and the bottom pass would read the same row back. So the bottom pass runs only if top <= bottom, and the left pass only if left <= right. Without those two checks, the walkthrough's single row [[2,3,4,5]] would come back as 2,3,4,5,4,3,2, three values read twice. Walk the edges clockwise, pull each edge in after walking it, and stop as soon as opposite edges cross.
Algorithm
1. Start the four edges on the grid's outer rows and columns.
2. Walk the top row left to right, then move top down; walk the right column downward, then move right in.
3. If top <= bottom, walk the bottom row right to left, then move bottom up.
4. If left <= right, walk the left column upward, then move left in.
5. Repeat while top <= bottom and left <= right, then return the values in the order read.
Time & Space
Time O(m x n): each value is read once. Space O(1) beyond the output: only the four edges are kept.