<p>Given an <code>m x n</code> binary matrix <code>mat</code>, <em>return the number of <strong>submatrices</strong> that have all ones</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/10/27/ones1-grid.jpg" style="width: 244px; height: 245px;" />
<pre>
<strong>Input:</strong> mat = [[1,0,1],[1,1,0],[1,1,0]]
<strong>Output:</strong> 13
<strong>Explanation:</strong> 
There are 6 rectangles of side 1x1.
There are 2 rectangles of side 1x2.
There are 3 rectangles of side 2x1.
There is 1 rectangle of side 2x2. 
There is 1 rectangle of side 3x1.
Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13.
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/10/27/ones2-grid.jpg" style="width: 324px; height: 245px;" />
<pre>
<strong>Input:</strong> mat = [[0,1,1,0],[0,1,1,1],[1,1,1,0]]
<strong>Output:</strong> 24
<strong>Explanation:</strong> 
There are 8 rectangles of side 1x1.
There are 5 rectangles of side 1x2.
There are 2 rectangles of side 1x3. 
There are 4 rectangles of side 2x1.
There are 2 rectangles of side 2x2. 
There are 2 rectangles of side 3x1. 
There is 1 rectangle of side 3x2. 
Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24.
</pre>

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

<ul>
	<li><code>1 &lt;= m, n &lt;= 150</code></li>
	<li><code>mat[i][j]</code> is either <code>0</code> or <code>1</code>.</li>
</ul>

class Solution {
public:
    int numSubmat(vector<vector<int>>& mat) {
        int n = mat.size();
        int m = mat[0].size();

        vector<int> h(m);
        int ans = 0;
        for(int i = 0; i < n; i++) {
            for(int j = 0; j < m; j++) {
                if(mat[i][j] == 1) h[j] += 1;
                else h[j] = 0;
            }

            for(int x : h) cout << x << " ";
            cout << endl;

            vector<int> s;
            for(int j = 0; j < m; j++) {
                // while(!s.empty() && h[s.back()] >= h[j]) {
                //     int idx = s.back();
                //     s.pop_back();
                //     int height = h[idx] - (s.empty() ? h[j] : h[s.back()]);
                //     int width = j - (s.empty() ? -1 : s.back()) - 1;
                //     ans += (height * (width * (width + 1) / 2));
                // }
                // s.push_back(j);
                int height = h[j];
                for(int k = j; k >= 0; k--) {
                    height = min(height, h[k]);
                    ans += height;
                }
            }

            // cout << ans << endl;

            // while(!s.empty()) {
            //     int idx = s.back();
            //     s.pop_back();
            //     int height = h[idx] - (s.empty() ? 0 : h[s.back()]);
            //     int width = m - (s.empty() ? -1 : s.back()) - 1;
            //     ans += (height * (width * (width + 1) / 2));
            // }

            // cout << ans << endl;
        }

        return ans;
    }
};
17300