<p>Given a <code>m * n</code> matrix of ones and zeros, return how many <strong>square</strong> submatrices have all ones.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> matrix =
[
&nbsp; [0,1,1,1],
&nbsp; [1,1,1,1],
&nbsp; [0,1,1,1]
]
<strong>Output:</strong> 15
<strong>Explanation:</strong> 
There are <strong>10</strong> squares of side 1.
There are <strong>4</strong> squares of side 2.
There is  <strong>1</strong> square of side 3.
Total number of squares = 10 + 4 + 1 = <strong>15</strong>.
</pre>

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

<pre>
<strong>Input:</strong> matrix = 
[
  [1,0,1],
  [1,1,0],
  [1,1,0]
]
<strong>Output:</strong> 7
<strong>Explanation:</strong> 
There are <b>6</b> squares of side 1.  
There is <strong>1</strong> square of side 2. 
Total number of squares = 6 + 1 = <b>7</b>.
</pre>

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

<ul>
	<li><code>1 &lt;= arr.length&nbsp;&lt;= 300</code></li>
	<li><code>1 &lt;= arr[0].length&nbsp;&lt;= 300</code></li>
	<li><code>0 &lt;= arr[i][j] &lt;= 1</code></li>
</ul>

class Solution:
    def countSquares(self, matrix: List[List[int]]) -> int:
        rows: int = len(matrix)
        cols: int = len(matrix[0])
        curr_row: int
        curr_col: int
        largest_square_cache: Dict[Tuple[int, int], int] = dict()
        total_squares: int = 0

        for curr_row in range(rows):
            for curr_col in range(cols):
                count_of_square_submatrix_with_curr_row_col_as_top_left = Solution.get_largest_square(
                    matrix=matrix,
                    rows=rows,
                    cols=cols,
                    curr_row=curr_row,
                    curr_col=curr_col,
                    largest_square_cache=largest_square_cache
                )
                total_squares += count_of_square_submatrix_with_curr_row_col_as_top_left
        return total_squares

    @staticmethod
    def get_largest_square(matrix: List[List[int]], rows: int, cols: int, curr_row: int, curr_col: int, largest_square_cache: Dict[Tuple[int, int], int]) -> int:
        if curr_row == rows or curr_col == cols or matrix[curr_row][curr_col] == 0:
            largest_square_cache[(curr_row, curr_col)] = 0
            return 0

        if (curr_row, curr_col) in largest_square_cache:
            return largest_square_cache.get((curr_row, curr_col))

        largest_square_cache[(curr_row, curr_col)] = 1 + min(
            Solution.get_largest_square(
                matrix=matrix,
                rows=rows,
                cols=cols,
                curr_row=curr_row+1,
                curr_col=curr_col,
                largest_square_cache=largest_square_cache
            ),
            Solution.get_largest_square(
                matrix=matrix,
                rows=rows,
                cols=cols,
                curr_row=curr_row,
                curr_col=curr_col+1,
                largest_square_cache=largest_square_cache
            ),
            Solution.get_largest_square(
                matrix=matrix,
                rows=rows,
                cols=cols,
                curr_row=curr_row+1,
                curr_col=curr_col+1,
                largest_square_cache=largest_square_cache
            )
        )
        return largest_square_cache.get((curr_row, curr_col))
    
27900