neighbors = np.concatenate([x[:, :-1].flatten(), x[:, +1:].flatten(), x[+1:,
    :].flatten(), x[:-1, :].flatten()])
centers = np.concatenate([x[:, +1:].flatten(), x[:, :-1].flatten(), x[:-1,
    :].flatten(), x[+1:, :].flatten()])
valid = neighbors != centers
import numpy_indexed as npi
regions, neighbors_per_regions = npi.group_by(centers[valid], neighbors[valid])
for region, neighbors_per_region in zip(regions, neighbors_per_regions):
    print(region)
unique_neighbors, neighbor_counts = npi.count(neighbors_per_region)
print((unique_neighbors, neighbor_counts / neighbor_counts.sum() * 100))
Count cells of adjacent numpy regions