n_interior = abs(diff(a, axis=0)).sum() + abs(diff(a, axis=1)).sum()
n_boundary = a[(0), :].sum() + a[:, (0)].sum() + a[(-1), :].sum() + a[:, (-1)
    ].sum()
perimeter = n_interior + n_boundary
Calculate perimeter of numpy array