M1 = np.array([[0, 0, 0, 0, 0, 0, 0, 1], [0, 2, 2, 1, 1, 0, 0, 0], [0, 0, 2,
    0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 3, 3, 0, 0, 0, 0, 0]])
M2 = np.array([[0, 0, 0, 1, 1, 1, 1, 1], [0, 2, 2, 0, 1, 0, 0, 0], [0, 0, 2,
    0, 0, 0, 0, 0], [0, 3, 0, 0, 0, 0, 0, 0], [3, 3, 3, 0, 0, 0, 0, 0]])

def overlaps_eh(M):
    uniques = np.delete(np.unique(M), 0)
unival_inds = [np.transpose(np.where(M == unival)) for unival in uniques]
for i1, i2 in combinations(list(range(len(unival_inds))), 2):
    if np.any(cdist(unival_inds[i1], unival_inds[i2], 'cityblock') == 1):
        return True
return False
Efficient identification of adjacent elements in numpy matrix