Complete functions to count neighbors