Given an m x n matrix, return all elements of the matrix in spiral order.
Example 1
Input: matrix = [[1,2,3],[4,5,6],[7,8,9]]
Output: [1,2,3,6,9,8,7,4,5]
Explanation: Go right along the top (1, 2, 3), down the right side (6, 9), left along the bottom (8, 7), up the left side (4), then into the centre (5).
Example 2
Input: matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]
Output: [1,2,3,4,8,12,11,10,9,5,6,7]
Explanation: Go right along the top (1-4), down the right side (8, 12), left along the bottom (11, 10, 9), up to 5, then right through the remaining middle row (6, 7).
Example 3
Input: matrix = [[7]]
Output: [7]
Explanation: A single cell is its own spiral.
Keep four walls — top, bottom, left, right. Walk along the top wall, then the right wall, then the bottom, then the left, shrinking the corresponding wall inward after each pass. Repeat until the walls cross.
Run checks all cases above. Submit evaluates all test cases.