refactored neighbour code for the square