<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:
    void solve(int i,int j,vector<vector<int>> &mat,int target,int &ans)
    {
        int n=mat.size();
        int m=mat[0].size();
        // vector<vector<int>> temp;
        vector<int> h(m);
        vector<int> v(n);
        vector<int> cur(m);
        h[j]=mat[i][j];
        v[i]=mat[i][j];
        // temp=mat;
        if(mat[i][j]==target)
        ans++;
        for(int ii=i+1;ii<n;ii++)
        {
            v[ii]=v[ii-1]+mat[ii][j];
            if(v[ii]==target)
            ans++;
        }
        for(int jj=j+1;jj<m;jj++)
        {
            h[jj]=h[jj-1]+mat[i][jj];
            if(h[jj]==target)
            ans++;
        }

        // for(int jj=j+1;jj<m;jj++)
        // {
        //     temp[i][jj]=temp[i][jj]+temp[i][jj-1];
        //     if(temp[i][jj]==target)
        //     ans++;
        // }
        // for(int ii=i+1;ii<n;ii++)
        // {
        //     temp[ii][j]=temp[ii][j]+temp[ii-1][j];
        //     if(temp[ii][j]==target)
        //     ans++;
        // }
        for(int ii=i+1;ii<n;ii++)
        {
            cur[j]=v[ii];
            for(int jj=j+1;jj<m;jj++)
            {
                // temp[ii][jj]+=temp[ii-1][jj]+temp[ii][jj-1]-temp[ii-1][jj-1];
                // if(temp[ii][jj]==target)
                // ans++;
                cur[jj]=mat[ii][jj]+cur[jj-1]+h[jj]-h[jj-1];
                if(cur[jj]==target)
                ans++;
            }
            h=cur;
        }
    }
    int numSubmatrixSumTarget(vector<vector<int>>& matrix, int target) {
        int n=matrix.size();
        int m=matrix[0].size();
        int ans=0;
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                solve(i,j,matrix,target,ans);
            }
        }
        return ans;
    }
};
19825