<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:
        # checking over every possible square, O((m*n)^2)
        # optimized 
        # keeping column and row totals, and then computing various squares using those totals
        # for each element in the mat
        #   add currelement to running sum
        # keeping another matrix of the same size, but computing the sqaures as we go, in a runningsum fashion
        # 1 1 3 2 4 
        # 1 1 3 2 4
        # 1 1 3 2 4
        #
        # 0 0 0  0  0 
        # 1 2 5  7  11
        # 2 4 10 14 22
        # 3 6 15 21 33
        #
        # [0]
        # 
        # [0] [0]
        # [0] [0]
        #
        newMat = [[0 for col in range(len(mat[0])+1)] for row in range(len(mat)+1)]
        for rowIter in range(len(mat)):
            for colIter in range(len(mat[rowIter])):
                newMat[rowIter+1][colIter+1] = mat[rowIter][colIter] + newMat[rowIter][colIter+1] + newMat[rowIter+1][colIter] - newMat[rowIter][colIter]

        # [[0, 0]
        # ,[0, 1]
        # ,[0, 2]
        #  [0, 2]]
        # 0 0 0  0  0 
        # 0 1 2 5  7  11
        # 0 2 4 10 14 22
        # 0 3 6 15 21 33

        print(newMat)
        largestSize = 0 
        for squareIter in range(1, min(len(newMat), len(newMat[0]))):
            for rowIter in range(squareIter, len(newMat)):
                for colIter in range(squareIter, len(newMat[rowIter])):
                    currSquare = newMat[rowIter][colIter] - newMat[rowIter - squareIter][colIter] - newMat[rowIter][colIter- squareIter] + newMat[rowIter- squareIter][colIter - squareIter]
                    if currSquare <= threshold:
                        largestSize = squareIter

        return largestSize
        



23900