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

from typing import List
from collections import deque

class Solution:
    def minFlips(self, grid: List[List[int]]) -> int:
        m, n = len(grid), len(grid[0])
        visited = set()
        pairs = []

        def get_cycle(i, j):
            ones, zeros = 0, 0
            # Start from the cell (i, j)
            if grid[i][j] == 1:
                ones += 1
            else:
                zeros += 1
            
            # Symmetric cells
            q = deque([(i, j)])
            visited.add((i, j))
            
            while q:
                cx, cy = q.popleft()
                ox, oy = m - 1 - cx, n - 1 - cy
                
                if (cx, oy) not in visited:
                    visited.add((cx, oy))
                    q.append((cx, oy))
                    if grid[cx][oy] == 1:
                        ones += 1
                    else:
                        zeros += 1
                
                if (ox, cy) not in visited:
                    visited.add((ox, cy))
                    q.append((ox, cy))
                    if grid[ox][cy] == 1:
                        ones += 1
                    else:
                        zeros += 1
            
            return ones, zeros
        
        def calculate_flips():
            # Traverse the top-left corner of each cycle
            for i in range((m + 1) // 2):
                for j in range((n + 1) // 2):
                    if (i, j) not in visited:
                        ones, zeros = get_cycle(i, j)
                        pairs.append((ones, zeros))

        calculate_flips()
        
        total_flips = 0
        zero_flips_needed = 0
        one_flips_needed = 0
        even_pairs = 0
        
        for ones, zeros in pairs:
            if (ones + zeros) == 4:
                # Case where there are 4 cells in the cycle
                total_flips += min(ones, zeros)
            elif (ones + zeros) == 2:
                # Case where there are 2 cells in the cycle
                if ones == zeros:
                    even_pairs += 1
                elif ones > zeros:
                    one_flips_needed += 1
                else:
                    zero_flips_needed += 1
            else:
                # Case where there is only one cell in the cycle
                if ones == 1:
                    total_flips += 1
        
        # Adjust the flips to ensure the total number of ones is divisible by 4
        if one_flips_needed % 2 == 0:
            return total_flips + even_pairs
        
        # When one_flips_needed is odd, we need to make adjustments
        remaining_needed = one_flips_needed % 2
        if even_pairs >= remaining_needed:
            return total_flips + even_pairs
        remaining_needed -= even_pairs
        return total_flips + (remaining_needed * 2)

79400