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]])
Efficient identification of adjacent elements in numpy matrix