l=[]
n=int(input())
for i in range(n):
    l.append([int(i) for i in input().split()])
print(l)    
ans=[]
top,bottom = 0,len(l)
left,right = 0,len(l[0])
while(top < bottom and left < right):
    for i in range(left,right):
        ans.append(l[top][i])
    top+=1
    for i in range(top,bottom):
        ans.append(l[i][right-1])
    right-=1
    if(not (top < bottom and left < right)):
        break
    for i in range(right-1,left-1,-1):
        ans.append(l[bottom-1][i])
    bottom-=1
    for i in range(bottom-1,top-1,-1):
        ans.append(l[i][left])
    left+=1
    
print("spiral matrix order:")
print(ans)

Embed on website

To embed this program on your website, copy the following code and paste it into your website's HTML: