<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 calculate_prefix_sum(self, matrix, rows, columns):
        prefix_sum=[[0]*columns for i in range(rows)]
        prefix_sum[0][0]=matrix[0][0]
        #initial vertical setup
        for i in range(1, rows):
            prefix_sum[i][0]=matrix[i][0]+prefix_sum[i-1][0]

        #initial diagonal setup
        for i in range(1, columns):
            prefix_sum[0][i]=matrix[0][i]+prefix_sum[0][i-1]
        
        for i in range(1,rows):
            for j in range(1, columns):
                prefix_sum[i][j]=(
                    prefix_sum[i-1][j]+
                    prefix_sum[i][j-1]+
                    matrix[i][j]-
                    prefix_sum[i-1][j-1]
                )

        return prefix_sum




    def check_square(self, matrix, prefix_sum, k, threshold):
        rows = len(matrix)
        columns = len(matrix[0])
        
        for i in range(k - 1, rows):
            for j in range(k - 1, columns):
                # Calculate the sum of the k x k submatrix using prefix sums
                total = prefix_sum[i][j]
                if i >= k:
                    total -= prefix_sum[i - k][j]
                if j >= k:
                    total -= prefix_sum[i][j - k]
                if i >= k and j >= k:
                    total += prefix_sum[i - k][j - k]
                
                if total <= threshold:
                    return True
        
        return False






    def maxSideLength(self, mat: List[List[int]], threshold: int) -> int:

       res=0
       rows=len(mat)
       columns = len(mat[0])
       
       #calculate prefix_sum
       prefix_sum=self.calculate_prefix_sum(mat, rows, columns)
       print(prefix_sum)

       #perform binary search over different square lengths
       l=0
       r=min(rows, columns)
       while l<=r:
            mid = (l+r)//2
            if self.check_square(mat, prefix_sum, mid, threshold):
                res=max(res, mid)
                l=mid+1
            else:
                r=mid-1

       return res
24200