I gave the solution which passes all given test cases, but I am sure it will fail, if there are multiple combinations of sizes of A and B.
I believe, to get a full-proof solution, more test cases are needed. Or such solution can work for limited combination of sizes of multidimensional matrices to be multiplied.