Answer
If both $AB$ and $BA$ are defined, the number of rows of $A$ must be equal to the number of columns of $B$, and vice versa.
Work Step by Step
If the size of $A$ is $a_r\times a_c$ and the size of $B$ is $b_r\times b_c$, $a_c=b_r$ because $AB$ is defined and $a_r=b_c$ because $BA$ is defined.