The problem:
Given a NxN 0-1 matrix and a number K, count all sub matrices (any size) with exactly K 1's.
Constrains: 2 <= N <= 600, 1<= K <= 6
Example
1 0 1
0 0 0
1 0 1
Count: 8
With memoization of the sums of all possible sub matrices my algorithm has complexity O(n^4). I tried to combine various solutions for different but similar problems with no luck. I can't think a better way to reduce the time complexity. Could be done in O(n^3)?
I have read the following:
Minimum area submatrix with sum K
Minimal subrectangle with at least K 1's
This is a homework.