added countAdjacent
incorporated square-root order