You are given a \$3\times3\$ square matrix where each cell is any digit between \$0\$ and \$9\$ except \$7\$. Your task is to figure out the minimum number of digits that must be replaced with \$7\$'s so that the sums of the digits in each row and each column are the same.
NB: There is no constraint whatsoever on the diagonals, so we end up with a semi-magical square.
Examples
Here is a matrix where three digits need to be turned into \$7\$'s so that all sums are \$20\$:
$$\begin{pmatrix}8&6&6\\1&5&8\\6&9&5\end{pmatrix}\rightarrow\begin{pmatrix}\color{red}7&6&\color{red}7\\\color{red}7&5&8\\6&9&5\end{pmatrix}$$
In this one, only one digit needs to be replaced with a \$7\$ so that all sums are \$13\$:
$$\begin{pmatrix}9&2&2\\0&9&4\\4&2&9\end{pmatrix}\rightarrow\begin{pmatrix}9&2&2\\0&9&4\\4&2&\color{red}7\end{pmatrix}$$
And for this one, our only option is to replace all digits with \$7\$'s:
$$\begin{pmatrix}0&6&8\\3&6&1\\8&4&0\end{pmatrix}\rightarrow\begin{pmatrix}\color{red}7&\color{red}7&\color{red}7\\\color{red}7&\color{red}7&\color{red}7\\\color{red}7&\color{red}7&\color{red}7\end{pmatrix}$$
So the expected outputs for the above examples are \$3\$, \$1\$ and \$9\$ respectively.
Rules
- Because the size of the matrix is fixed, you may take input as a flattened array or 9 distinct arguments.
- Because we're dealing with digits exclusively, you may also take a string of 9 characters.
- The input matrix may already fulfill the sum constraints, in which case the expected answer is \$0\$.
- This is code-golf.
Test cases
[[9,4,3],[3,4,9],[4,8,4]] -> 0
[[5,1,3],[3,1,5],[1,2,1]] -> 1
[[3,9,6],[8,5,5],[8,4,0]] -> 2
[[5,3,5],[1,9,5],[3,3,3]] -> 2
[[8,3,0],[8,0,8],[0,8,4]] -> 3
[[1,5,2],[5,9,5],[6,5,3]] -> 4
[[3,0,8],[1,8,0],[1,3,8]] -> 4
[[3,3,0],[5,1,9],[9,9,5]] -> 5
[[2,4,5],[5,3,4],[4,4,8]] -> 6
[[3,0,3],[8,3,5],[8,3,4]] -> 9