We don't need to continuously perform division and multiplication when computing the output cell for a value in the distance matrix where we know the neighboring value's output cell already. Instead, we can compute the cell boundaries and check whether the value we are computing is outside of them.