AB = A4×pBq×4 and BA=Bq×4A4×p
For AB. It depends on the number of columns of A and number of rows of B i.e., p,q and both should be equal .
For BA, It is always defined because the number of columns in prefactor B is the same as the number of rows in post - factor A i.e., 4 = 4.