to compute nxn matching