Given a 2D binary matrix filled with 0’s and 1’s, find the largest square containing only 1’s and return its area.


Input: 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 0 0 1 0 Output: 4


Its a matrix DP problem, Maintain state matrix based on previous states.

If current element having value as 1 then it could be part of matrix, Possible size of the square matrix ending at the current element could be :




We encourage you to write a comment if you have a better solution or having any doubt on the above topic.

Leave a Reply

Your email address will not be published. Required fields are marked *