You are trying to explain your program, not really to translate the code into mathematics. As such, standard notation works fine. It is up to you to make sure that your code does what you say it does. One thing to watch is that in many languages the indices start at $0$ while for most mathematical presentations of matrices they start at $1$. Using $1$ as the base, you are very close: I would say $$a_{ij}=\sum_{v=1}^n\sum_{w=1}^m D(i,j,v,w)$$ defining $D(i,j,i,j)=0$ so you don't mind having it in the sum. You can use whatever distance function $D$ you want. You could define the distance function as operating on ordered pairs, so it looks like $D((i,j),(v,w))$ if you want. It emphasizes the fact that it is the distance between two points. Then to add them up $$b=\sum_{i=1}^n\sum_{j=1}^ma_{ij}$$ I would like to see it stated somewhere that $a_{ij}$ is the $i,j$ element of $A$ rather than assuming that the reader will understand that.