<p>Given an <code>m x n</code> binary <code>matrix</code> filled with <code>0</code>&#39;s and <code>1</code>&#39;s, <em>find the largest square containing only</em> <code>1</code>&#39;s <em>and return its area</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max1grid.jpg" style="width: 400px; height: 319px;" />
<pre>
<strong>Input:</strong> matrix = [[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;]]
<strong>Output:</strong> 4
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max2grid.jpg" style="width: 165px; height: 165px;" />
<pre>
<strong>Input:</strong> matrix = [[&quot;0&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;]]
<strong>Output:</strong> 1
</pre>

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

<pre>
<strong>Input:</strong> matrix = [[&quot;0&quot;]]
<strong>Output:</strong> 0
</pre>

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

<ul>
	<li><code>m == matrix.length</code></li>
	<li><code>n == matrix[i].length</code></li>
	<li><code>1 &lt;= m, n &lt;= 300</code></li>
	<li><code>matrix[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li>
</ul>

'''

Brute force solution:
- Scan each position
- Keep expanding the squares outwards...

O(n^4) assuming a square input matrix.

DP:
- largest[i,j] =
    if mat[i][j] = 0
        0
    else:
        res = 1
        expansion_width = min(largest[i+1, j+1], right[i+1, j], down[i, j+1])
        if expansion_width > 0:
            res = (expansion_width + 1) ** 2

'''

def right(mat: list[list[str]], i: int, j: int, cache: dict[tuple[str, int, int], int]) -> int:
    if not 0 <= i < len(mat):
        return 0
    if not 0 <= j < len(mat[0]):
        return 0
    
    if mat[i][j] == '0':
        return 0

    key = (1, i, j)
    if key in cache:
        return cache[key]
    
    res = 1 + right(mat, i, j+1, cache)

    cache[key] = res
    return res

def down(mat: list[list[str]], i: int, j: int, cache: dict[tuple[str, int, int], int]) -> int:
    if not 0 <= i < len(mat):
        return 0
    if not 0 <= j < len(mat[0]):
        return 0
    
    if mat[i][j] == '0':
        return 0
    
    key = (2, i, j)
    if key in cache:
        return cache[key]
    
    res = 1 + down(mat, i+1, j, cache)

    cache[key] = res
    return res

def largest(mat: list[list[str]], i: int, j: int, cache: dict[tuple[str, int, int], int]) -> int:
    if not 0 <= i < len(mat):
        return 0
    if not 0 <= j < len(mat[0]):
        return 0

    if mat[i][j] == '0':
        return 0
    
    key = (0, i, j)
    if key in cache:
        return cache[key]

    res = 1
    res += min(
        largest(mat, i+1, j+1, cache),
        min(
            right(mat, i, j+1, cache),
            down(mat, i+1, j, cache)
        )
    )

    cache[key] = res
    return res

class Solution:
    def maximalSquare(self, matrix: List[List[str]]) -> int:
        best = 0
        cache = {}
        for i in range(len(matrix)):
            for j in range(len(matrix[i])):
                best = max(best, largest(matrix, i, j, cache) ** 2)
        return best

'''
["1","1","1","1","1","1","1","1"]
["1","1","1","1","1","1","1","0"]
["1","1","1","1","1","1","1","0"]
["1","1","1","1","1","0","0","0"]
["0","1","1","1","1","0","0","0"]
'''
49825