Brute force solution using a fixed size grid, relying on the properties of the input to never exceed the bounds. Some bit manipulation tricks are used to speeds things up slightly.