<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 is_magic_matrix(self, matrix, n):
        row_idx = 0
        diag_sum = 0
        sum_matrix = sum(matrix[0])

        while row_idx<n:
            if sum(matrix[row_idx])!=sum_matrix:
                return False

            col_sum = 0
            col_idx = 0

            while col_idx<n:
                col_sum += matrix[col_idx][row_idx]
                col_idx += 1
            if col_sum != sum_matrix:
                return False

            diag_sum += matrix[row_idx][row_idx]
            row_idx += 1

        if diag_sum != sum_matrix:
            return False

        diag_sum = 0
        r_idx = 0
        c_idx = n-1
        itr = 0
        while itr < n:
            diag_sum += matrix[r_idx+itr][c_idx-itr]
            itr += 1
        
        if diag_sum != sum_matrix:
            return False

        return True

    def largestMagicSquare(self, grid: List[List[int]]) -> int:
        grid_cols = len(grid[0])
        grid_rows = len(grid)
        if grid_cols < 2 or grid_rows < 2:
            return 1
        n_range = min(grid_rows, grid_cols)
        n_itr = n_range
        while n_itr > 1:
            itr_cols = grid_cols-n_itr
            itr_rows = grid_rows-n_itr
            for i_row in range(itr_rows+1):
                for i_col in range(itr_cols+1):
                    tmp_matrix = []
                    for r in range(i_row, i_row+n_itr):
                        col_matrix = []
                        for c in range(i_col, i_col+n_itr):
                            col_matrix.append(grid[r][c])
                        tmp_matrix.append(col_matrix)
                    if self.is_magic_matrix(tmp_matrix, n_itr):
                        return n_itr
            n_itr -= 1
        return 1
1814