Generalised the adjacent square checking loop.