Solution (source code)

= Solution

The product $AB$ is $m\times p$, so multiplying it by the $p\times q$ <matrix> $C$ gives an \b[$m\times q$ <matrix>]. The product $BC$ is $n\times q$, so multiplying it on the left by the $m\times n$ <matrix> $A$ also gives an \b[$m\times q$ <matrix>]. Both multiplications have matching inner <dimensions>.