<p>A <code>k x k</code> <strong>magic square</strong> is a <code>k x k</code> grid filled with integers such that every row sum, every column sum, and both diagonal sums are <strong>all equal</strong>. The integers in the magic square <strong>do not have to be distinct</strong>. Every <code>1 x 1</code> grid is trivially a <strong>magic square</strong>.</p>

<p>Given an <code>m x n</code> integer <code>grid</code>, return <em>the <strong>size</strong> (i.e., the side length </em><code>k</code><em>) of the <strong>largest magic square</strong> that can be found within this grid</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/05/29/magicsquare-grid.jpg" style="width: 413px; height: 335px;" />
<pre>
<strong>Input:</strong> grid = [[7,1,4,5,6],[2,5,1,6,4],[1,5,4,3,2],[1,2,7,3,4]]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The largest magic square has a size of 3.
Every row sum, column sum, and diagonal sum of this magic square is equal to 12.
- Row sums: 5+1+6 = 5+4+3 = 2+7+3 = 12
- Column sums: 5+5+2 = 1+4+7 = 6+3+3 = 12
- Diagonal sums: 5+4+3 = 6+4+2 = 12
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/05/29/magicsquare2-grid.jpg" style="width: 333px; height: 255px;" />
<pre>
<strong>Input:</strong> grid = [[5,1,3,1],[9,3,3,1],[1,3,3,8]]
<strong>Output:</strong> 2
</pre>

<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;= 50</code></li>
	<li><code>1 &lt;= grid[i][j] &lt;= 10<sup>6</sup></code></li>
</ul>

class Solution:
    def largestMagicSquare(self, grid: List[List[int]]) -> int:
        ans=1
        for i in range(len(grid)):
            for j in range(len(grid[0])):
                nxt=i+1
                nxt1=j+1
                while nxt<len(grid) and nxt1<len(grid[0]):
                            d1=0
                            rstart=i
                            cstart=j
                            while rstart<nxt+1 and cstart<nxt1+1:
                                d1+=grid[rstart][cstart]
                                rstart+=1
                                cstart+=1
                            rstart=i
                            cstart=nxt1
                            d2=0
                            while rstart<nxt+1 and cstart>j-1:
                                d2+=grid[rstart][cstart]
                                rstart+=1
                                cstart-=1
                            if d1==d2:
                                cstart=0
                                pv1=0
                                sr=0
                                inv=0
                                clsm=0
                                rwsm=0
                                
                                
                                for n1 in range(j,nxt1+1):
                                    sr=0
                                    
                                    for k in range(i,nxt+1):
                                        sr+=grid[k][n1]
                                    if clsm==0:
                                        clsm=sr
                                        
                                        continue
                                    elif clsm!=sr:
                                        inv=1
                                        break
                                    else:
                                        continue
                                if inv:
                                    nxt+=1
                                    nxt1+=1
                                    continue
                                for n2 in range(i,nxt+1):
                                    sv=0
                                    for n3 in range(j,nxt1+1):
                                        sv+=grid[n2][n3]
                                    if rwsm==0:
                                        rwsm=sv
                                    elif rwsm==sv:
                                        sv=0
                                        continue
                                    else:
                                        inv=1
                                        break
                                if inv==0 and rwsm==clsm==d1:
                                    if (nxt-i==49):
                                        print()
                                    ans=max(ans,(nxt-i)+1)
                                nxt+=1
                                nxt1+=1
                                continue
                                    
                            else:
                                nxt+=1
                                nxt1+=1
                                continue
        return ans                        
                                
                            
                    
                
        
2793