4. Marina fills the cells of a $4 \times 4$ grid by writing the number 1, the number 2, or the number 3 in each. How many ways are there to fill the grid such that the sum of each row and the sum of each column is divisible by 3?
(A) $3^{8}-1$
(B) $3^{8}$
(C) $2 \cdot 3^{8}$
(D) $3^{9}$
(E) None of the above
4. The answer is (D). Let's start filling the $3 \times 3$ sub-table in the top left corner in any way we like: for each of the 9 cells, we have 3 choices, so in total $3^{9}$ possibilities. Now we show that, for each of these, the choice of the other 7 cells is both forced and always possible.

Each of the first three cells in the fourth row (let's call them $A, B, C$) is clearly forced based on the sum of the three cells in the same column, and the same applies to the first three cells in the fourth column $(D, E, F)$.

Now, the last remaining cell, in the bottom right corner, is forced by the sum of the cells in the last row and also by the sum of the cells in the last column: we show that these sums leave the same remainder when divided by 3, which allows us to conclude. Calling $S$ the sum of the 9 numbers chosen initially, $A+B+C+S$ is a multiple of 3, as is $D+E+F+S$, but then $A+B+C$ and $D+E+F$ leave the same remainder when divided by 3, as we wanted. In particular, in the bottom right corner, only the remainder of $S$ when divided by 3 can go (doing so, the sum of the columns in the fourth row has the same remainder when divided by 3 as $A+B+C+S$, which is 0, and similarly for the fourth column).

Thus, for each of the $3^{9}$ initial choices, there is one and only one valid completion, so the answer is $3^{9}$.