N = np.zeros(M.shape)
for i in range(M.shape[0]):
    for j in range(M.shape[1]):
        N[i][j] = sumNeighbors(M, i, j)
print('Original matrix:\n', M)
print('Summed neighbors matrix:\n', N)
Easiest way to return sum of a matrix's neighbors in numpy