Extended solution to support n adjacent neighbors.