class Solution {
public:
int minimumOperationsToWriteY(vector<vector<int>>& grid) {
int num0 = 0, num1 = 0, num2 = 0, num0Y = 0, num1Y = 0, num2Y = 0;
int n = grid.size();
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if ((i == j && i <= n / 2) || (i + j == n - 1 && i <= n / 2) || (i >= n / 2 && j == n / 2)) {
if (grid[i][j] == 0) num0Y++;
else if (grid[i][j] == 1) num1Y++;
else num2Y++;
} else {
if (grid[i][j] == 0) num0++;
else if (grid[i][j] == 1) num1++;
else num2++;
}
}
}
int change0 = num1Y + num2Y + min(num0 + num2, num0 + num1);
int change1 = num0Y + num2Y + min(num1 + num2, num0 + num1);
int change2 = num0Y + num1Y + min(num1 + num2, num0 + num2);
return min({change0, change1, change2});
}
};