'At most k 1's submatrix in the matrix

I am solving a problem where we are given a square binary grid. We have to find the number of rectangles where the number of 1's is at most k in following time complexity: O(N^2 log(N)k).

I am not able to think of any approach to this time complexity. Is it even possible and if yes how can we approach it?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source