Consider a regular finite grid of $D$ dimensions, with $l$ points per dimension. Within the bulk of the grid, a point has $2D$ neighbours. For a point on an edge of the grid, however, some of these neighbours do not exist. For example, in the $1$-dimensional case, there are 2 points with 1 invalid neighbour, totalling 2 invalid neighbours.
I am trying to work out how many invalid neighbours there are in a grid of arbitrary $D$ and $l$, and also how many points there are with $n_{invalid}$ neighbours.
You are talking about a $D$ dimensional hypercube with a lattice inside it. The Wikipedia page has a section about the number of each piece of the hypercube, points, edges, faces, etc. For your question about invalid neighbors, note that you hypercube has $l^D$ lattice points in it. A hypercube of the same dimension but with $l+2$ points in each dimension will include your hypercube, the invalid points, and the outside corners.
If we position one corner of the hypercube at the origin the other is at $(l,l,l,\ldots)$ with $D$ coordinates. A point will have one invalid neighbor for each coordinate that is either $0$ or $l$ because adding or subtracting $1$ in that coordinate will take you outside the original hypercube.
For a point to have $k$ invalid neighbors, you choose $k$ of the $D$ coordinates to be on the edge, choose whether the coordinate is $0$ or $l$, and require that all the other coordinates be in the range $[1,l-1]$, so there are ${D \choose k}2^k(l-1)^{D-k}$ points with $k$ invalid neighbors.