#!/usr/bin/env python3 from queue import Queue def main(): r, s = map(int, input().split()) grid = list(list(False for x in range(s)) for y in range(r)) traps = set() # nacteme bludiste for y in range(r): line = input() for x in range(s): if line[x] != '#': grid[y][x] = True # True = pruchozi pole, pruchozi jsou vsechny krome "#" if line[x] == 'V': sy, sx = (y, x) # zadefinujeme pocatecni pozici elif line[x] == 'P': traps.add((y, x)) # pridame past reachable_traps = set() # sem budeme znacit odpovedi # Pouzijeme BFS, z kazde pasti pujdeme zvlast a urcime, jestli ji jde dosahnout for ty, tx in traps: neighbours = 0 # spocteme vsechny volne sousedy for ny, nx in ((ty + 1, tx), (ty - 1, tx), (ty, tx + 1), (ty, tx - 1)): if not (0 <= ny < r and 0 <= nx < s): continue if grid[ny][nx]: neighbours += 1 q = Queue() # fronta q.put((ty, tx)) visited = set() # policka, ktera uz jsme navstivili delayed = dict() # policka, ktera jsou "zpozdena" - mozna je navstivime, # ale jen pokud je muzeme dosahnout ze vsech jejich sousedu while not q.empty(): y, x = q.get() if (y, x) in visited: continue neighbours = 0 # spocteme vsechny volne sousedy for ny, nx in ((y + 1, x), (y - 1, x), (y, x + 1), (y, x - 1)): if not (0 <= ny < r and 0 <= nx < s): continue if grid[ny][nx]: neighbours += 1 if neighbours > 2 and (y, x) != (ty, tx) and not (y, x) in delayed.keys(): delayed[(y, x)] = neighbours - 2 # pridame nove policko do zpozdenych continue if (y, x) == (sy, sx): # jestli se umime dostat az ke startovnimu policku reachable_traps.add((ty, tx)) break visited.add((y, x)) for ny, nx in ((y + 1, x), (y - 1, x), (y, x + 1), (y, x - 1)): # pokusime se sousedy pridat do fronty if not (0 <= ny < r and 0 <= nx < s): continue if not grid[ny][nx]: continue if (ny, nx) in visited: continue if (ny, nx) in delayed.keys(): # pokud bylo oznaceno jako zpozdene delayed[(ny, nx)] -= 1 # oznacime, ze jsme ho dosahly z dalsiho smeru if delayed[(ny, nx)] == 0: # jestli uz je dosahnutelne ze vsech smeru q.put((ny, nx)) # muzeme pokracovat v hledani else: q.put((ny, nx)) print("".join('A' if trap in reachable_traps else 'N' for trap in sorted(traps))) main()