#!/usr/bin/python3 def find_start(grid): for y, line in enumerate(grid): for x, c in enumerate(line): if c == "C": return x, y # nacteni vstupu rows, columns = map(int, input().split()) grid = [] for i in range(rows): grid.append(list(input())) # nalezeni ciloveho policka - z nej zacneme prohledavat start = find_start(grid) stack = [start] while len(stack) != 0: x, y = stack.pop() # kontrola vsech sousedu policka na souradnicich (x, y) for d in ((1, 0), (0, 1), (-1, 0), (0, -1)): xn, yn = x + d[0], y + d[1] # kontrola, jestli se nachazime v hranicich mapy if not (0 <= xn < columns) or not (0 <= yn < rows): continue # prozkoumani sousedu aktualniho policka, ze kterych zatim nebyla nalezena cesta do cile if grid[yn][xn] != 'C' and grid[yn][xn] != '#' and ( (d == (1, 0) and grid[yn][xn] == 'Z') or (d == (0, 1) and grid[yn][xn] == 'S') or (d == (-1, 0) and grid[yn][xn] == 'V') or (d == (0, -1) and grid[yn][xn] == 'J') ): # pridani policka do zasobniku a oznaceni znakem '#' stack.append((xn, yn)) grid[yn][xn] = '#' # vypsani mapy for line in grid: for l in range(len(line)): if line[l] != 'C' and line[l] != '#': line[l] = '.' print("".join(line))