[Question]
  [
Sometimes, I have lists of constant matrices in my code:
```
[ [[1, 0],
   [0, 1]],
 
  [[1, 0],
   [0,-1]],
 
  [[0, 1],
   [1, 0]],
 
  [[0,-1],
   [1, 0]] ]
```
That's a terrible use of screen real estate. I'd much rather write them next to each other:
```
[   [[1, 0],    [[1, 0],    [[0, 1],    [[0,-1],
     [0, 1]],    [0,-1]],    [1, 0]],    [1, 0]]   ]
```
You'll find that this is still a syntactically valid nested list, it's just no longer rectangular and it has a very different structure (in particular, it gets deeper every time I add a matrix larger than 1x1). However, it's still possible to reconstruct the initial list of matrices from this new list.
So that I can use this syntax in the future, I need you to write some code which converts arrays that were written in a horizontal arrangement to the list of matrices they represent.
To make sure that answers don't perform 2D pattern matching on the layout in the source code, the input will be given either as just the array object, or if you take a string representation, then it will not contain any whitespace indicating how the literal was written in the code. So you'd get some input like this:
```
[[[1, 0], [[1, 0], [[0, 1], [[0,-1], [0, 1]], [0,-1]], [1, 0]], [1, 0]]]
```
And the output should be the following array or its string representation (again, no further layout is needed):
```
[[[1, 0], [0, 1]], [[1, 0], [0,-1]], [[0, 1], [1, 0]], [[0,-1], [1, 0]]]
```
This is the first and easier part of a two-part challenge. In this one, you may assume that all matrices are square and have the same dimensions and that they are properly aligned next to each other. In the second part we'll relax these assumptions.
## Rules
The input will be a nested list or its canonical string representation (in your language of choice), and you should output the result in the same format. The result will always contain at least one matrix, and the matrices can be as small as 1x1. The matrices will only contain (signed) integers with absolute value less than 128.
You may write a [program or a function](https://codegolf.meta.stackexchange.com/q/2419) and use any of the [standard methods](https://codegolf.meta.stackexchange.com/q/2447) of receiving input and providing output.
You may use any [programming language](https://codegolf.meta.stackexchange.com/q/2028), but note that [these loopholes](https://codegolf.meta.stackexchange.com/q/1061/) are forbidden by default.
This is [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'"), so the shortest valid answer – measured in *bytes* – wins.
## Test Cases
Each test case has a) the list matrices laid out nicely next to each other as they would be in the code (this is *not* your input), b) the unformatted list without the extraneous whitespace (*this* is your input), c) the expected output.
```
Pretty: [ [[0]] ]
Input:  [[[0]]]
Output: [[[0]]]
Pretty: [  [[-1]],  [[0]],  [[1]]  ]
Input:  [[[-1]],[[0]],[[1]]]
Output: [[[-1]],[[0]],[[1]]]
Pretty: [  [[1, 0],   [[1, 0],   [[0, 1],   [[0,-1],
            [0, 1]],   [0,-1]],   [1, 0]],   [1, 0]]  ]
Input:  [[[1,0],[[1,0],[[0,1],[[0,-1],[0,1]],[0,-1]],[1,0]],[1,0]]]
Output: [[[1,0],[0,1]],[[1,0],[0,-1]],[[0,1],[1,0]],[[0,-1],[1,0]]]
Pretty: [  [[1, 0, 0],   [[ 127,  63,   31],   [[1, 0, 0],   [[0, 0, 0],
            [0, 1, 0],    [  15,   0,  -15],    [0, 0, 1],    [0, 0, 0],
            [0, 0, 1]],   [ -31, -63, -127]],   [0, 1, 0]],   [0, 0, 0]]  ]
Input:  [[[1,0,0],[[127,63,31],[[1,0,0],[[0,0,0],[0,1,0],[15,0,-15],[0,0,1],[0,0,0],[0,0,1]],[-31,-63,-127]],[0,1,0]],[0,0,0]]]
Output: [[[1,0,0],[0,1,0],[0,0,1]],[[127,63,31],[15,0,-15],[-31,-63,-127]],[[1,0,0],[0,0,1],[0,1,0]],[[0,0,0],[0,0,0],[0,0,0]]]
```
  
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), ~~20~~ ~~15~~ ~~13~~ 11 bytes
```
Fðs⁹œsZµḢḢL
```
[Try it online!](https://tio.run/nexus/jelly#@@92eEPxo8adRycXRx3a@nDHIiDy@X@4/VHTmqOTHu6cAaSzHjXMObTt0LZHDXMVHu7YDuQp@CYWKORk5mUrJOalKBQUZeaVKARUlmTk56kXKxSXAPnpCkWpBUWpxal5JYklmfl5xXr//0dzKURHRxvExsbqgFm6hkAWWABIGsKFDXUMwAIQykDHEELpgmgQD0xB9ILUwCiEbqh@I3MdM2MdY0OoYTDjIAygQWDa0FQHZJgpWAhiF0IJ1DZdY0MdXaBRukAjY@F64SpjY7liAQ "Jelly – TIO Nexus")
### Background
Like most things, this challenge is rather simple once you've figured out what you have to do. And I eventually did, after three deletions and one rollback...
First, we must figure out the dimensions of the matrices. That's easier done than said: the first element of the first element is the first row of the first output matrix, so its length is equal to the number of columns of the square output matrices.
For example, if the input is
```
[  [[1, 0, 0],   [[ 127,  63,   31],   [[1, 0, 0],   [[0, 0, 0],
    [0, 1, 0],    [  15,   0,  -15],    [0, 0, 1],    [0, 0, 0],
    [0, 0, 1]],   [ -31, -63, -127]],   [0, 1, 0]],   [0, 0, 0]]  ]
```
the first element of the first element is `[1, 0, 0]`, whose length is **ℓ = 3**.
If we flatten the input and split it into chunks of that length, we get all rows of the output matrices, albeit in the wrong order. For our example input, this gives
```
[  [1, 0, 0], [127,  63,   31], [1, 0, 0], [0, 0, 0],
   [0, 1, 0], [ 15,  0,   -15], [0, 0, 1], [0, 0, 0],
   [0, 0, 1], [-31, -63, -127], [0, 1, 0], [0, 0, 0]  ]
```
To obtain the final output, we must first split the row array into **ℓ** chunks of equal length. For our example input, this gives
```
[ [[1, 0, 0], [127,  63,   31], [1, 0, 0], [0, 0, 0]],
  [[0, 1, 0], [ 15,  0,   -15], [0, 0, 1], [0, 0, 0]],
  [[0, 0, 1], [-31, -63, -127], [0, 1, 0], [0, 0, 0]] ]
```
Each column is now one of the output matrices, so transposing the resulting matrix of arrays is all that's left to do. For our example input, that gives
```
[
 [[1, 0, 0],
  [0, 1, 0],
  [0, 0, 1]],
 [[127,  63,   31],
  [ 15,   0,  -15],
  [-31, -63, -127]],
 [[1, 0, 0],
  [0, 0, 1],
  [0, 1, 0]],
 [[0, 0, 0],
  [0, 0, 0],
  [0, 0, 0]]
]
```
as desired.
### How it works
```
Fðs⁹œsZµḢḢL  Monadic link. Argument: A (ragged array)
F            Monadic chain. Argument: A
F            Flatten A. This yields the vector V.
       µḢḢL  Monadic chain. Argument: A
        Ḣ    Head; retrieve the first element of A.
         Ḣ   Head; retrieve the first element of the first element of A.
          L  Compute ℓ, the length of the resulting array.
 ðs⁹œsZ      Dyadic chain. Left argument: V. Right argument: ℓ
  s⁹         Split V into chunks of length ℓ.
    œs       Split the result into ℓ chunks of equal length.
      Z      Zip/transpose the result.
```
[Answer]
# [Pyth](https://github.com/isaacg1/pyth), 12 bytes
```
CcJlhhQc.nQJ
```
This is a port of my Jelly answer.
[Try it online!](https://tio.run/##TY2xDoIwEIZn@xQX4tgWKkEGiYsbGzPpgIApCQGiNerT1/Za0KVf7/L/312bhzJto@EMt@HNl49WUBTAzaUtR6Wqlk9VaTghLzWMPdz7poNhWp76BN1MdvG86NiV8LH1f0sB0R6zEenmqTd1XSdSSmLJhJQUR/uKsBQ0wdEjocKDOboJ4Zsus2LthvYhp8eUpiKoVpn/WA1SZNSpMlz5S79IuMVSQZlVMauUW3dL2rtf "Bash – Try It Online")
### How it works
Pyth parses the program as follows (pseudo-code).
```
C(c(J = l(h(h(Q))), c(.n(Q), J)))
```
**Q** is a variable that holds the input. **J** is an undefined variable.
First `J = l(h(h(Q)))` stores the length of the head (first element) of the head of **Q** in **J**.
Then, `.n(Q)` flattens **Q**, and `c(..., J)` splits the result into pieces of length **J**.
Afterwards, `c(J, ...)` splits the result in **J** pieces.
Finally, `C(...)` transposes the result.
[Answer]
# [Pyth](https://github.com/isaacg1/pyth), 29 bytes
```
.xj\,sCcJh-UJ:z"\B,"3x\[@R1Jz
```
[Test suite.](http://pyth.herokuapp.com/?code=.xj%5C%2CsCcJh-UJ%3Az%22%5CB%2C%223x%5C%5B%40R1Jz&test_suite=1&test_suite_input=%5B%5B%5B0%5D%5D%5D%0A%5B%5B%5B-1%5D%5D%2C%5B%5B0%5D%5D%2C%5B%5B1%5D%5D%5D%0A%5B%5B%5B1%2C0%5D%2C%5B%5B1%2C0%5D%2C%5B%5B0%2C1%5D%2C%5B%5B0%2C-1%5D%2C%5B0%2C1%5D%5D%2C%5B0%2C-1%5D%5D%2C%5B1%2C0%5D%5D%2C%5B1%2C0%5D%5D%5D%0A%5B%5B%5B1%2C0%2C0%5D%2C%5B%5B127%2C63%2C31%5D%2C%5B%5B1%2C0%2C0%5D%2C%5B%5B0%2C0%2C0%5D%2C%5B0%2C1%2C0%5D%2C%5B15%2C0%2C-15%5D%2C%5B0%2C0%2C1%5D%2C%5B0%2C0%2C0%5D%2C%5B0%2C0%2C1%5D%5D%2C%5B-31%2C-63%2C-127%5D%5D%2C%5B0%2C1%2C0%5D%5D%2C%5B0%2C0%2C0%5D%5D%5D&debug=0)
## How it works
```
.xj\,sCcJh-UJ:z"\B,"3x\[@R1Jz
              z        input
             :      3  split at matches of the following regex
               "\B,"   /\B,/
            J          store at J
           U           [1,2,...,len(J)]
                           J  J
                        @R1   take the second character of each
                              substring in J
                     x\[      indices of all occurrences of "["
          -   filter away the elements in ^ from the
              elements in ^^ to find the first substring
              which does not start with "[["
         h    the first element
              note: this will generate an error if
              all substrings start with "[[", e.g. in the
              first example. We will deal with the error later.
       cJ     split J in groups of the specified length
      C       transpose                             ^
     s        flatten                               |
  j\,         join with ","                         |
.x            if the above code generated an error (|), return
              the following instead:
                            z      the input
```
## Algorithm
Let's work on the input `[[[1,0],[[1,0],[[0,1],[[0,-1],[0,1]],[0,-1]],[1,0]],[1,0]]]`.
We will be using pure string operations here.
Firstly, we split the input at the commas which are not part of the deepest list (this is done by splitting at the regex `\B,`):
```
[[[1,0]
[[1,0]
[[0,1]
[[0,-1]
[0,1]]
[0,-1]]
[1,0]]
[1,0]]]
```
Then, we find the index of the first substring which does not start with `[[` (this is done by checking whether the character at index `1` is `[`). In this case, it is `4`, because the substring at index 4 is `[0,1]]` which does not start with `[[`.
Then, we group the substrings in groups of 4, and then transpose:
```
[[[1,0]
[0,1]]
[[1,0]
[0,-1]]
[[0,1]
[1,0]]
[[0,-1]
[1,0]]]
```
And then we join them with commas:
```
[[[1,0],[0,1]],[[1,0],[0,-1]],[[0,1],[1,0]],[[0,-1],[1,0]]]
```
[Answer]
## JavaScript (ES6), ~~132~~ 130 bytes
```
f=(a,n=1)=>a[0][n]?a[0][n][0][0]?f(a,n+1,a[0].splice(n,1,...a[0][n])):n>1?[...Array(n)].map((_,i)=>a[0].filter((_,j)=>j%n==i)):a:a
```
There are four cases:
* A 1×n array, which is just returned (this is the first test, but inverted)
* An m×n array which hasn't been flattened yet, which we recursively flatten by one step, counting `n` at the same time.
* An m×n array which has been flattened, where we filter out every `n`th element.
* An m×1 array, which is just returned
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E), 11 bytes
A port of [**Dennis**' answer](https://codegolf.stackexchange.com/a/121901/34388). Code:
```
¬¬g¹˜sô¬gäø
```
Uses the **CP-1252** encoding. [Try it online!](https://tio.run/nexus/05ab1e#PYyxDYAwDAQXeks2ETBM5JoBWIeSMiUFtEnJUOEVhVR/f7K/5pTTlp/32MtFKme5a40xGhTqIEwrloBgrfxWOyispc0UYnNTlD11dIIEg3BKOOnjd1y6fw "05AB1E – TIO Nexus")
[Answer]
## Mathematica, 104 bytes
```
l=Length;(m=l@#[[1,1]];n=l@Flatten@#/m;Table[Partition[#~ArrayReshape~{n,m},n/m][[j,i]],{i,n/m},{j,m}])&
```
**Input** 
> 
> {{{1, 0}, {{1, 0}, {{0, 1}, {{0, -1}, {0, 1}}, {0, -1}}, {1, 0}}, {1, 
>  0}}}
> 
> 
> 
**output** 
> 
> {{{1, 0}, {0, 1}}, {{1, 0}, {0, -1}}, {{0, 1}, {1, 0}}, {{0, -1}, {1, 
>  0}}}
> 
> 
> 
**input** 
> 
> {{{1, 0, 0}, {{127, 63, 
>  31}, {{1, 0, 
>  0}, {{0, 0, 0}, {0, 1, 0}, {15, 0, -15}, {0, 0, 1}, {0, 0, 
>  0}, {0, 0, 1}}, {-31, -63, -127}}, {0, 1, 0}}, {0, 0, 0}}}
> 
> 
> 
**output** 
> 
> {{{1, 0, 0}, {0, 1, 0}, {0, 0, 1}}, {{127, 63, 31}, {15, 
>  0, -15}, {-31, -63, -127}}, {{1, 0, 0}, {0, 0, 1}, {0, 1, 0}}, {{0,
>  0, 0}, {0, 0, 0}, {0, 0, 0}}}
> 
> 
> 
{{{0}}} and {{{-1}}, {{0}}, {{1}}} work,too
-11 bytes thanks to Martin Ender
]