Swap only when a [ i ] [ j ] > b [ i ] [ j ]
for i in range ( n ) : NEW_LINE INDENT for j in range ( m ) : NEW_LINE INDENT if a [ i ] [ j ] > b [ i ] [ j ] : NEW_LINE INDENT a [ i ] [ j ] , b [ i ] [ j ] = b [ i ] [ j ] , a [ i ] [ j ] NEW_LINE DEDENT DEDENT DEDENT