<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:

        # Good Problem ( Two Pointer | Matrix )
        
        m, n = len(grid), len(grid[0])
        res = 0                 # minimum changes required in 4 way symmetry
        single = 0              # number of (1, 0) pair in a 2 way symmetry
        double = 0              # number of (1, 1) pair in a 2 way symmetry

        # Process 4-way symmetry
        for i in range(m // 2):
            for j in range(n // 2):
                # Change to all 1s or all 0s
                ones = grid[i][j] + grid[i][n-1-j] + grid[m-1-i][j] + grid[m-1-i][n-1-j]
                res += min(ones, 4 - ones)

            # Process the middle column if it exists (2-way symmetry)
            if n % 2 == 1:
                ones = grid[i][n//2] + grid[m-1-i][n//2]
                single += (ones == 1)   # (1, 0)
                double += (ones == 2)   # (1, 1)
        
        # Process the middle row if it exists (2-way symmetry)
        if m % 2 == 1:
            for j in range(n // 2):
                ones = grid[m//2][j] + grid[m//2][n-1-j] 
                single += (ones == 1)   # (1, 0)
                double += (ones == 2)   # (1, 1)
            if n % 2 == 1:
                res += grid[m//2][n//2]     # Center cell needs to be 0 if it's 1

        # Adjust based on the number of (1, 1) pairs:
        # 1. If even, the total number of 1s is already a multiple of 4.
        #    We can change every (1, 0) pair to (0, 0) if there's any.
        # 2. If odd, but we have (1, 0) pairs, convert one (1, 0) to (1, 1).      
        if double % 2 == 0 or single > 0:     
            return res + single

        # 3. If the number of (1, 1) pairs is odd and there are no (1, 0) pairs,
        #    flip one (1, 1) pair to (0, 0).
        return res + 2
69200