computing the distance matrices