how many $3\times 3$ matrices with entries from $\{0,1,2\}$. 
How many $3 × 3$ matrices $M$ with entries from $\left\{0, 1, 2\right\}$ are there for which taken from 
the sum of the main diagonal of $M^TM$ is $5$.

Attempt: Let $M = \begin{pmatrix}
a & b & c\\ 
d & e & f\\ 
g & h & i
\end{pmatrix}$. where  $a,b,c,d,e,f,g,h,i\in \{0,1,2\}$
$$M^{T}M= \begin{pmatrix}
a & d & g\\ 
b & e & h\\ 
c & f & i
\end{pmatrix}\begin{pmatrix}
a & b & c\\ 
d & e & f\\ 
g & h & i
\end{pmatrix} $$.
sum of diagonal entries $$a^2+b^2+c^2+d^2+e^2+f^2+g^2+h^2+i^2 = 5$$
How can I form different cases?
 A: Positioning the $0$'s, we get $\dbinom94=126$ for the $'000011111'$ case.
For the $'000000012'$ case, we have $\dbinom97=36$ ways to position the $0$'s, we need to multiply this by $2!$ to account for the two cases $12, 21$.
Total: $126+72=198$.
A: The numbers $a^2$ etc. are either $0$, $1$or $4$. To get them adding to $5$ you need five $1$s, or a $4$ and a $1$ and all the rest $0$s.
A: Either $5=1^2+1^2+1^2+1^2+1^2+0^2+0^2+0^2+0^2$ or $5=1^2+2^2+0^2+0^2+0^2+0^2+0^2+0^2+0^2$
thus there are two cases: 5 of them are 1 and the other 4 are 0, or 1 is 1, 1 is 2, and the other 7 are 0.
Case 1: there are $\binom{9}{5} = 126$ possibilities.
Case 2: there are $\binom{9}{1}\binom{8}{1}\binom{7}{7} = 72$ possibilities.
In total there are $126+72=198$ possibilities.
A: Following Lord Shark the Unknown's answer.
For five 1s and the rest zero, there are $C^9_5$ matrices.
For one 1 and one 2, there are $9\times 8$ matrices.
A: Since the square of each number can only equal $0$, $1$ or $4$, it comes down to selecting either five numbers which equal $1$ (with the four remaining ones being $0$), or select one which equals $2$ and one which equals $1$ (with the seven remaining ones being $0$). As such, the number of valid matrices equals:
$${9 \choose 5} + {9 \choose 1} {8 \choose 1} = 126 + 72 = 198$$
