In [166]:
import numpy as np
n = 1001

In [169]:
a = np.zeros( (n, n), np.int32)
x, y = int(n/2), int(n/2)
cx, cy =  x, y
dire = 0
dim = 1
for i in range(1,n**2+1):
    a[y, x] = i
    if dim ** 2 == i :
        dim += 1 
    if dire == 0: 
        if x < cx + dim /2:
            x += 1
        else:
            dire = 1
            y += 1
    elif dire == 1:
        if y < cy + dim /2:
            y += 1
        else:
            dire = 2
            x -= 1
    elif dire == 2:
        if x > cx - int(dim/2):
            x -= 1
        else:
            dire = 3
            y -= 1
    elif dire == 3:
        if y > cy - int(dim /2):
            y -= 1        
        else:
            dire = 0
            x += 1
print(a, '\nsec=', a[::-1,:].diagonal(), '\nmain',a.diagonal())
print(-1 + sum(a[::-1,:].diagonal()) + sum( a.diagonal()))


[[1001001 1001002 1001003 ..., 1001999 1002000 1002001]
 [1001000  997003  997004 ...,  998000  998001  998002]
 [1000999  997002  993013 ...,  994009  994010  998003]
 ..., 
 [1000003  996006  992017 ...,  991021  995006  998999]
 [1000002  996005  996004 ...,  995008  995007  999000]
 [1000001 1000000  999999 ...,  999003  999002  999001]] 
sec= [1000001  996005  992017 ...,  994009  998001 1002001] 
main [1001001  997003  993013 ...,  991021  995007  999001]
669171001