<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 maxSideLength(self, mat: List[List[int]], threshold: int) -> int:
#         preSums = [[0] * len(mat[0]) for _ in range(len(mat))]
#         for i in range(len(mat)):
#             for j in range(len(mat[0])):
#                 preSums[i][j] = mat[i][j]
#                 if 0 <= i-1 < len(mat):
#                     preSums[i][j] += preSums[i-1][j]
#                 if 0 <= j-1 < len(mat[0]):
#                     preSums[i][j] += preSums[i][j-1]
#                 if 0 <= i-1 < len(mat) and 0 <= j-1 < len(mat[0]):
#                     preSums[i][j] -= preSums[i-1][j-1]
        
#         def helper(mid):
#             for i in range(mid, len(mat)):
#                 for j in range(mid, len(mat[0])):
#                     print(i,j, preSums[i][j] - preSums[i][j-mid] - preSums[i-mid][j] + preSums[i-mid][j-mid])
#                     if (preSums[i][j] - preSums[i][j-mid] - preSums[i-mid][j] + preSums[i-mid][j-mid]) <= threshold:
#                         for row in preSums:
#                             print(row)
#                         return True
#             return False

#         # Binary search on answer
#         ans = None
#         low = 0
#         high = min(len(mat), len(mat[0]))
#         while low <= high:
#             mid = (high + low) // 2
#             if helper(mid):
#                 ans = mid
#                 low = mid+1
#             else:
#                 high = mid-1
#         return ans if ans else 0

class Solution:
    def maxSideLength(self, mat: List[List[int]], threshold: int) -> int:
        rows, cols = len(mat), len(mat[0])
        preSums = [[0] * (cols + 1) for _ in range(rows + 1)]
        
        # Calculate the prefix sums
        for i in range(1, rows + 1):
            for j in range(1, cols + 1):
                preSums[i][j] = mat[i-1][j-1] + preSums[i-1][j] + preSums[i][j-1] - preSums[i-1][j-1]
        
        def helper(mid):
            for i in range(mid, rows + 1):
                for j in range(mid, cols + 1):
                    total = preSums[i][j] - preSums[i-mid][j] - preSums[i][j-mid] + preSums[i-mid][j-mid]
                    print(i, j, total)
                    if total <= threshold:
                        return True
            return False

        # Binary search on answer
        ans = 0
        low, high = 0, min(rows, cols)
        while low <= high:
            mid = (low + high) // 2
            if helper(mid):
                ans = mid
                low = mid + 1
            else:
                high = mid - 1
        
        return ans

22800