> For the complete documentation index, see [llms.txt](https://maksimdan.gitbook.io/interview-practice-problems/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://maksimdan.gitbook.io/interview-practice-problems/leetcode_sessions/221-maximal-square.md).

# 221 Maximal Square

Given a 2D binary matrix filled with 0's and 1's, find the largest square containing only 1's and return its area.

For example, given the following matrix:

```
1 0 1 0 0
1 0 1 1 1
1 1 1 1 1
1 0 0 1 0
```

Return 4.

**The Idea:** Harness the power of DP. Store the matrix as a submatrix of an n+1 larger matrix. Then, for each element in the submatrix, find the `min(left, right, diag) + 1`. The above example will in effect become:

```
 0 0 0 0 0 0
 0 1 0 1 0 0
 0 1 0 1 1 1
 0 1 1 1 2 2 
 0 1 0 0 1 0
```

The intuition is that we want to find a way that tells us what size our square matrix is by looking it's surrounding neighbors. A 4 by 4 square for example, can become decomposed as follows:

```
0 0 0 0 0
0 1 1 1 1 
0 1 2 2 2
0 1 2 3 3
0 1 2 3 4
```

Notice how every number is true for every subsquare in the matrix. Once we know what matrix we want to generate, we can reverse engineer a formula (`min(left, right, diag) + 1`) that makes this true.

**Complexity:** O(2n) time (extra time converting char matrix to int matrix) and O(1) space

```python
import numpy as np

class Solution:
    def maximalSquare(self, matrix):
        """
        :type matrix: List[List[str]]
        :rtype: int
        """

        if not matrix:
            return 0

        Matrix = np.zeros((len(matrix) + 1, len(matrix[0]) + 1))
        Matrix[1:Matrix.shape[0], 1:Matrix.shape[1]] = np.array(matrix, dtype=int)
        the_max = 0

        for i in range(1, Matrix.shape[0]):
            for j in range(1, Matrix.shape[1]):
                if Matrix[i][j] == 1:
                    Matrix[i][j] = min(Matrix[i - 1, j], Matrix[i - 1, j - 1], Matrix[i, j - 1]) + 1
                    the_max = max(the_max, int(Matrix[i][j]))
        return the_max*the_max


t1 = [['1', '0', '1', '0', '0'],
      ['1', '0', '1', '1', '1'],
      ['1', '1', '1', '1', '1'],
      ['1', '0', '0', '1', '0']]

obj = Solution()
print(obj.maximalSquare(t1))
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://maksimdan.gitbook.io/interview-practice-problems/leetcode_sessions/221-maximal-square.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
