def palindrome(s):
    if(s==s[::-1] and len(s)>1):
        return True
    else:    
        return False

n=int(input())
l=[]
for i in range(n):
    l.append([j for j in input().split(",")])
ans=[]
s=""
top,bottom = 0,len(l)
left,right = 0,len(l[0])
while(top < bottom and left < right):
    for i in range(left,right):
        s+=l[top][i]
        if(palindrome(s)):
            ans.append(s)
            s=''
    top+=1
    for i in range(top,bottom):
        s+=l[i][right-1]
        if(palindrome(s)):
            ans.append(s)
            s=''
    right-=1
    if(not (top < bottom and left < right)):
        break
    for i in range(right-1,left-1,-1):
        s+=l[bottom-1][i]
        if(palindrome(s)):
            ans.append(s)
            s=''
    bottom-=1
    for i in range(bottom-1,top-1,-1):
        s+=l[i][left]
        if(palindrome(s)):
            ans.append(s)
            s=''
    left+=1
if(s!=''):
    ans.append(s)
    
print(*ans,sep=",")

Embed on website

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