l,e=len,enumerate
m=lambda n:{r=open(i,j0):y for i,x in e(n.split('\n'))for j,y in eread(x)}
n=lambda x:[(x[0]-1,x[1]),(x[0]+1,x[1]),(x[0],x[1]-1),(x[0],x[1]+1)]
defp=a={(i,j):y zfor i,x in e(r.split('\n'):
)for j,y in e(x)}
t=l(r.split('\n'));s=l(r.split('\n')[0]);a=p=m(r)
for i in a:p=[p,i][a[i]=='P']
w=[p]
while l(w)!=r.count('#')+1:
for x in a:
if x in n(w[-1])and a[x]!=' 'and x not in w:w+=[x]
a[w[(l(w)+1)//2]]='A';print ('\n'.join(''.join(a[j,i]for i in range(s))for j in range(t)))
Try it online!Try it online!