from collections import deque
def solution(places):
dx = [-1,0,1,0]
dy = [0,1,0,-1]
q = deque()
def bfs(x,y,check_box):
now_array = [[0 for _ in range(5)] for _ in range(5)]
visit = [[False for _ in range(5)] for _ in range(5)]
q.append((x,y))
visit[x][y] = True
while q:
now_x,now_y = q.popleft()
for i in range(4):
nx = now_x + dx[i]
ny = now_y + dy[i]
if 0<=nx<5 and 0<=ny<5 and check_box[nx][ny] == 0:
if visit[nx][ny] == False:
visit[nx][ny] = True
q.append((nx,ny))
now_array[nx][ny] = now_array[now_x][now_y] + 1
return now_array
answer = []
for array in places:
human = []
now = []
for i in range(5):
aa = []
for j in range(5):
if array[i][j] == 'X':
aa.append(1)
elif array[i][j] == 'P':
human.append([i,j])
aa.append(0)
else:
aa.append(0)
now.append(aa)
chk = True
for i in range(len(human)):
now_places = bfs(human[i][0],human[i][1],now)
for j in range(len(human)):
if j != i:
if 0 < now_places[human[j][0]][human[j][1]] < 3:
chk = False
break
if chk == False:
break
if chk:
answer.append(1)
else:
answer.append(0)
return answer