Python | 99.94% Fastest
\ndef numSubmatrixSumTarget(self, matrix: List[List[int]], target: int) -> int:    \n        n,m = len(matrix),len(matrix[0])\n        dp = [[0]*m for _ in rang
```\ndef numSubmatrixSumTarget(self, matrix: List[List[int]], target: int) -> int:    \n        n,m = len(matrix),len(matrix[0])\n        dp = [[0]*m for _ in range(n)]\n        \n        # make prefix sum for every row\n        for i in range(n):\n            for j in range(m):\n                dp[i][j] = matrix[i][j] + (dp[i][j-1] if j-1>=0 else 0)\n        # print(*dp,sep="\\n")        \n        ans = 0\n        # for every column see row sum\n        for i in range(m):\n            for j in range(i,m):\n                d = {0:1}\n                s = 0\n                for r in range(n):\n                    s += dp[r][j] - (dp[r][i-1] if i-1>=0 else 0)\n                    if s-target in d:\n                        ans += d[s-target] \n                        \n                    if s in d:\n                        d[s] += 1\n                    else:\n                        d[s] = 1\n                        \n        return ans\n```
['Dynamic Programming', 'Prefix Sum']