Since we don’t know the map \(T\) or anything about the bases \(B_U,B_W\text{,}\) we’re looking for a fairly general statement here. Since \(U\) is \(T\)-invariant, we must have \(T(\uu_i)\in U\) for each \(i=1,\ldots, m\text{.}\) Similarly, \(T(\ww_j)\in W\) for each \(j=1,\ldots, n\text{.}\) This means that we have
\begin{align*}
T(\uu_1) \amp = a_{11}\uu_1 + \cdots + a_{m1}\uu_m + 0\ww_1+\cdots + 0\ww_n\\
\amp \vdots \\
T(\uu_m) \amp = a_{1m}\uu_1 + \cdots + a_{mm}\uu_m+0\ww_1+\cdots + 0\ww_n\\
T(\ww_1) \amp = 0\uu_1 + \cdots + 0\uu_m+b_{11}\ww_1 + \cdots + b_{n1}\ww_n \\
\amp \vdots \\
T(\ww_n) \amp = 0\uu_1 + \cdots + 0\uu_m+b_{1n}\ww_1 + \cdots + b_{nn}\ww_n
\end{align*}
for some scalars \(a_{ij},b_{ij}\text{.}\) If we set \(A = [a_{ij}]_{m\times m}\) and \(B = [b_{ij}]_{n\times n}\text{,}\) then we have
\begin{equation*}
M_B(T) = \bbm A \amp 0\\0\amp B\ebm\text{.}
\end{equation*}
Moreover, we can also see that \(A = M_{B_U}(T|_U)\text{,}\) and \(B = M_{B_W}(T|_W)\text{.}\)