updating the first row and col if 1 is encountered
for i in range ( 0 , len ( mat ) ) : NEW_LINE INDENT for j in range ( 0 , len ( mat ) ) : NEW_LINE INDENT if ( i == 0 and mat [ i ] [ j ] == 1 ) : NEW_LINE INDENT row_flag = True NEW_LINE DEDENT if ( j == 0 and mat [ i ] [ j ] == 1 ) : NEW_LINE INDENT col_flag = True NEW_LINE DEDENT if ( mat [ i ] [ j ] == 1 ) : NEW_LINE INDENT mat [ 0 ] [ j ] = 1 NEW_LINE mat [ i ] [ 0 ] = 1 NEW_LINE DEDENT DEDENT DEDENT