Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com

Maximum Area Square

Moderate
0/80
Average time to solve is 10m
12 upvotes
Asked in companies
FlipkartUrban Company (UrbanClap)Freshworks

Problem statement

You have been given a non-empty grid ‘MAT’ consisting of only 0s and 1s. Your task is to find the area of maximum size square sub-matrix with all 1s.

If there is no such sub-matrix, print 0.

For example, for the following grid:

Input

The area of the largest square submatrix with all 1s is 4.
Detailed explanation ( Input/output format, Notes, Images )
Constraints:
1 <= T <= 100
1 <= N <= 50
1 <= M <= 50
0 <= MAT[i][j] <= 1

Time limit: 1 sec
Sample Input 1:
1
2 2
1 0
0 0
Sample Output 1:
1
Explanation of the Sample Input 1:
For the given grid, the largest square with all 1s has a side of length 1. So, the area will be 1 * 1 = 1. 
Sample Input 2:
2
3 4
1 1 1 1
0 1 1 0
0 0 0 0
2 3
0 0 0
0 0 1    
Sample Output 2:
4
1
Full screen
Console