<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

class Solution:
    def minFlips(self, grid: List[List[int]]) -> int:
        # Dimensions of the grid
        row_count = len(grid)
        col_count = len(grid[0])
        total_flips = 0

        # Handle the case where both dimensions are odd
        if row_count % 2 == 1 and col_count % 2 == 1:
            if grid[row_count // 2][col_count // 2] == 1:
                total_flips += 1

        # Array to count the required flips
        flip_counts = [0, 0, 0]  # flip_counts[0] for zero flips, [1] for one flip, [2] for two flips

        # Handle rows if the number of rows is odd
        if row_count % 2 == 1:
            left = 0
            right = col_count - 1
            while left < right:
                total_sum = grid[row_count // 2][left] + grid[row_count // 2][right]
                flip_counts[total_sum] += 1
                left += 1
                right -= 1

        # Handle columns if the number of columns is odd
        if col_count % 2 == 1:
            top = 0
            bottom = row_count - 1
            while top < bottom:
                total_sum = grid[top][col_count // 2] + grid[bottom][col_count // 2]
                flip_counts[total_sum] += 1
                top += 1
                bottom -= 1

        # Adjust total flips based on the counts
        if flip_counts[2] % 2 == 1:
            if flip_counts[1] > 0:
                total_flips += flip_counts[1]
            else:
                total_flips += 2
        else:
            total_flips += flip_counts[1]

        # Handle the grid's 4-way symmetry
        row = 0
        while row < row_count // 2:
            col = 0
            while col < col_count // 2:
                top_left = grid[row][col]
                bottom_left = grid[row_count - 1 - row][col]
                top_right = grid[row][col_count - 1 - col]
                bottom_right = grid[row_count - 1 - row][col_count - 1 - col]

                total_sum = top_left + bottom_left + top_right + bottom_right

                if total_sum != 0 and total_sum != 4:
                    total_flips += min(total_sum, 4 - total_sum)

                col += 1
            row += 1

        return total_flips

72000