<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:
        covered = set()

        m = len(grid)
        n = len(grid[0])
        result = 0
        total_ones = 0
        two1 = 0
        two0 = 0
        twomix = 0
        for i in range(0, m):
            for j in range(0, n):
                if (i, j) in covered:
                    continue
                #print("procing {} {}".format(i, j))
                tr_i = i
                tr_j = (n-1) - j

                bl_i = (m-1) - i
                bl_j = j

                br_i = bl_i
                br_j = tr_j

                points = set([(i, j), (tr_i, tr_j), (bl_i, bl_j), (br_i, br_j)])
                one_count = 0
                zero_count = 0
                for point in points:
                    covered.add(point)
                    if grid[point[0]][point[1]] == 0:
                        zero_count += 1
                    else:
                        one_count += 1
                
                if len(points) == 1:
                    result += one_count
                    continue
                if len(points) == 2:
                    if one_count == 2:
                        two1 += 1
                    elif zero_count == 2:
                        two0 += 1
                    else:
                        twomix += 1
                    continue

                if one_count > zero_count:
                    result += zero_count
                    total_ones += one_count + zero_count
                else:
                    result += one_count
        two1 = two1 % 2
        if two1 == 0:
            result += twomix
        elif twomix > 0:
            result += twomix
        else:
            result += 2
        return result
81100