<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>

class Solution {
public:
#if 1
    int maximalSquare(vector<vector<char>>& matrix) {
        vector<vector<short>> dp(matrix.size(), vector<short>(matrix[0].size()));
        bool exist_one = false;
        for (int i = 0; i < matrix.size(); i++) {
            for (int j = 0; j < matrix[0].size(); j++) {
                dp[i][j] = matrix[i][j] - '0';
                if (dp[i][j]) exist_one = true;
            }
        }
        if (!exist_one) {
            return 0;
        }
        short d = 1;
        for (int i = 1; i < dp.size(); i++) {
            for (int j = 1; j < dp[0].size(); j++) {
                if (dp[i][j]) {
                    dp[i][j] = 1+ min({dp[i-1][j], dp[i][j-1], dp[i-1][j-1]});
                    d = max(d, dp[i][j]);
                }
            }
        }
        return d*d;
   }
#else
    int maximalSquare(vector<vector<char>>& matrix) {
        bool exist_one = false;
        for (auto &m:matrix) {
            for (auto &v:m) {
                v -= '0';
                if (v) exist_one = true;
            }
        }
        if (!exist_one) {
            return 0;
        }
        int d = 0;
        bool updated = true;
        while (updated) {
            d++;
            int check = d+1;
            updated = false;
            for (int i = 0; i <= (int)matrix.size()-check; i++) {
                for (int j = 0; j <= (int)matrix[0].size()-check; j++) {
                    if (matrix[i][j] && matrix[i][j+1] && matrix[i+1][j] && matrix[i+1][j+1]) {
                        matrix[i][j] = 1;
                        updated = true;
                    } else {
                        matrix[i][j] = 0;
                    }
                }
            }
        }
        return d*d;
    }
#endif
};
23800