Output the anti-clockwise inward spiral of a 2D array From this stackoverflow question

Given a 2D array of size \$ M \times N \$, output the values in a anti-clockwise fashion. The output must start from the outside to the inside and the initial point always is going to be \$(0,0)\$.

Example Given:

$$ \begin{bmatrix} \color{blue}1&amp;\color{red}2&amp;\color{red}3&amp;\color{red}4 \\ \color{red}5&amp;6&amp;7&amp;\color{red}8 \\ \color{red}9&amp;10&amp;11&amp;\color{red}{12} \\ \color{red}{13}&amp;\color{red}{14}&amp;\color{red}{15}&amp;\color{red}{16}\end{bmatrix} $$

The edge values in counterclockwise is then \$ 1,5,9,13,14,15,16,12,8,4,3,2 \$. 

Now we repeat the process for the inner values. This will end up with a matrix like the following  

$$ \begin{bmatrix} \color{blue}6&amp;\color{red}7 \\ \color{red}{10}&amp;\color{red}{11} \end{bmatrix}$$

And the inner values is then \$ 6,10,11,7 \$

The final result will be then \$ 1,5,9,13,14,15,16,12,8,4,3,2,6,10,11,7 \$



Rules


Assume non-empty input
Assume matrix values as positive integers
Standard I/O Methods apply
Standard code-golf rules and winning criteria apply




Some test cases

Input
[
  [1, 2, 3, 4, 5, 6, 7],
  [8, 9, 10,11,12,13,14],
  [15,16,17,18,19,20,21]
]
Output
1,8,15,16,17,18,19,20,21,14,7,6,5,4,3,2,9,10,11,12,13

--------------------------------------------------------

Input
[
    [1,2,3],
    [3,2,1],
    [4,5,6],
    [6,5,4],
    [7,8,9],
    [9,8,7]
]
Output
1,3,4,6,7,9,8,7,9,4,6,1,3,2,2,5,5,8

-----------------------------------------------------
Input
[
    [1]
]
Output
1
-----------------------------------
Input
[
    [1, 2],
    [2, 1]
]
Output
1,2,1,2
-----------------------------------------------------
Input
[
    [1,2,3,6,7],
    [2,4,3,2,1],
    [3,2,4,5,6],
    [6,5,6,5,4],
    [10,4,7,8,9],
    [12,4,9,8,7]
]
Output
1,2,3,6,10,12,4,9,8,7,9,4,6,1,7,6,3,2,4,2,5,4,7,8,5,5,2,3,4,6
R, 54 bytes

Several bytes saved by @Giuseppe and @J.Doe.



f=function(m)if(ncol(m))c(m[,1],f(t(m[nrow(m):1,-1])))


Try it online!

Recursively strip off the first column and row-reverse/transpose (making the bottom row the new first column) the rest of the matrix until you end up with only one column. Ungolfed "traditional" version:

f &lt;- function(m) {
 if(ncol(m) == 1) {
    m
  } else {
    c(m[,1], f(t(m[nrow(m):1,-1])))
  }
}


It was pointed out that ncol(m) could be golfed to sum(m) to save another byte because we are allowed to assume positive integer matrix values. But I'll leave it like this since it works for all matrices (even matrices of strings!)