Added grid bounds checking

Might make this optional, but this change ensures that the keys into
the grid are within the bounds of the grid dimensions.

It might be useful in some algorithms to allow out-of-bounds indices
allowing the grid to become a sort of torus...