implemented cost matrix