Fixed a bug in algorithm for determining neighbour cells.

Fixed a bug in algorithm for determining neighbour cells.