Number of square matrices with all 1 s | Javascript 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
var n = 3 var m = 3 function countSquareMatrices ( a , N , M ) { var count = 0 ; for ( var i = 1 ; i < N ; i ++ ) { for ( var j = 1 ; j < M ; j ++ ) { if ( a [ i ] [ j ] == 0 ) continue ; a [ i ] [ j ] = Math . min ( Math . min ( a [ i - 1 ] [ j ] , a [ i ] [ j - 1 ] ) , a [ i - 1 ] [ j - 1 ] ) + 1 ; } } for ( var i = 0 ; i < N ; i ++ ) for ( var j = 0 ; j < M ; j ++ ) count += a [ i ] [ j ] ; return count ; } var arr = [ [ 1 , 0 , 1 ] , [ 1 , 1 , 0 ] , [ 1 , 1 , 0 ] ] ; document . write ( countSquareMatrices ( arr , n , m ) ) ;