Count 1 s in binary matrix having remaining indices of its row and column filled with 0 s | Function to count required 1 s from the given matrix ; Stores the dimensions of the mat ; Calculate sum of rows ; Calculate sum of columns ; Stores required count of 1 s ; If current cell is 1 and sum of row and column is 1 ; Increment count of 1 s ; Return the final count ; Driver Code ; Given matrix ; Function call
def numSpecial ( mat ) : NEW_LINE INDENT m = len ( mat ) NEW_LINE n = len ( mat [ 0 ] ) NEW_LINE rows = [ 0 ] * m NEW_LINE cols = [ 0 ] * n NEW_LINE i , j = 0 , 0 NEW_LINE for i in range ( m ) : NEW_LINE INDENT rows [ i ] = 0 NEW_LINE for j in range ( n ) : NEW_LINE INDENT rows [ i ] += mat [ i ] [ j ] NEW_LINE DEDENT DEDENT for i in range ( n ) : NEW_LINE INDENT cols [ i ] = 0 NEW_LINE for j in range ( m ) : NEW_LINE INDENT cols [ i ] += mat [ j ] [ i ] NEW_LINE DEDENT DEDENT cnt = 0 NEW_LINE for i in range ( m ) : NEW_LINE INDENT for j in range ( n ) : NEW_LINE INDENT if ( mat [ i ] [ j ] == 1 and rows [ i ] == 1 and cols [ j ] == 1 ) : NEW_LINE INDENT cnt += 1 NEW_LINE DEDENT DEDENT DEDENT return cnt NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT mat = [ [ 1 , 0 , 0 ] , [ 0 , 0 , 1 ] , [ 0 , 0 , 0 ] ] NEW_LINE print ( numSpecial ( mat ) ) NEW_LINE DEDENT