Given an $m\times n$ matrix $A$ and an $n\times m$ matrix $B$ such that $AB=I_m$ and $BA=I_n$, I would like to show that, in fact, $m=n$.
(The $m$ and $n$ may be the other way around. They should be such that $A$ and $B$ are inverses, as per the title of the question)
I know this can be done easily by seeing $A$ and $B$ as linear transformations between vector spaces, but I am wondering if anyone is aware of a proof that only involves matrix algebra. That is, a proof that would work even if the entries of $A$ and $B$ were merely from a unital ring rather than a field (although I am not even sure that the statement would be true in that case, but I haven't been able to find a counterexample).
Here is another way: if $A$ and $B$ are any two matrices that can be multiplied from both sides then $tr(AB)=tr(BA)$. You can easily prove it using the definition of matrix multiplication. Hence if $AB=I_m$ and $BA=I_n$ then $m=tr(I_m)=tr(AB)=tr(BA)=tr(I_n)=n$.