JavaScript 2D Prefix Sums - Clean and Simple
js\nvar numSubmatrixSumTarget = function(matrix, target) {\n    const prefixSums = new Array(matrix.length + 1)\n        .fill()\n        .map(() => new Array(m
```js\nvar numSubmatrixSumTarget = function(matrix, target) {\n    const prefixSums = new Array(matrix.length + 1)\n        .fill()\n        .map(() => new Array(matrix[0].length + 1).fill(0));\n    \n    for (let i = 1; i <= matrix.length; i++) {\n        for (let j = 1; j <= matrix[0].length; j++) {\n            prefixSums[i][j] = matrix[i-1][j-1] + prefixSums[i-1][j] + prefixSums[i][j-1] - prefixSums[i-1][j-1];\n        }\n    }\n    \n    let count = 0;\n    for (let r1 = 1; r1 <= matrix.length; r1++) {\n        for (let r2 = r1; r2 <= matrix.length; r2++) {\n            const counts = {};\n            counts[0] = 1;\n            for (let c = 1; c <= matrix[0].length; c++) {\n                const sum = prefixSums[r2][c] - prefixSums[r1 - 1][c];\n                count += counts[sum - target] || 0;\n                counts[sum] = (counts[sum] || 0) + 1;\n            }\n        }\n    }\n    \n    return count;\n};\n```
['JavaScript']