def dist(A, B):
    return abs(A[0] - B[0]) + abs(A[1] - B[1])

def grow(K, E):
    return {M for M in E for N in K if dist(M, N) <= 1}

def cohesive(E):
    K = {min(E)}
L = grow(K, E)
while len(K) < len(L):
    K, L = L, grow(L, E)
Check if some elements in a matrix are cohesive