<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:
#         max_size = 0 
#         rows = len(grid)
#         cols = len(grid[0])
#         row_sums = [row[:] for row in grid]
#         col_sums = [row[:] for row in grid]
#         for row in range(rows):
#             for col in range(1, cols):
#                 row_sums[row][col] += row_sums[row][col-1]
#         print(row_sums)
        
#         for col in range(cols):
#             for row in range(1, rows):
#                 col_sums[row][col] += col_sums[row-1][col]
#         print(col_sums)
#         for k in range(min(rows,cols)+1, 1, -1):
#             for row in range(rows-k+1):
#                 pre_col, count = 0, 0
#                 for col in range(cols):
#                     col_sum = col_sums[row+k-1][col] - (col_sums[row-1][col] if row else 0)
#                     if col_sum != pre_col:
#                         pre_col = col_sum
#                         col_sum = 1 
#                     else:
#                         col_sum += 1
#                     if col_sum > k:
#                         col_sum -= 1
#                     if col_sum ==k:
#                         for row in range(row, row+k):
#                             if row_sums[row][col] - (row_sums[row][col-k] if col >= k else 0) !=pre_col:
#                                 break 
#                         if  sum(grid[row+dia][col-k+1+dia] for dia in range(k)) == pre_col:
#                             if sum(grid[row+dia][col-dia] for dia in range(k)) == pre_col:
#                                 return k
        # return 1 
                            
class Solution:
    def largestMagicSquare(self, grid: List[List[int]]) -> int:
        rows = len(grid)
        cols = len(grid[0])
        
        def sum_row(row, col1, col2):
            return sum(grid[row][col1:col2+1])
        
        def sum_col(col, row1, row2):
            return sum(grid[i][col] for i in range(row1, row2+1))
        
        def sum_diag(row, col, k):
            return sum(grid[row+x][col+x] for x in range(k))
        
        def sum_anti_diag(row, col, k):
            return sum(grid[row+x][col-x] for x in range(k))
        
        max_size = 1
        
        for k in range(2, min(rows, cols) + 1):
            for i in range(rows - k + 1):
                for j in range(cols - k + 1):
                    row_sum = sum_row(i, j, j + k - 1)
                    if all(sum_row(i + x, j, j + k - 1) == row_sum for x in range(k)) and \
                       all(sum_col(j + x, i, i + k - 1) == row_sum for x in range(k)) and \
                       sum_diag(i, j, k) == row_sum and \
                       sum_anti_diag(i, j + k - 1, k) == row_sum:
                        max_size = k
        return max_size        
2989