Supposingly distance_mat.size() = 630
then see the last iteration:
iindex = ibase + j = 629 * 630 / 2 = 198135
and then you try to access for j=0
: distance_mat[iindex=198135]
... which you do not have... Something feels wrong with the algorithm.