<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:
        # IDEA: prefix sum + binary search on output!
        n, m = len(mat), len(mat[0])

        # setup our 2D prefix sum :)
        pref = [ [ 0 for _ in range(m) ] for _ in range(n) ]
        for i in range(n):
            for j in range(m):
                if i == 0 and j == 0:
                    pref[0][0] = mat[0][0]
                elif i == 0:
                    pref[0][j] = mat[0][j] + pref[0][j-1]
                elif j == 0:
                    pref[i][0] = mat[i][0] + pref[i-1][0]
                else:
                    pref[i][j] = mat[i][j] + pref[i-1][j] + pref[i][j-1] - pref[i-1][j-1]
        
        # subroutine that does the square-sum :)
        def sum_square(row1, col1, row2, col2) -> int:
            if row2 < 0 or col2 < 0:
                return 0
            else:
                return pref[row2][col2] - sum_square(0,0,row1-1,col2) - sum_square(0,0,row2,col1-1) + sum_square(0,0,row1-1,col1-1)

        # subroutine that tries every square-sum for a given side length :)
        def try_side_length(length) -> bool:
            # print(f"side length = {length}")
            for i in range(n-length+1):
                for j in range(m-length+1):
                    # print(f"trying {i},{j}")
                    if sum_square(i,j,i+length-1,j+length-1) <= threshold:
                        return True
            return False

        # do BS over side length
        low = 1 # inclusive :)
        high = min(n, m) # exclusive :)
        largest = 0 # largest side length to work

        while low <= high:
            mid = (low+high) // 2
            # print(f"trying mid={mid}")

            if try_side_length(mid):
                low = mid+1
            else:
                high = mid - 1

        # if low == high and low != 0 and low <= min(n,m):
        #     # print(f"trying mid={low}")
        #     if try_side_length(low):
        #         largest = max(low, largest)

        return high
22200