Number of square matrices with all 1 s | Python3 program to return the number of square submatrices with all 1 s ; Function to return the number of square submatrices with all 1 s ; Initialize count variable ; If a [ i ] [ j ] is equal to 0 ; Calculate number of square submatrices ending at ( i , j ) ; Calculate the sum of the array ; Driver code
n = 3 NEW_LINE m = 3 NEW_LINE def countSquareMatrices ( a , N , M ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT for j in range ( 1 , M ) : NEW_LINE INDENT if ( a [ i ] [ j ] == 0 ) : NEW_LINE INDENT continue NEW_LINE DEDENT a [ i ] [ j ] = min ( [ a [ i - 1 ] [ j ] , a [ i ] [ j - 1 ] , a [ i - 1 ] [ j - 1 ] ] ) + 1 NEW_LINE DEDENT DEDENT for i in range ( N ) : NEW_LINE INDENT for j in range ( M ) : NEW_LINE INDENT count += a [ i ] [ j ] NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT arr = [ [ 1 , 0 , 1 ] , [ 1 , 1 , 0 ] , [ 1 , 1 , 0 ] ] NEW_LINE print ( countSquareMatrices ( arr , n , m ) ) NEW_LINE