How to fix this hash grid problem?

I have a simple hash grid that I use to keep track of objects which uses indices from 0 (bottom left) to width*height -1 (top right).

The selection at the edges seep through which is obviously unwanted behavior, A solution I came up with was to find the boundary vertices for the grid and use that to compare if any buckets selected are “passed” any corners but this requires many for-loops and seems excessive for a hash grid. I was wondering if there were any other solutions that might have flew over my head.

Is it a problem that the hash function happens to wrap?

Or is it that you are feeding wrapped inputs into the hash function?

Yes. The wrapping is something I don’t want happening.

I think he was saying, the issue isn’t that it’s wrapping; it’s that you are feeding it inputs that allow it to wrap.