Swap only when a [ i ] [ j ] > b [ i ] [ j ]
for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < m ; j ++ ) if ( a [ i , j ] > b [ i , j ] ) { int tmp = a [ i , j ] ; a [ i , j ] = b [ i , j ] ; b [ i , j ] = tmp ; }