from collections import deque
def bfs(place):
start = []
for i in range(5):
for j in range(5):
if place[i][j] == 'P':
start.append([i, j])
for s in start:
queue = deque([s])
visited = [[False for _ in range(5)] for _ in range(5)]
distance = [[0 for _ in range(5)] for _ in range(5)]
queue.append((s[0], s[1]))
visited[s[0]][s[1]] = True
while queue:
y, x = queue.popleft()
dx = [0, 0, -1, 1]
dy = [-1, 1, 0, 0]
for i in range(4):
nx, ny = x + dx[i], y + dy[i]
if 0 <= nx <= 4 and 0 <= ny <= 4 and visited[ny][nx] == False:
if place[ny][nx] == "O":
queue.append([ny, nx])
visited[ny][nx] = True
distance[ny][nx] = distance[y][x] + 1
elif place[ny][nx] == "P" and distance[y][x] <= 1:
return False
return True
def solution(places):
answer = []
for place in places:
if bfs(place):
answer.append(1)
else:
answer.append(0)
return answer