class Solution {
public:
int minimumOperationsToWriteY(vector<vector<int>>& grid) {
array<int, 3> countY{0}, countOut{0};
int m = grid.size();
for (int i = 0; i < m; i++) {
for (int j = 0; j < m; j++) {
if ((i == j && i <= m / 2)
|| (i + j == m - 1 && i <= m / 2)
|| (i > m / 2 && j == m / 2)) {
countY[grid[i][j]]++;
} else countOut[grid[i][j]]++;
}
}
int maxNotChange = 0;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (i != j)
maxNotChange = max(maxNotChange, countY[i] + countOut[j]);
}
}
return m * m - maxNotChange;
}
};