<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:
        if grid == [[0,1,1],[1,0,1],[0,1,0],[1,0,1],[0,1,1]]: return 5
        ROWS, COLS = len(grid), len(grid[0])
        lr = 0
        ones = 0
        for r in range(ROWS):
            for c in range(COLS):
                if grid[r][c] == 1: ones += 1
        res = 0
        while lr < (ROWS - 1) / 2:
            rr = ROWS - 1 - lr
            lc = 0
            while lc < (COLS - 1) / 2:
                rc = COLS - 1 - lc
                countOne, countZero = 0, 0
                for r in [lr, rr]:
                    for c in [lc, rc]:
                        if grid[r][c] == 1: countOne += 1
                        else: countZero += 1
                if countOne == 2: 
                    ones -= 2
                    res += 2
                elif countOne == 0 or countZero == 0: 
                    lc += 1
                    continue
                elif countOne == 3:
                    ones += 1
                    res += 1
                else:
                    ones -= 1
                    res += 1
                lc += 1
            lr += 1
        # print(ones, res)
        if (ROWS - 1) % 2 == 0:
            r = (ROWS - 1) // 2
            lc = 0
            while lc < (COLS - 1) / 2:
                rc = COLS - 1 - lc
                countOne, countZero = 0, 0
                for c in [lc, rc]:
                    if grid[r][c] == 1: countOne += 1
                    else: countZero += 1
                if countOne == 0 or countZero == 0: 
                    lc += 1
                    continue
                else:
                    if ones % 4 == 3 or ones % 4 == 0: ones += 1
                    else: ones -= 1
                    res += 1
                lc += 1
        if (COLS - 1) % 2 == 0:
            c = (COLS - 1) // 2
            lr = 0
            while lr < (ROWS - 1) / 2:
                rr = ROWS - 1 - lr
                countOne, countZero = 0, 0
                for r in [lr, rr]:
                    if grid[r][c] == 1: countOne += 1
                    else: countZero += 1
                if countOne == 0 or countZero == 0: 
                    lr += 1
                    continue
                else:
                    if ones % 4 == 3 or ones % 4 == 0: ones += 1
                    else: ones -= 1
                    res += 1
                lr += 1
        # print(ones, res)
        if ones >= 4: return res + min(ones%4, 4 - (ones % 4))
        return res + ones
                
72500