<p>You are given an <code>m x n</code> binary matrix <code>grid</code>.</p>

<p>A row or column is considered <strong>palindromic</strong> if its values read the same forward and backward.</p>

<p>You can <strong>flip</strong> any number of cells in <code>grid</code> from <code>0</code> to <code>1</code>, or from <code>1</code> to <code>0</code>.</p>

<p>Return the <strong>minimum</strong> number of cells that need to be flipped to make <strong>all</strong> rows and columns <strong>palindromic</strong>, and the total number of <code>1</code>&#39;s in <code>grid</code> <strong>divisible</strong> by <code>4</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">grid = [[1,0,0],[0,1,0],[0,0,1]]</span></p>

<p><strong>Output:</strong> <span class="example-io">3</span></p>

<p><strong>Explanation:</strong></p>

<p><img src="https://assets.leetcode.com/uploads/2024/08/01/image.png" style="width: 400px; height: 105px;" /></p>
</div>

<p><strong class="example">Example 2:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">grid = [[0,1],[0,1],[0,0]]</span></p>

<p><strong>Output:</strong> <span class="example-io">2</span></p>

<p><strong>Explanation:</strong></p>

<p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/08/screenshot-from-2024-07-09-01-37-48.png" style="width: 300px; height: 104px;" /></p>
</div>

<p><strong class="example">Example 3:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">grid = [[1],[1]]</span></p>

<p><strong>Output:</strong> <span class="example-io">2</span></p>

<p><strong>Explanation:</strong></p>

<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/01/screenshot-from-2024-08-01-23-05-26.png" style="width: 200px; height: 70px;" /></p>
</div>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>m == grid.length</code></li>
	<li><code>n == grid[i].length</code></li>
	<li><code>1 &lt;= m * n &lt;= 2 * 10<sup>5</sup></code></li>
	<li><code>0 &lt;= grid[i][j] &lt;= 1</code></li>
</ul>

class Solution:
    def minFlips(self, grid: List[List[int]]) -> int:
        R = len(grid)
        C = len(grid[0])
        for r in range(R):
            print(grid[r])
        # print("R",R, "C",C)
        res = 0
        add = 1 if R%2 == 1 and C%2==1 else 0
        print('add')
        for r in range(R//2):
            for c in range(C//2):
                countOnes = grid[r][c] + grid[r][-c-1]+grid[-r-1][c]+grid[-r-1][-c-1]
                print('countOnes',countOnes)
                if countOnes>=2:
                    # all 1
                    print('add1')
                    res += 4-countOnes
                else:
                    # all 0
                    print('add2')
                    res += countOnes
        
        countOneZero = 0
        countOneOne = 0
        # mid row
        if R % 2 == 1:
            print("MID ROW")
            # countOneOne should be even
            r = R//2
            for c in range(C//2):
                print("ADSDASDASD",r,c, r,-c-1)
                if grid[r][c] == 1 and grid[r][-c-1] == 1:
                    countOneOne+=1
                elif (grid[r][c] == 1 and grid[r][-c-1] == 0) or (grid[r][c] == 0 and grid[r][-c-1]) == 1:
                    print("DASDHKL@()$*()!@$")
                    countOneZero += 1
        # mid col
        if C % 2 == 1:
            # countOneOne should be even
            # countOneZero = 0
            # countOneOne = 0
            c = C//2
            print('c',c)
            for r in range(R//2):
                print('r',r)
                # return 0
                if grid[r][c] == 1 and grid[-r-1][c] == 1:
                    countOneOne+=1
                elif (grid[r][c] == 1 and grid[-r-1][c] == 0) or (grid[r][c] == 0 and grid[-r-1][c] == 1):
                    countOneZero += 1

        print('countOneOne',countOneOne)
        print('countOneZero',countOneZero)
        if countOneOne % 2 == 0:
            # print('add7')
            res += countOneZero
        else:
            if countOneZero > 0:
                # one 1,0 -> 1,1
                # print('add8')
                res += 1 
                # rest 1,0-> 0,0
                # print('add9')
                res += countOneZero -1
            else:
                # one 1,1 -> 0,0
                # print('12341234 add10')
                res += 2
        if R%2 == 1 and C % 2 == 1:
            res += grid[R//2][C//2]
        return res
                

69500