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

class Solution
{
public:
    int numSubmatrixSumTarget(vector<vector<int>> &matrix, int target)
    {
        int m = size(matrix), n = size(matrix[0]);
        vector<vector<int>> prefixSum(m + 1, vector<int>(n + 1, 0));

        for (int r = 0; r < m; r++)
        {
            for (int c = 0; c < n; c++)
            {
                int left = 0, above = 0, prev = 0;
                if (c > 0)
                    left = prefixSum[r][c - 1];
                if (r > 0)
                    above = prefixSum[r - 1][c];
                if (r > 0 && c > 0)
                    prev = prefixSum[r - 1][c - 1];

                prefixSum[r][c] = matrix[r][c] + left + above - prev;
            }
        }

        int res = 0, sum = 0;
        unordered_map<int, int> counter;
        for (int rowStart = 0; rowStart < m; rowStart++)
        {
            for (int row = rowStart; row < m; row++)
            {
                counter.clear();
                counter[0] = 1;

                for (int col = 0; col < n; col++)
                {
                    int sum = prefixSum[row][col];
                    if (rowStart > 0)
                        sum -= prefixSum[rowStart - 1][col];
                    
                    // What we are trying to figure out is, 
                    // do we have any prefix sums that we can remove 
                    // that will make the current sum equal to the target, 
                    // not only that but since we have a hashmap, 
                    // we know how many prefix sums we have that we can remove to make sum = target

                    // Now for the equation below, you would think it would be the other way around,
                    // but think about it because diff is the value 
                    // that we need to remove from the current sum 
                    // to make the current sum equal to target
                    // sum - diff = target -> sum - target = diff
                    int diff = sum - target;
                    if (counter.count(diff))
                        res += counter[diff];

                    counter[sum]++;
                }
            }
        }
        return res;
    }
};
70375