<p>Given a <code>matrix</code>&nbsp;and a <code>target</code>, return the number of non-empty submatrices that sum to <font face="monospace">target</font>.</p>

<p>A submatrix <code>x1, y1, x2, y2</code> is the set of all cells <code>matrix[x][y]</code> with <code>x1 &lt;= x &lt;= x2</code> and <code>y1 &lt;= y &lt;= y2</code>.</p>

<p>Two submatrices <code>(x1, y1, x2, y2)</code> and <code>(x1&#39;, y1&#39;, x2&#39;, y2&#39;)</code> are different if they have some coordinate&nbsp;that is different: for example, if <code>x1 != x1&#39;</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/02/mate1.jpg" style="width: 242px; height: 242px;" />
<pre>
<strong>Input:</strong> matrix = [[0,1,0],[1,1,1],[0,1,0]], target = 0
<strong>Output:</strong> 4
<strong>Explanation:</strong> The four 1x1 submatrices that only contain 0.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> matrix = [[1,-1],[-1,1]], target = 0
<strong>Output:</strong> 5
<strong>Explanation:</strong> The two 1x2 submatrices, plus the two 2x1 submatrices, plus the 2x2 submatrix.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> matrix = [[904]], target = 0
<strong>Output:</strong> 0
</pre>

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

<ul>
	<li><code>1 &lt;= matrix.length &lt;= 100</code></li>
	<li><code>1 &lt;= matrix[0].length &lt;= 100</code></li>
	<li><code>-1000 &lt;= matrix[i][j] &lt;= 1000</code></li>
	<li><code>-10^8 &lt;= target &lt;= 10^8</code></li>
</ul>

#pragma GCC optimize("O3", "unroll-loops")
class Solution {
public:
    int alternative(vector<vector<int>>& matrix, int target, int n, int m){
        // Calculate prefix sums for every column
        for (int j = 0; j < m; j++) {
            for (int i = 1; i < n; i++) {
                matrix[i][j] += matrix[i-1][j];
            }
        }
        int ans= 0; 
        for(int r0 = 0 ; r0 < n ; r0++){
            for(int r = r0 ; r < n; r++){
                unordered_map<int , int> f;
                f[0] = 1;
                int sum =0;
                for(int c = 0 ; c < m ; c++){
                    int prev=(r0==0) ? 0 : matrix[r0-1][c];
                    sum += matrix[r][c]-prev;
                    int x=sum-target;
                    ans += f.count(x) ? f[x] : 0;
                    f[sum]++;
                }
            }
        }
        return ans;
    }

    int numSubmatrixSumTarget(vector<vector<int>>& matrix, int target) {
        int n = matrix.size(), m = matrix[0].size();
        if ( m>n ) return alternative(matrix, target, n, m);

        // Calculate prefix sums for every row
        for (int i = 0; i < n; i++) {
            for (int j = 1; j < m; j++) {
                matrix[i][j] += matrix[i][j-1];
            }
        }
        int ans= 0; 
        for(int c0 = 0 ; c0 < m ; c0++){
            for(int c = c0 ; c < m; c++){
                unordered_map<int , int> f;
                f[0] = 1;
                int sum =0;
                for(int r = 0 ; r < n ; r++){
                    int prev=(c0==0) ? 0 : matrix[r][c0-1];
                    sum += matrix[r][c]-prev;
                    int x= sum-target;
                    ans += f.count(x) ? f[x] : 0;
                    f[sum]++;
                }
            }
        }
        return ans;
    }
};


auto init = []()
{ 
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    return 'c';
}();
87225