<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(), m = mat[0].size();
        int ans = 0;

        vector<int> presum(m, 0);
        for (int i=0; i < n; i++) {
            for (int j=0; j<m; j++) {
                presum[j] = mat[i][j] + mat[i][j] * presum[j];
            }

            ans += countRectanglesInHistogram(presum);
        }

        return ans;
    }

    int countRectanglesInHistogram(vector<int> &nums) {
        int n = nums.size();
        stack<int> s;

        int ans = 0;
        
        // decreasing stack to find next, prev smaller

        nums.push_back(-1);
        for (int i=0; i<=n; i++) {
            while (s.size() > 0 && nums[s.top()] > nums[i]) {
                int top = s.top(); s.pop();

                int left = (s.size() > 0)? s.top() + 1: 0;
                int right = i-1;

                ans += nums[top] * (right - top + 1) * (top - left + 1);
            }
            if (i != n) 
                s.push(i);
        }
        nums.pop_back();

        return ans;
    }
};

/*
O(N * N * M)

class Solution {
public:
    int numSubmat(vector<vector<int>>& matrix) {
        ios::sync_with_stdio(0);
        cin.tie(0);
        cout.tie(0);
        
        int n = matrix.size(), m = matrix[0].size();
        int ans = 0;

        for (int c1=0; c1 < m; c1++) {
            vector<int> rowsToValue(n, 0);

            for (int c2=c1; c2 < m; c2++) {
                // Convert Row to single val
                // By taking total sum

                for (int r=0; r<n; r++)
                    rowsToValue[r] += matrix[r][c2];

                // Get Count of Subarrays w all elements equal to c2-c1+1
                // using 1-D Solution
                
                // printf("c1: %d, c2: %d\n", c1, c2);
                ans += subarraysWithAllEqual(rowsToValue, c2-c1+1);
            }
        }

        return ans;
    }

    int subarraysWithAllEqual(vector<int> &nums, int k) {
        int n = nums.size();
        int ans = 0;
        int count = 0;

        // cout<<k<<endl;

        // for (auto &i: nums)
        //     cout<<i<<" ";
        
        // cout<<endl;

        for (int i=0; i<n; i++) {
            if (nums[i] == k) {
                count += 1;
            } else {
                count = 0;
            }

            ans += count;
        }

        // cout<<ans<<endl;
        
        return ans;
    }
};

// https://leetcode.com/problems/number-of-submatrices-that-sum-to-target/description/

*/
20600