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

        seen=set()

        ans=0

        m=len(grid)
        n=len(grid[0])

        # if m==1 or n==1:
        #     cnt=0
        #     for r in range(m):
        #         for c in range(n):
        #             if grid[r][c]==1:
        #                 cnt+=1
        #     return cnt

        pair_11=0
        pair_10=0
        cnt_1=0
        
        for r in range(m):
            for c in range(n):

                if (r,c) not in seen:

                    tmp=set()

                    r1,c1=r,c
                    r2,c2=m-1-r,c
                    r3,c3=r,n-1-c
                    r4,c4=m-1-r,n-1-c

                    seen.add((r1,c1))
                    seen.add((r2,c2))
                    seen.add((r3,c3))
                    seen.add((r4,c4))

                    tmp.add((r1,c1))
                    tmp.add((r2,c2))
                    tmp.add((r3,c3))
                    tmp.add((r4,c4))


                    tmp_dict=defaultdict(int)

                    for y,x in [(r1,c1),(r2,c2),(r3,c3),(r4,c4)]:

                        tmp_dict[grid[y][x]]+=1

                    min_val=min(tmp_dict.get(0,0),tmp_dict.get(1,0))

                    if len(tmp)==4:
                        ans+=min_val

                    elif len(tmp)==2:
                        if tmp_dict.get(1,0) and not tmp_dict.get(0,0):
                            pair_11+=1

                        elif tmp_dict.get(1,0) and  tmp_dict.get(0,0):
                            pair_10+=1
                    else :
                        if grid[r][c]==1:
                            cnt_1+=1


             
        if pair_11%2==1 :
            if pair_10 :
                ans+=pair_10

            else:
                ans+=2

        else:
            if pair_10:
                ans+=pair_10


        ans+=cnt_1           

        return ans                



        
        
        
        
80600