<p>Given a <code>m x n</code> matrix <code>mat</code> and an integer <code>threshold</code>, return <em>the maximum side-length of a square with a sum less than or equal to </em><code>threshold</code><em> or return </em><code>0</code><em> if there is no such square</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2019/12/05/e1.png" style="width: 335px; height: 186px;" />
<pre>
<strong>Input:</strong> mat = [[1,1,3,2,4,3,2],[1,1,3,2,4,3,2],[1,1,3,2,4,3,2]], threshold = 4
<strong>Output:</strong> 2
<strong>Explanation:</strong> The maximum side length of square with sum less than 4 is 2 as shown.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> mat = [[2,2,2,2,2],[2,2,2,2,2],[2,2,2,2,2],[2,2,2,2,2],[2,2,2,2,2]], threshold = 1
<strong>Output:</strong> 0
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>m == mat.length</code></li>
	<li><code>n == mat[i].length</code></li>
	<li><code>1 &lt;= m, n &lt;= 300</code></li>
	<li><code>0 &lt;= mat[i][j] &lt;= 10<sup>4</sup></code></li>
	<li><code>0 &lt;= threshold &lt;= 10<sup>5</sup></code></li>
</ul>

class Solution:

    def caluclatePrefixSum(self,mat):
        m = len(mat)
        n = len(mat[0])

        prefix = [[0 for _ in range(n)] for _ in range(m)]

        prefix[0][0] = mat[0][0]

        for i in range(1,m):
            prefix[i][0] = prefix[i-1][0] + mat[i][0]

        for j in range(1,n):
            prefix[0][j] = prefix[0][j-1] + mat[0][j]

        for i in range(1,m):
            for j in range(1,n):
                prefix[i][j] = mat[i][j] + prefix[i-1][j] + prefix[i][j-1] - prefix[i-1][j-1]
        
        return prefix

    def check(self,mat,k,prefix,thres):
        m = len(mat)
        n = len(mat[0])
        for i in range(m-k+1):
            total = 0
            for j in range(n-k+1):
                # i,j is starting point
                leftX = i
                leftY = j
                rightX = i + k - 1
                rightY = j + k - 1

                total = prefix[rightX][rightY]

                if leftY > 0:
                    total -= prefix[rightX][leftY-1]
                
                if leftX > 0:
                    total -= prefix[leftX-1][rightY]
                
                if leftX > 0 and leftY > 0:
                    total += prefix[leftX-1][leftY-1]

                if total <= thres:
                    return True
        
        return False

    def maxSideLength(self, mat: List[List[int]], threshold: int) -> int:
        m = len(mat)
        n = len(mat[0])
        low = 0

        ans = 0
        high = min(m,n)
        prefix = self.caluclatePrefixSum(mat)

        while low <= high:
            mid = int((low+high)//2)

            if self.check(mat,mid,prefix,threshold):
                low = mid+1
                ans = mid
            else:
                high = mid-1

        return ans
22300