<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:
        m = len(grid)
        n = len(grid[0])

        dp = [[0] * (n + 1)]
        for line in grid:
            dp_line = [0]
            for j, elem in enumerate(line, 1):
                current = (
                    elem
                    + dp_line[-1]
                    + dp[-1][j]
                    - dp[-1][j - 1]
                )
                dp_line.append(current)
            dp.append(dp_line)

        def check_rows(x, y, u, v, value):
            for i in range(x, u + 1):
                if value != (
                    dp[i + 1][v + 1]
                    - dp[i + 1][y]
                    - dp[i][v + 1]
                    + dp[i][y]
                ):
                    return False
            return True
    
        def check_cols(x, y, u, v, value):
            for j in range(y, v + 1):
                if value != (
                    dp[u + 1][j + 1]
                    - dp[x][j + 1]
                    - dp[u + 1][j]
                    + dp[x][j]
                ):
                    return False
            return True
        
        def check_diag(x, y, u, v, value):
            return value == sum(
                grid[x + step][y + step]
                for step in range(u - x + 1)
            )
        
        def check_other_diag(x, y, u, v, value):
            return value == sum(
                grid[u - step][y + step]
                for step in range(u - x + 1)
            )
        
        def check(u, v, prev_result):
            max_size = min(u, v) + 1
            if max_size < prev_result:
                return 1
            x = u - max_size
            y = v - max_size

            while x + 1 < u:
                x += 1
                y += 1

                sum_ = (
                    dp[u + 1][v + 1]
                    - dp[x][v + 1]
                    - dp[u + 1][y]
                    + dp[x][y]
                )

                size = u - x + 1
                if sum_ % size != 0:
                    continue
                value = sum_ // size

                if (
                    check_rows(x, y, u, v, value)
                    and check_cols(x, y, u, v, value)
                    and check_diag(x, y, u, v, value)
                    and check_other_diag(x, y, u, v, value)
                ):
                    return size
            
            return 1

        result = 1
        for i in range(m):
            for j in range(n):
                result = max(result, check(i, j, result))
        return result


247