Fix reversed rows and columns when computing distances