This module should help you work with automatas on a grid.
Compute all 8 surrounding neighbors
Compute the 4 direct neighbors
Compute the 4 diagonal neighbors
Compute all 8 surrounding neighbors