Problem: Find all integers $n$ for which each cell of $n \times n$ table
can be filled with one of the letters $I$, $M$ and $O$ in such a way that:
\begin{itemize}
\item In each row and column,  one third of the entries are $I$,
one third are $M$ and one third are $O$; and
\item in any diagonal, if the number of entries on the diagonal is a multiple of three,
then one third of the entries are $I$, one third are $M$ and one third are $O$.
\end{itemize}
Note that an $n \times n$ table has $4n-2$ diagonals.

Solution: The answer is $n$ divisible by $9$.

First we construct $n=9$ and by extension every multiple of $9$.
\[
 \begin{array}{|ccc|ccc|ccc|} \hline
  I & I & I & M & M & M & O & O & O \\
  M & M & M & O & O & O & I & I & I \\
  O & O & O & I & I & I & M & M & M \\\hline
  I & I & I & M & M & M & O & O & O \\
  M & M & M & O & O & O & I & I & I \\
  O & O & O & I & I & I & M & M & M \\\hline
  I & I & I & M & M & M & O & O & O \\
  M & M & M & O & O & O & I & I & I \\
  O & O & O & I & I & I & M & M & M \\\hline
 \end{array}
\]

We now prove $9 \mid n$ is necessary.

Let $n = 3k$, which divides the given grid into $k^2$ sub-boxes
(of size $3 \times 3$ each).
We say a multiset of squares $S$ is \emph{clean} if
the letters distribute equally among them;
note that unions of clean multisets are clean.

Consider the following clean sets (given to us by problem statement):
\begin{itemize}
\ii All columns indexed $2 \pmod 3$,
\ii All rows indexed $2 \pmod 3$, and
\ii All $4k-2$ diagonals mentioned in the problem.
\end{itemize}
Take their union.
This covers the center of each box four times,
and every other cell exactly once.
We conclude the set of $k^2$ center squares
are clean, hence $3 \mid k^2$ and so $9 \mid n$,
as desired.

Shown below is the sums over all diagonals only,
and of the entire union.
\[
 \begin{array}{|ccc|ccc|ccc|} \hline
  1 &   & 1 & 1 &   & 1 & 1 &   & 1 \\
  & 2 &   &   & 2 &   &   & 2 &   \\
  1 &   & 1 & 1 &   & 1 & 1 &   & 1 \\\hline
  1 &   & 1 & 1 &   & 1 & 1 &   & 1 \\
  & 2 &   &   & 2 &   &   & 2 &   \\
  1 &   & 1 & 1 &   & 1 & 1 &   & 1 \\\hline
  1 &   & 1 & 1 &   & 1 & 1 &   & 1 \\
  & 2 &   &   & 2 &   &   & 2 &   \\
  1 &   & 1 & 1 &   & 1 & 1 &   & 1 \\\hline
 \end{array}
 \qquad
 \begin{array}{|ccc|ccc|ccc|} \hline
    1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 \\
    1 & 4 & 1 & 1 & 4 & 1 & 1 & 4 & 1 \\
    1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 \\\hline
    1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 \\
    1 & 4 & 1 & 1 & 4 & 1 & 1 & 4 & 1 \\
    1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 \\\hline
    1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 \\
    1 & 4 & 1 & 1 & 4 & 1 & 1 & 4 & 1 \\
    1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 \\\hline
 \end{array}
\]