<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 {
public:
    int countSquares(vector<vector<int>>& matrix) {
        int m = matrix.size();
        int n = matrix[0].size();

        int sq_sz[m][n];
        int up_sz[m][n];
        int lf_sz[m][n];

        int total=0;

        for (int i=0; i<m; i++) {
            for (int j=0; j<n; j++) {
                bool positive = (matrix[i][j] == 1);

                if (i==0 && j==0) {
                    if (positive) {sq_sz[i][j] = 1; up_sz[i][j] = 1; lf_sz[i][j] = 1;}
                    else          {sq_sz[i][j] = 0; up_sz[i][j] = 0; lf_sz[i][j] = 0;}
                    total = total+sq_sz[i][j];

                    continue;
                }

                if (j==0) {
                    if (positive) {sq_sz[i][j] = 1; up_sz[i][j] = up_sz[i-1][j]+1; lf_sz[i][j] = 1;}
                    else          {sq_sz[i][j] = 0; up_sz[i][j] = 0;               lf_sz[i][j] = 0;}
                                        total = total+sq_sz[i][j];

                    continue;
                }

                if (i==0) {
                    if (positive) {sq_sz[i][j] = 1; up_sz[i][j]=1;   lf_sz[i][j] = lf_sz[i][j-1] + 1;}
                    else          {sq_sz[i][j] = 0; up_sz[i][j] = 0; lf_sz[i][j] = 0;}
                                        total = total+sq_sz[i][j];

                    continue;
                }
                if (!positive) {sq_sz[i][j] = 0; up_sz[i][j] = 0; lf_sz[i][j] = 0;total = total+sq_sz[i][j]; continue;}
                up_sz[i][j] = up_sz[i-1][j] + 1;
                lf_sz[i][j] = lf_sz[i][j-1] + 1;

                int sq_candidate = m+n;
                if (up_sz[i][j] < sq_candidate) {sq_candidate = up_sz[i][j];}
                if (lf_sz[i][j] < sq_candidate) {sq_candidate = lf_sz[i][j];}
                if (sq_sz[i-1][j-1] + 1 < sq_candidate) {sq_candidate = sq_sz[i-1][j-1] + 1;}
                sq_sz[i][j] = sq_candidate;
                total = total+sq_candidate;
            }


        }
    return total;
    }
};

27300