<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:
        m = len(grid)
        n = len(grid[0])
        ones = set()
        visited = set()
        for i in range(m):
            for j in range(n):
                if grid[i][j] == 1:
                    ones.add((i,j))
        
        total = 0
        
        origin = after = 0
        
        # preprocessing two symmery axis
        if m % 2 == 1:
            cur = 0
            x = (m-1)//2
            for i in range(0, n//2):
                if grid[x][i] == 1:
                    visited.add((x,i))
                if grid[x][n-1-i] == 1:
                    visited.add((x, n-1-i))
                if grid[x][i] == 1 and grid[x][n-1-i] == 1:
                    origin += 1
                if (grid[x][i] ^ grid[x][n-1-i]) != 0:
                    after += 1
                    cur += 1
            total += cur
            
            
        if n % 2 == 1:
            y = (n-1)//2
            cur = 0
            for i in range(0, m//2):
                if grid[i][y] == 1:
                    visited.add((i,y))
                if grid[m-1-i][y] == 1:
                    visited.add((m-1-i,y))
                if grid[i][y] == 1 and grid[m-1-i][y] == 1:
                    origin += 1
                if (grid[i][y]^grid[m-1-i][y]) != 0:
                    after += 1
                    cur += 1
            total += cur
            
        if (origin+after)%2 != 0 and after == 0:
            total += 2
        
        
        if m%2==1 and n%2==1 and grid[m//2][n//2] == 1:
            total += 1
        
        for i,j in ones:
            if (i,j) in visited:
                continue
            # central node, delete it
            # if (2*i == m-1 and m%2!=0) or (2*j == n-1 and n%2!=0):
            #     print("continue")
            #     total += 1
            #     visited.add((i,j))
            else:
                cur = 1
                visited.add((i,j))
                if (m-1-i, j) in ones:
                    cur += 1
                    visited.add((m-1-i, j))
                if (i, n-1-j) in ones:
                    cur += 1
                    visited.add((i, n-1-j))
                if (m-1-i, n-1-j) in ones:
                    cur += 1
                    visited.add((m-1-i, n-1-j))
                if cur == 1 or cur == 3:
                    total += 1
                elif cur == 2:
                    total += 2
        
        return total
        
67800