For fixed $n$, you can solve the problem via integer linear programming as follows. Define a graph $G=(N,E)$ with a node for each square and a link for each pair of adjacent squares. Let binary decision variable $x_i$ indicate whether node $i\in N$ is black. We want to maximize $\sum_{(i,j)\in E} (x_i (1-x_j)+x_j (1-x_i))$. To linearize this quadratic objective, introduce binary variable $y_{i,j}$ to represent the summand. The problem is to maximize $\sum_{(i,j)\in E} y_{i,j}$ subject to linear constraints
$$y_{i,j} \le x_i + x_j \le 2 - y_{i,j}.$$
If $y_{i,j} = 1$, the constraints force $x_i + x_j = 1$, so $x_i (1-x_j)+x_j (1-x_i) = 1$, as desired.
By the way, we are solving the maximum cut problem for $G$.
Example: for $n=3$, number the squares from 1 to 9 as follows:
\begin{matrix}
1 & 2 & 3\\
4 & 5 & 6\\
7 &8 &9
\end{matrix}
The problem is then to maximize $$y_{1,2} + y_{1,4} +
y_{1,5} + y_{2,3} + y_{2,4} +
y_{2,5} + y_{2,6} + y_{3,5} +
y_{3,6} + y_{4,5} + y_{4,7} +
y_{4,8} + y_{5,6} + y_{5,7} +
y_{5,8} + y_{5,9} + y_{6,8} +
y_{6,9} + y_{7,8} + y_{8,9}$$
subject to
\begin{align}
y_{1,2} &\le x_1 + x_2 \le 2 - y_{1,2}\\
y_{1,4} &\le x_1 + x_4 \le 2 - y_{1,4}\\
y_{1,5} &\le x_1 + x_5 \le 2 - y_{1,5}\\
y_{2,3} &\le x_2 + x_3 \le 2 - y_{2,3}\\
y_{2,4} &\le x_2 + x_4 \le 2 - y_{2,4}\\
y_{2,5} &\le x_2 + x_5 \le 2 - y_{2,5}\\
y_{2,6} &\le x_2 + x_6 \le 2 - y_{2,6}\\
y_{3,5} &\le x_3 + x_5 \le 2 - y_{3,5}\\
y_{3,6} &\le x_3 + x_6 \le 2 - y_{3,6}\\
y_{4,5} &\le x_4 + x_5 \le 2 - y_{4,5}\\
y_{4,7} &\le x_4 + x_7 \le 2 - y_{4,7}\\
y_{4,8} &\le x_4 + x_8 \le 2 - y_{4,8}\\
y_{5,6} &\le x_5 + x_6 \le 2 - y_{5,6}\\
y_{5,7} &\le x_5 + x_7 \le 2 - y_{5,7}\\
y_{5,8} &\le x_5 + x_8 \le 2 - y_{5,8}\\
y_{5,9} &\le x_5 + x_9 \le 2 - y_{5,9}\\
y_{6,8} &\le x_6 + x_8 \le 2 - y_{6,8}\\
y_{6,9} &\le x_6 + x_9 \le 2 - y_{6,9}\\
y_{7,8} &\le x_7 + x_8 \le 2 - y_{7,8}\\
y_{8,9} &\le x_8 + x_9 \le 2 - y_{8,9}
\end{align}
One optimal solution has $x_4=x_5=x_6=1$, $$y_{1,4}=y_{1,5}=y_{2,4}=y_{2,5}=y_{2,6}=y_{3,5}=y_{3,6}=y_{7,4}=y_{7,5}=y_{8,4}=y_{8,5}=y_{8,6}=y_{9,5}=y_{9,6}=1,$$ and all other variables 0.