<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:
        if len(grid) == 0:
            return 0

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

        ps_row = [[0] * (n + 1) for _ in range(m)] # prefix_sum for each rows
        ps_col = [[0] * n for _ in range(m + 1)] # columns 
        ps_diag = [[0] * (n + 1)  for _ in range(m + 1)] # diag
        ps_ant_diag = [[0] * (n + 1) for _ in range(m + 1)] # anti-diag

        for i in range(m):
            for j in range(n):
                # column
                ps_row[i][j+1] = ps_row[i][j] + grid[i][j]
                # row 
                ps_col[i+1][j] = ps_col[i][j] + grid[i][j]
                # diag (i, j) -> (i+1, j+1)
                ps_diag[i+1][j+1] = ps_diag[i][j] + grid[i][j]
                # anti_dig remember (i, j + 1) -> (i + 1, j)
                ps_ant_diag[i+1][j] = ps_ant_diag[i][j+1] + grid[i][j]
        
        ans = 1
        # 然后再每个位置暴力搜索就可以了
        for i in range(m):
            for j in range(n):
                # iterate each length, 1...min(i, j) + 1 for each pair (i, j)
                # 最大边长就是x,y的最小值
                for k in range(1, min(i, j)+1):
                    if self.isMagicSquare(i, j, k, ps_row, ps_col, ps_diag, ps_ant_diag):
                        ans = max(ans, k+1) 
 
        return ans

    def isMagicSquare(self, i, j, side_length, ps_row, ps_col, ps_diag, ps_ant_diag):
        """
        check the square with i, j as bottom right location
        """
        # ii, jj top-left location, i, j is bottom right location
        ii, jj = i - side_length, j - side_length
        target = ps_diag[i+1][j+1] - ps_diag[ii][jj]
        # 这里画图就知道了（加一行， 加一列）
        match = (ps_ant_diag[i+1][jj] - ps_ant_diag[ii][j+1]) == target
        for r in range(ii, i+1):
            match &= (ps_row[r][j+1] - ps_row[r][jj]) == target
            
        for c in range(jj, j+1):
            match &= (ps_col[i+1][c] - ps_col[ii][c]) == target

        return match
1031