Optimised counting of neighbours