computing distance mat