Bomberman lives in a rectangular grid. Each cell in the grid either contains a bomb or nothing at all.

Each bomb can be planted in any cell of the grid but once planted, it will detonate after exactly 3 seconds. Once a bomb detonates, it's destroyed — along with anything in its four neighboring cells. This means that if a bomb detonates in cell $i,j$, any valid cells $(i\pm1,j)$ and $(i,j\pm1)$ are cleared. If there is a bomb in a neighboring cell, the neighboring bomb is destroyed without detonating, so there's no chain reaction.   

Bomberman is immune to bombs, so he can move freely throughout the grid. Here's what he does:

Initially, Bomberman arbitrarily plants bombs in some of the cells, the initial state.  
After one second, Bomberman does nothing.  
After one more second, Bomberman plants bombs in all cells without bombs, thus filling the whole grid with bombs.  No bombs detonate at this point.
After one more second, any bombs planted exactly three seconds ago will detonate. Here, Bomberman stands back and observes.
Bomberman then repeats steps 3 and 4 indefinitely.  

Note that during every second Bomberman plants bombs, the bombs are planted simultaneously (i.e., at the exact same moment), and any bombs planted at the same time will detonate at the same time.  

Given the initial configuration of the grid with the locations of Bomberman's first batch of planted bombs, determine the state of the grid after $N$ seconds.

For example, if the initial grid looks like:

...
.0.
...

it looks the same after the first second.  After the second second, Bomberman has placed all his charges:

000
000
000

At the third second, the bomb in the middle blows up, emptying all surrounding cells:

0.0
...
0.0

Function Description  

Complete the bomberMan function in the editory below.     

bomberMan has the following parameter(s):

int n: the number of seconds to simulate  
string grid[r]: an array of strings that represents the grid  

Returns  

string[r]: n array of strings that represent the grid in its final state

Input Format

The first line contains three space-separated integers $\textbf{r}$, $\textbf{c}$, and $n$, The number of rows, columns and seconds to simulate. 

Each of the next $\textbf{r}$ lines contains a row of the matrix as a single string of $\textbf{c}$ characters.  The . character denotes an empty cell, and the O character (ascii 79) denotes a bomb.  

Constraints

$1\leq r,c\leq200$  
$1\leq n\leq10^9$  

Subtask

$1\leq n\leq200$ for $\textbf{40\%}$ of the maximum score.

Sample Input
STDIN           Function
-----           --------
6 7 3           r = 6, c = 7, n = 3
.......         grid =['.......', '...0...', '....0..',\
...0...                '.......', '00.....', '00.....']
....0..
.......
00.....
00.....

Sample Output
000.000
00...00
000...0
..00.00
...0000
...0000

Explanation

The initial state of the grid is:

.......
...0...
....0..
.......
00.....
00.....

Bomberman spends the first second doing nothing, so this is the state after 1 second:

.......
...0...
....0..
.......
00.....
00.....

Bomberman plants bombs in all the empty cells during his second second, so this is the state after 2 seconds:

0000000
0000000
0000000
0000000
0000000
0000000

In his third second, Bomberman sits back and watches all the bombs he planted 3
seconds ago detonate. This is the final state after $3$ seconds:

000.000
00...00
000...0
..00.00
...0000
...0000

Source: hackerrank | Difficulty: MEDIUM
Tags: Implementation


Solutions:
def printGrid(grid):
	for row in grid:
		print(''.join(row))
(r, c, n) = [int(i) for i in input().strip().split()]
D = [['O' for i in range(c)] for j in range(r)]
G = []
for _ in range(r):
	G.append(list(input()))
S3 = [['.' for i in range(c)] for j in range(r)]
for i in range(r):
	for j in range(c):
		if not (j + 1 < c and G[i][j + 1] == 'O' or (j - 1 >= 0 and G[i][j - 1] == 'O') or (i - 1 >= 0 and G[i - 1][j] == 'O') or (i + 1 < r and G[i + 1][j] == 'O')):
			if G[i][j] != 'O':
				S3[i][j] = 'O'
S5 = [['.' for i in range(c)] for j in range(r)]
for i in range(r):
	for j in range(c):
		if not (j + 1 < c and S3[i][j + 1] == 'O' or (j - 1 >= 0 and S3[i][j - 1] == 'O') or (i - 1 >= 0 and S3[i - 1][j] == 'O') or (i + 1 < r and S3[i + 1][j] == 'O')):
			if S3[i][j] != 'O':
				S5[i][j] = 'O'
if n % 2 == 0:
	printGrid(D)
elif n == 1:
	printGrid(G)
elif (n + 1) % 4 == 0:
	printGrid(S3)
else:
	printGrid(S5)


import re

def isolated(r, c, grid):
	iso = True
	if r > 0:
		if grid[r - 1][c] == 'O':
			iso = False
	if r < R - 1:
		if grid[r + 1][c] == 'O':
			iso = False
	if c > 0:
		if grid[r][c - 1] == 'O':
			iso = False
	if c < C - 1:
		if grid[r][c + 1] == 'O':
			iso = False
	return iso

def plant(grid):
	A = [row for row in grid]
	for r in range(0, R):
		A[r] = re.sub('\\.', '2', A[r])
	for r in range(0, R):
		s = ''
		for c in range(0, C):
			if A[r][c] == '2' and (not isolated(r, c, A)):
				s += '1'
			else:
				s += A[r][c]
		A[r] = s
	return A

def detonate(grid):
	A = [row for row in grid]
	for r in range(0, R):
		A[r] = re.sub('O', '.', A[r])
		A[r] = re.sub('1', '.', A[r])
		A[r] = re.sub('2', 'O', A[r])
	return A
(R, C, N) = map(int, input().strip().split(' '))
grid = []
for row in range(0, R):
	grid.append(input().strip())
grid1 = [row for row in grid]
if N == 1:
	pass
elif N % 2 == 0:
	for row in range(0, R):
		grid[row] = 'O' * C
else:
	grid2 = plant(grid1)
	grid3 = detonate(grid2)
	if N % 4 == 3:
		grid = [row for row in grid3]
	else:
		grid4 = plant(grid3)
		grid5 = detonate(grid4)
		grid = [row for row in grid5]
for row in range(0, R):
	print(grid[row])


def main():
	(r, c, n) = [int(i) for i in input().split()]
	if not n & 1:
		return output(['1' * c] * r)
	elif n == 1:
		for i in range(r):
			print(input())
		return
	n //= 2
	grid = [[1 if i == 'O' else 0 for i in input()] for j in range(r)]
	grid = nextGen(grid, r, c)
	if not n & 1:
		grid = nextGen(grid, r, c)
	output(grid)

def nextGen(grid, r, c, adjacent=((0, 0), (0, 1), (0, -1), (1, 0), (-1, 0))):
	emptycells = frozenset(((i + dx, j + dy) for (j, row) in enumerate(grid) for (i, cell) in enumerate(row) if cell for (dx, dy) in adjacent))
	newgrid = [[0 if (i, j) in emptycells else 1 for i in range(c)] for j in range(r)]
	return newgrid

def output(grid):
	for r in grid:
		row = []
		for c in r:
			if c:
				row.append('O')
			else:
				row.append('.')
		print(''.join(row))
main()


(m, n, s) = [int(x) for x in input().split()]
s0 = []
s2 = []
s3 = []
States = []
for i in range(m):
	for j in input():
		s0.append(j)
States.append(list(s0))
stop = False
s -= 1
if s <= 0:
	for i in range(len(s0)):
		if i % n == 0 and i != 0:
			print()
		print(s0[i], end='')
	stop = True
while not stop:
	s3 = ['O' for i in range(m * n)]
	s -= 1
	if s <= 0:
		for i in range(len(s3)):
			if i % n == 0 and i != 0:
				print()
			print(s3[i], end='')
		break
	for i in range(m * n):
		if s0[i] == 'O':
			s3[i] = '.'
			(x, y) = divmod(i, n)
			if x != 0:
				s3[i - n] = '.'
			if x != m - 1:
				s3[i + n] = '.'
			if y != 0:
				s3[i - 1] = '.'
			if y != n - 1:
				s3[i + 1] = '.'
	s -= 1
	if s3 in States:
		pre = States.index(s3)
		(sta, rem) = divmod(s, 2)
		if rem == 1:
			for i in range(m):
				print('O' * n)
		else:
			i = States[pre + sta % (len(States) - pre)]
			for j in range(len(i)):
				if j % n == 0 and j != 0:
					print()
				print(i[j], end='')
		break
	else:
		States.append(list(s3))
	if s <= 0:
		for i in range(len(s3)):
			if i % n == 0 and i != 0:
				print()
			print(s3[i], end='')
		break
	s0 = list(s3)


(r, c, n) = map(int, input().strip().split())
arr = [[0] * (c + 2)]
for _ in range(r):
	arr.append([0] + list(map(lambda x: int(x == 'O'), input().strip())) + [0])
arr.append([0] * (c + 2))
outcome = [('O' * c + '\n') * r]
for _ in range(4):
	new_arr = [[0] * (c + 2) for _ in range(r + 2)]
	for row in range(1, r + 1):
		for col in range(1, c + 1):
			if 0 < arr[row][col] < 3 and all(map(lambda x: x < 3, [arr[row][col + i] for i in (1, -1)] + [arr[row + i][col] for i in (1, -1)])):
				new_arr[row][col] = arr[row][col] + 1
	outcome.append('\n'.join(map(lambda row: ''.join(map(lambda x: 'O' if x else '.', row[1:-1])), new_arr[1:-1])))
	for row in range(1, r + 1):
		for col in range(1, c + 1):
			new_arr[row][col] += 1
	arr = new_arr
if n % 2 == 0:
	print(outcome[0])
elif n < 8:
	print(outcome[n // 2 + 1])
else:
	print(outcome[n % 4 // 2 + 3])


(r, c, n) = [int(x) for x in input().split()]
mat = []
bomb = []
for i in range(r):
	t = list(input())
	mat.append(t)
	for j in range(c):
		if t[j] == 'O':
			bomb.append([i, j])
if n == 1:
	for i in mat:
		s = ''
		for j in i:
			s += j
		print(s)
elif n % 2 == 0:
	for i in range(r):
		print('O' * c)
else:
	for x in bomb:
		(i, j) = (x[0], x[1])
		mat[i][j] = 'b'
		if i - 1 >= 0:
			mat[i - 1][j] = 'b'
		if i + 1 < r:
			mat[i + 1][j] = 'b'
		if j - 1 >= 0:
			mat[i][j - 1] = 'b'
		if j + 1 < c:
			mat[i][j + 1] = 'b'
	bomb1 = []
	for i in range(r):
		for j in range(c):
			if mat[i][j] == '.':
				mat[i][j] = 'O'
				bomb1.append([i, j])
			else:
				mat[i][j] = '.'
	if int(n / 2) % 2 != 0:
		for i in mat:
			s = ''
			for j in i:
				s += j
			print(s)
	else:
		for x in bomb1:
			(i, j) = (x[0], x[1])
			mat[i][j] = 'b'
			if i - 1 >= 0:
				mat[i - 1][j] = 'b'
			if i + 1 < r:
				mat[i + 1][j] = 'b'
			if j - 1 >= 0:
				mat[i][j - 1] = 'b'
			if j + 1 < c:
				mat[i][j + 1] = 'b'
		for i in mat:
			s = ''
			for j in i:
				if j == 'b':
					s += '.'
				else:
					s += 'O'
			print(s)


(R, C, N) = [int(inp) for inp in input().strip().split(' ')]

def two_steps_later(G):
	H = []
	for i in range(R):
		Hrow = []
		for j in range(C):
			if G[i][j]:
				Hrow.append(False)
			elif i > 0 and G[i - 1][j]:
				Hrow.append(False)
			elif j > 0 and G[i][j - 1]:
				Hrow.append(False)
			elif i < R - 1 and G[i + 1][j]:
				Hrow.append(False)
			elif j < C - 1 and G[i][j + 1]:
				Hrow.append(False)
			else:
				Hrow.append(True)
		H.append(Hrow)
	return H
if N < 2:
	for r in range(R):
		print(input().strip())
elif N % 2 == 0:
	for r in range(R):
		print('O' * C)
else:
	G = []
	for r in range(R):
		G.append([c == 'O' for c in list(input().strip())])
	grids = [G]
	step = 1
	repeat = None
	while step < N:
		H = two_steps_later(grids[-1])
		step += 2
		for i in range(len(grids)):
			if grids[i] == H:
				repeat = i
		if repeat == None:
			grids.append(H)
		else:
			break
	if repeat == None:
		G = grids[-1]
	else:
		i = ((N - 1) // 2 - repeat) % (len(grids) - repeat) + repeat
		G = grids[i]
	for i in range(R):
		print(*map(lambda x: 'O' if x else '.', G[i]), sep='')


(R, C, N) = map(int, input().split())
G = [[0] * C for i in range(R)]
T = [[0] * C for i in range(R)]
for i in range(R):
	row = input()
	for j in range(C):
		if row[j] == 'O':
			G[i][j] = 3
if N > 4:
	N = 4 + (N & 3)
for n in range(N):
	for i in range(R):
		for j in range(C):
			g = G[i][j]
			if n & 1 and g == 0:
				g = 4
			T[i][j] = g - 1 if g else 0
	for i in range(R):
		for j in range(C):
			if G[i][j] == 1:
				if i > 0:
					T[i - 1][j] = 0
				if i < R - 1:
					T[i + 1][j] = 0
				if j > 0:
					T[i][j - 1] = 0
				if j < C - 1:
					T[i][j + 1] = 0
	(G, T) = (T, G)
for i in range(R):
	print(''.join(['O' if G[i][j] else '.' for j in range(C)]))


def show():
	for row in a:
		print(''.join(row).replace('-', 'O'))
(r, c, n0) = map(int, input().split())
a = [list(input().strip()) for _ in range(r)]
fuses = ['-', 'O']
fuse = 0
if n0 < 2:
	show()
else:
	n = 2
	while n - 5 < n0 % 4 if n0 > 5 else n <= n0:
		if (n - 2) % 2 == 0:
			for i in range(r):
				for j in range(c):
					if a[i][j] == '.':
						a[i][j] = fuses[fuse % 2]
			fuse += 1
		else:
			for i in range(r):
				for j in range(c):
					if a[i][j] == fuses[fuse % 2]:
						for (ii, jj) in [(0, 1), (1, 0), (0, -1), (-1, 0)]:
							if 0 <= i + ii < r and 0 <= j + jj < c and (a[i + ii][j + jj] != fuses[fuse % 2]):
								a[i + ii][j + jj] = '.'
						a[i][j] = '.'
		n += 1
	show()


def renew(G):
	X = [['O' for i in range(c)] for j in range(r)]
	for i in range(r):
		for j in range(c):
			if G[i][j] == 'O' or (j + 1 < c and G[i][j + 1] == 'O') or (j - 1 >= 0 and G[i][j - 1] == 'O') or (i - 1 >= 0 and G[i - 1][j] == 'O') or (i + 1 < r and G[i + 1][j] == 'O'):
				X[i][j] = '.'
	return X

def printGrid(grid):
	for row in grid:
		print(''.join(row))
	return
(r, c, n) = [int(i) for i in input().strip().split()]
if n % 2 == 0:
	printGrid([['O'] * c for i in range(r)])
else:
	G = [list(input()) for _ in range(r)]
	if n == 1:
		printGrid(G)
	elif n % 4 == 3:
		printGrid(renew(G))
	else:
		printGrid(renew(renew(G)))


from collections import Counter
import sys, math
from copy import deepcopy
(R, C, N) = map(int, input().strip().split(' '))
G = [list(input().strip()) for n in range(R)]
NB = '.'
B = 'O'
EB = 'X'

def printG(G):
	print('\n'.join(map(lambda x: ''.join(map(str, x)), G)))

def printErgG(G):
	print('\n'.join(map(lambda x: ''.join(map(str, x)), G)).replace(EB, B))

def plant(G):
	for r in range(R):
		for c in range(C):
			if G[r][c] == NB:
				G[r][c] = B
			elif G[r][c] == B:
				G[r][c] = EB

def explode(G):
	E = []
	for r in range(R):
		for c in range(C):
			if G[r][c] == EB:
				E.append((r, c))
	for (r, c) in E:
		G[r][c] = NB
		G[min(r + 1, R - 1)][c] = NB
		G[max(r - 1, 0)][c] = NB
		G[r][min(c + 1, C - 1)] = NB
		G[r][max(c - 1, 0)] = NB
A = []
if N in [0, 1]:
	printErgG(G)
	sys.exit()
plant(G)
if N == 2:
	printErgG(G)
	sys.exit()
explode(G)
if N == 3:
	printErgG(G)
	sys.exit()
plant(G)
if N == 4:
	printErgG(G)
	sys.exit()
explode(G)
if N == 5:
	printErgG(G)
	sys.exit()
A.append(deepcopy(G))
plant(G)
A.append(deepcopy(G))
explode(G)
A.append(deepcopy(G))
plant(G)
A.append(deepcopy(G))
N = (N - 5) % 4
printErgG(A[N])


import copy

def grid2string(grid):
	return '\n'.join([''.join(row) for row in grid])
(r, c, n) = [int(e) for e in input().strip().split(' ')]
grid = [[e for e in input().strip()] for i in range(r)]
full_grid = [['O' for col in range(c)] for row in range(r)]
if n in (0, 1):
	print(grid2string(grid))
elif n % 2 == 0:
	print(grid2string(full_grid))
else:
	steps = (n - 1) // 2
	if steps % 2 == 1:
		steps = 1
	else:
		steps = 2
	for s in range(steps):
		new_grid = copy.deepcopy(full_grid)
		for (i, row) in enumerate(grid):
			for (j, col) in enumerate(row):
				if col == 'O':
					new_grid[i][j] = '.'
		for (i, row) in enumerate(grid):
			for (j, col) in enumerate(row[1:], 1):
				if col == 'O':
					new_grid[i][j - 1] = '.'
			for (j, col) in enumerate(row[:-1]):
				if col == 'O':
					new_grid[i][j + 1] = '.'
		for (i, row) in enumerate(grid[1:], 1):
			for (j, col) in enumerate(row):
				if col == 'O':
					new_grid[i - 1][j] = '.'
		for (i, row) in enumerate(grid[:-1]):
			for (j, col) in enumerate(row):
				if col == 'O':
					new_grid[i + 1][j] = '.'
		grid = new_grid
	print(grid2string(grid))


(R, C, N) = (int(x) for x in input().strip().split(' '))

def p(board):
	for num in board:
		for c in range(C):
			print('O' if num >> c & 1 else '.', end='')
		print(' ')

def flip(board):
	newboard = [0] * (R + 2)
	for r in range(R):
		newboard[r] |= board[r]
		newboard[r + 1] |= board[r] << 1 | board[r] | board[r] >> 1
		newboard[r + 2] |= board[r]
	return [~x & (1 << C) - 1 for x in newboard[1:-1]]
if N % 2 == 0:
	for r in range(R):
		print('O' * C)
elif N == 1:
	for r in range(R):
		print(input().strip())
else:
	board = []
	for r in range(R):
		num = 0
		for (c, b) in enumerate(input()):
			if b == 'O':
				num |= 1 << c
		board.append(num)
	board = flip(board)
	if (N - 1) % 4 == 0:
		board = flip(board)
	p(board)


def boom(graph):
	newG = []
	r = len(graph)
	c = len(graph[0])
	for i in range(r):
		newG.append([])
		for j in range(c):
			newG[i].append(char)
	for i in range(r):
		for j in range(c):
			if graph[i][j] == char or (i + 1 < r and graph[i + 1][j] == char) or (j + 1 < c and graph[i][j + 1] == char) or (i - 1 >= 0 and graph[i - 1][j] == char) or (j - 1 >= 0 and graph[i][j - 1] == char):
				newG[i][j] = '.'
	return newG

def show(graph):
	for i in range(r):
		for j in range(c):
			print(graph[i][j], end='')
		print()
(r, c, n) = list(map(int, input().strip().split()))
graph = [input().strip() for _ in range(r)]
char = 'O'
if n == 1:
	show(graph)
elif n % 2 == 0:
	for _ in range(r):
		for _ in range(c):
			print(char, end='')
		print()
else:
	times = 2 - (n - 1) // 2 % 2
	for _ in range(times):
		newG = boom(graph)
		graph = newG
	show(newG)


def bomb(b, r, c):
	field = [['O' for i in range(c)] for j in range(r)]
	for i in range(r):
		for j in range(c):
			if b[i][j] == 'O':
				field[i][j] = '.'
				if i + 1 < r:
					field[i + 1][j] = '.'
				if i > 0:
					field[i - 1][j] = '.'
				if j + 1 < c:
					field[i][j + 1] = '.'
				if j > 0:
					field[i][j - 1] = '.'
	return field
(r, c, n) = input().split()
(r, c, n) = (int(r), int(c), int(n))
b = []
for i in range(r):
	row = list(input())
	b.append(row)
if n % 2 == 0:
	f = [['O' for i in range(c)] for j in range(r)]
	for i in range(r):
		print(''.join(map(str, f[i])))
else:
	bombed1 = bomb(b, r, c)
	bombed2 = bomb(bombed1, r, c)
	if n == 1:
		for i in range(r):
			print(''.join(map(str, b[i])))
	elif (n + 1) % 4 == 0:
		for i in range(r):
			print(''.join(map(str, bombed1[i])))
	elif (n + 2) % 4 == 0:
		for i in range(r):
			print(''.join(map(str, b[i])))
	else:
		for i in range(r):
			print(''.join(map(str, bombed2[i])))


from copy import deepcopy

def inverse(r, c, matrix):
	result = [list(row) for row in matrix]
	for i in range(r):
		for j in range(c):
			if matrix[i][j] == 'O':
				if i - 1 >= 0:
					result[i - 1][j] = 'O'
				if i + 1 <= r - 1:
					result[i + 1][j] = 'O'
				if j - 1 >= 0:
					result[i][j - 1] = 'O'
				if j + 1 <= c - 1:
					result[i][j + 1] = 'O'
	for i in range(r):
		for j in range(c):
			if result[i][j] == '.':
				result[i][j] = 'O'
			else:
				result[i][j] = '.'
	return [''.join(row) for row in result]
(r, c, n) = map(int, input().split())
matrix = [input().strip() for _ in range(r)]
inv = inverse(r, c, matrix)
inv_inv = inverse(r, c, inv)
all = ['O' * c for _ in range(r)]
result = {0: inv_inv, 1: all, 2: inv, 3: all}
if n == 1:
	print(*matrix, sep='\n')
else:
	print(*result[(n - 1) % 4], sep='\n')


def explode(r, c):
	state[r] = state[r][:c] + '.' + state[r][c + 1:]
	if r > 0 and state[r - 1][c:c + 1] != '0':
		state[r - 1] = state[r - 1][:c] + '.' + state[r - 1][c + 1:]
	if r < R - 1 and state[r + 1][c:c + 1] != '0':
		state[r + 1] = state[r + 1][:c] + '.' + state[r + 1][c + 1:]
	if c > 0 and state[r][c - 1:c] != '0':
		state[r] = state[r][:c - 1] + '.' + state[r][c:]
	if c < C - 1 and state[r][c + 1:c + 2] != '0':
		state[r] = state[r][:c + 1] + '.' + state[r][c + 2:]
(R, C, N) = map(int, input().strip().split())
state = []
for r in range(R):
	s = input().strip()
	state.append(s)
cycle = 0
states = []
for n in range(N + 1):
	if n == 0:
		for r in range(R):
			state[r] = state[r].replace('O', '3')
	elif n % 2 == 0:
		for r in range(R):
			state[r] = state[r].replace('1', '0')
			state[r] = state[r].replace('2', '1')
			state[r] = state[r].replace('3', '2')
		for r in range(R):
			state[r] = state[r].replace('.', '3')
	else:
		for r in range(R):
			state[r] = state[r].replace('1', '0')
			state[r] = state[r].replace('2', '1')
			state[r] = state[r].replace('3', '2')
		for r in range(R):
			for c in range(C):
				if state[r][c:c + 1] == '0':
					explode(r, c)
	for (index, value) in enumerate(states):
		if value == state:
			cycle = n - index
			break
	if cycle == 0:
		states.append(list(state))
	else:
		break
if cycle > 0:
	state = states[index + (N - index) % cycle]
for r in range(R):
	state[r] = state[r].replace('3', 'O')
	state[r] = state[r].replace('2', 'O')
	state[r] = state[r].replace('1', 'O')
	state[r] = state[r].replace('0', 'O')
	print(state[r])


import copy

def print_array(arr):
	for i in range(len(arr)):
		string = ''.join((str(c) for c in arr[i]))
		print(string)

def convert_numbers(arr):
	arr_temp = []
	for r in range(len(arr)):
		inner_list = []
		for c in range(len(arr[r])):
			if isinstance(arr[r][c], int):
				inner_list.append('O')
			else:
				inner_list.append('.')
		arr_temp.append(inner_list)
	return arr_temp

def tick(arr):
	explosion_map = []
	for r in range(len(arr)):
		inner_list = []
		for c in range(len(arr[r])):
			inner_list.append(0)
		explosion_map.append(inner_list)
	for r in range(len(arr)):
		for c in range(len(arr[r])):
			if isinstance(arr[r][c], int):
				arr[r][c] -= 1
			if arr[r][c] == 0:
				explosion_map[r][c] = 1
				if r > 0:
					explosion_map[r - 1][c] = 1
				if r < len(arr) - 1:
					explosion_map[r + 1][c] = 1
				if c > 0:
					explosion_map[r][c - 1] = 1
				if c < len(arr[r]) - 1:
					explosion_map[r][c + 1] = 1
	for r in range(len(explosion_map)):
		for c in range(len(explosion_map[r])):
			if explosion_map[r][c] == 1:
				arr[r][c] = '.'

def fill_array(arr):
	for r in range(len(arr)):
		for c in range(len(arr[r])):
			if arr[r][c] == '.':
				arr[r][c] = 3

def copy_list(arr):
	arr_temp = []
	for r in range(len(arr)):
		inner_list = []
		for c in range(len(arr[r])):
			inner_list.append(arr[r][c])
		arr_temp.append(inner_list)
	return arr_temp
(rows, cols, seconds) = input().strip().split(' ')
(rows, cols, seconds) = [int(rows), int(cols), int(seconds)]
arr = []
for i in range(rows):
	arr.append(list(input()))
for r in range(rows):
	for c in range(cols):
		if arr[r][c] == 'O':
			arr[r][c] = 3
state_0 = copy_list(arr)
for t in range(1, 6):
	tick(arr)
	if t % 2 == 0:
		fill_array(arr)
	if t == 2:
		state_1 = copy_list(arr)
	if t == 3:
		state_2 = copy_list(arr)
	if t == 5:
		state_3 = copy_list(arr)
if seconds == 0 or seconds == 1:
	end_state = state_0
elif (seconds - 1) % 2 != 0:
	end_state = state_1
elif (seconds - 1) % 4 == 0:
	end_state = state_3
elif (seconds - 1) % 2 == 0:
	end_state = state_2
result = convert_numbers(end_state)
print_array(result)


(R, C, n) = [int(x) for x in input().strip().split(' ')]
grid = []
for c in range(R):
	grid.append(list(input().strip()))
import copy
bombs = [['O'] * C for m in range(R)]

def printg(grid):
	for r in grid:
		print(''.join(r))

def alter(grid):
	grid2 = copy.deepcopy(grid)
	for (i, r) in enumerate(grid):
		for (j, c) in enumerate(grid[i]):
			if grid[i][j] == 'O':
				grid2[i][j] = '.'
				continue
			for (i2, j2) in ((0, 1), (0, -1), (1, 0), (-1, 0)):
				try:
					k = grid[i + i2][j + j2]
					if i + i2 >= 0 and j + j2 >= 0 and (grid[i + i2][j + j2] == 'O'):
						grid2[i][j] = '.'
						break
				except IndexError:
					pass
			else:
				grid2[i][j] = 'O'
	return grid2

def endstate(grid, n):
	if n == 0 or n == 1:
		return grid
	if n % 2 == 0:
		return bombs
	if n % 4 == 1:
		return alter(alter(grid))
	else:
		return alter(grid)
printg(endstate(grid, n))


(r, c, n) = (int(x) for x in input().split())
bombs = [[-1 if x == '.' else 1 for x in list(input())] for _ in range(r)]

def time(seconds=1):
	for i in range(r):
		for j in range(c):
			if bombs[i][j] != -1:
				bombs[i][j] += seconds

def plantall():
	for i in range(r):
		for j in range(c):
			if bombs[i][j] == -1:
				bombs[i][j] = 0

def detonate(seconds=1):
	for i in range(r):
		for j in range(c):
			if bombs[i][j] >= seconds:
				bombs[i][j] = -1
				if i - 1 >= 0 and bombs[i - 1][j] < seconds:
					bombs[i - 1][j] = -1
				if i + 1 < r and bombs[i + 1][j] < seconds:
					bombs[i + 1][j] = -1
				if j - 1 >= 0 and bombs[i][j - 1] < seconds:
					bombs[i][j - 1] = -1
				if j + 1 < c and bombs[i][j + 1] < seconds:
					bombs[i][j + 1] = -1

def doall():
	time()
	plantall()
	detonate()

def printstate():
	for row in bombs:
		print(''.join(('.' if x == -1 else 'O' for x in row)))
if n <= 1:
	printstate()
elif n % 2 == 0:
	print('\n'.join(('O' * c for _ in range(r))))
else:
	doall()
	if n == 3:
		printstate()
	else:
		doall()
		if (n - 1) // 2 % 2 == 0:
			printstate()
		else:
			doall()
			printstate()


(r, c, n) = input().split()
(r, c, n) = (int(r), int(c), int(n))
b = [[] * c for i in range(r)]
for i in range(r):
	b[i] = list(input())
if n % 2 == 0:
	b = [['O'] * c for i in range(r)]
	for i in range(r):
		print(''.join(map(str, b[i])))
elif n == 1:
	for i in range(r):
		print(''.join(map(str, b[i])))
else:
	k = 2
	while k:
		k -= 1
		b1 = [['O'] * c for i in range(r)]
		for i in range(r):
			for j in range(c):
				if b[i][j] == 'O':
					b1[i][j] = '.'
					ii = i + 1
					jj = j + 1
					iii = i - 1
					jjj = j - 1
					if ii == r:
						ii = r - 1
					if jj == c:
						jj = c - 1
					if iii == -1:
						iii = 0
					if jjj == -1:
						jjj = 0
					b1[ii][j] = b1[iii][j] = b1[i][jj] = b1[i][jjj] = '.'
		if n % 4 == 1:
			b = b1
		else:
			b = b1
			break
	del b1
	for i in range(r):
		print(''.join(map(str, b[i])))


(R, C, N) = [int(i) for i in input().strip().split(' ')]
dict = {}
matrix0 = [[0 for i in range(C)] for j in range(R)]
matrix1 = [[1 for i in range(C)] for j in range(R)]
matrix2 = [[1 for i in range(C)] for j in range(R)]
matrix3 = [[1 for i in range(C)] for j in range(R)]
matrix4 = [[1 for i in range(C)] for j in range(R)]
row_index = 0
col_index = 0
for i in range(R):
	col_index = 0
	for char in input().strip():
		if char == '.':
			matrix0[row_index][col_index] = 0
			dict[row_index, col_index] = 0
		else:
			matrix0[row_index][col_index] = 1
			dict[row_index, col_index] = 1
		col_index += 1
	row_index += 1
row_index = 0
for i in range(R):
	col_index = 0
	for j in range(C):
		if matrix0[row_index][col_index] == 1:
			matrix2[max(0, row_index - 1)][col_index] = 0
			matrix2[min(R - 1, row_index + 1)][col_index] = 0
			matrix2[row_index][max(0, col_index - 1)] = 0
			matrix2[row_index][min(C - 1, col_index + 1)] = 0
			matrix2[row_index][col_index] = 0
		col_index += 1
	row_index += 1
row_index = 0
for i in range(R):
	col_index = 0
	for j in range(C):
		if matrix2[row_index][col_index] == 1:
			matrix4[max(0, row_index - 1)][col_index] = 0
			matrix4[min(R - 1, row_index + 1)][col_index] = 0
			matrix4[row_index][max(0, col_index - 1)] = 0
			matrix4[row_index][min(C - 1, col_index + 1)] = 0
			matrix4[row_index][col_index] = 0
		col_index += 1
	row_index += 1
if N == 1:
	result_matrix = matrix0
elif N == 2:
	result_matrix = matrix1
elif N % 2 == 0:
	result_matrix = matrix3
elif (N - 1) // 2 % 2 == 0:
	result_matrix = matrix4
else:
	result_matrix = matrix2
for i in range(R):
	row_string = ''
	for j in range(C):
		if result_matrix[i][j] == 1:
			row_string = row_string + 'O'
		else:
			row_string = row_string + '.'
	print(row_string)


def bomberman(grid, n):
	for i in range(2, n + 1):
		blit = []
		if i % 2 == 1:
			for r in range(len(grid)):
				blit.append(grid[r][:])
				for c in range(len(grid[r])):
					if blit[r][c] != -1 and (grid[r][c] == i - 3 or (r > 0 and grid[r - 1][c] == i - 3) or (r < len(grid) - 1 and grid[r + 1][c] == i - 3) or (c > 0 and grid[r][c - 1] == i - 3) or (c < len(grid[r]) - 1 and grid[r][c + 1] == i - 3)):
						blit[r][c] = -1
			if i >= 3 and ((n + 1) % 4 == 0 and (i + 1) % 4 == 0) or ((n - 1) % 4 == 0 and (i - 1) % 4 == 0):
				return blit
		else:
			for row in grid:
				blit.append(list(map(lambda x: i if x == -1 else x, row)))
			if n % 2 == 0:
				return blit
		(blit, grid) = (grid, blit)
	return grid

def printbomberman(grid):
	for row in grid:
		print(''.join(map(lambda x: '.' if x == -1 else 'O', row)))
(r, c, n) = map(int, input().split())
grid = []
for _ in range(r):
	grid.append(list(map(lambda x: 0 if x == 'O' else -1, list(input()))))
newgrid = bomberman(grid, n)
printbomberman(newgrid)


from sys import stderr
(r, c, n) = [int(x) for x in input().strip().split()]
init = []
full = []
other = []
again = []
for _ in range(r):
	row = list(input())
	init.append(row)
	nf = ['O'] * c
	full.append(nf)
	other.append(nf.copy())
	again.append(nf.copy())
for i in range(r):
	for j in range(c):
		if init[i][j] == 'O':
			other[i][j] = '.'
			if i > 0:
				other[i - 1][j] = '.'
			if i < r - 1:
				other[i + 1][j] = '.'
			if j > 0:
				other[i][j - 1] = '.'
			if j < c - 1:
				other[i][j + 1] = '.'
for i in range(r):
	for j in range(c):
		if other[i][j] == 'O':
			again[i][j] = '.'
			if i > 0:
				again[i - 1][j] = '.'
			if i < r - 1:
				again[i + 1][j] = '.'
			if j > 0:
				again[i][j - 1] = '.'
			if j < c - 1:
				again[i][j + 1] = '.'
loop = [full, again, full, other]
loop = n % 4
if n == 1:
	answer = init
elif loop == 0 or loop == 2:
	answer = full
elif loop == 1:
	answer = again
elif loop == 3:
	answer = other
print(n, n % 4, file=stderr)
print('\n'.join((''.join(row) for row in answer)))


def explode_field(r, c, grid):
	new_grid = [list('O' * c) for _ in range(r)]
	for irow in range(r):
		for icol in range(c):
			if grid[irow][icol] == 'O':
				new_grid[irow][icol] = '.'
				if icol - 1 >= 0:
					new_grid[irow][icol - 1] = '.'
				if icol + 1 < c:
					new_grid[irow][icol + 1] = '.'
				if irow - 1 >= 0:
					new_grid[irow - 1][icol] = '.'
				if irow + 1 < r:
					new_grid[irow + 1][icol] = '.'
	return [''.join(row) for row in new_grid]

def bomber_man(r, c, n, grid):
	if n <= 1:
		return '\n'.join(grid)
	elif n % 2 == 0:
		return '\n'.join(['O' * c] * r)
	elif (n - 1) % 4 != 0:
		return '\n'.join(explode_field(r, c, grid))
	else:
		grid2 = explode_field(r, c, grid)
		return '\n'.join(explode_field(r, c, grid2))
(r, c, n) = [int(i) for i in input().split()]
grid = []
for _ in range(r):
	grid.append(input())
print(bomber_man(r, c, n, grid))


import sys
(r, c, n) = [int(t) for t in input().strip().split(' ')]
grid = []
for i in range(r):
	grid.append(list(input()))
if n == 0 or n == 1:
	for i in range(r):
		print(''.join(grid[i]))
	sys.exit()
if n % 2 == 0:
	for i in range(r):
		print(''.join(['O'] * c))
	sys.exit()
for i in range(r):
	for j in range(c):
		if grid[i][j] == 'O':
			if i != 0 and grid[i - 1][j] != 'O':
				grid[i - 1][j] = '*'
			if i != r - 1 and grid[i + 1][j] != 'O':
				grid[i + 1][j] = '*'
			if j != 0 and grid[i][j - 1] != 'O':
				grid[i][j - 1] = '*'
			if j != c - 1 and grid[i][j + 1] != 'O':
				grid[i][j + 1] = '*'
for i in range(r):
	for j in range(c):
		if grid[i][j] == '.':
			grid[i][j] = 'O'
		else:
			grid[i][j] = '.'
if n % 4 == 3:
	for i in range(r):
		print(''.join(grid[i]))
	sys.exit()
for i in range(r):
	for j in range(c):
		if grid[i][j] == 'O':
			if i != 0 and grid[i - 1][j] != 'O':
				grid[i - 1][j] = '*'
			if i != r - 1 and grid[i + 1][j] != 'O':
				grid[i + 1][j] = '*'
			if j != 0 and grid[i][j - 1] != 'O':
				grid[i][j - 1] = '*'
			if j != c - 1 and grid[i][j + 1] != 'O':
				grid[i][j + 1] = '*'
for i in range(r):
	for j in range(c):
		if grid[i][j] == '.':
			grid[i][j] = 'O'
		else:
			grid[i][j] = '.'
for i in range(r):
	print(''.join(grid[i]))


import copy
(R, C, N) = map(int, input().split())
blist = []
for i in range(R):
	blist.append(list(input()))
orglist = copy.deepcopy(blist)

def update_list(org, b):
	for i in range(R):
		for j in range(C):
			if org[i][j] == 'O':
				if j > 0:
					b[i][j - 1] = 'O'
				if i > 0:
					b[i - 1][j] = 'O'
			if j > 0 and org[i][j - 1] == 'O':
				b[i][j] = 'O'
			if i > 0 and org[i - 1][j] == 'O':
				b[i][j] = 'O'
	for i in range(R):
		for j in range(C):
			if b[i][j] == '.':
				b[i][j] = 'O'
			elif b[i][j] == 'O':
				b[i][j] = '.'
update_list(orglist, blist)
blist_new = copy.deepcopy(blist)
update_list(blist, blist_new)
if N % 2 == 0:
	for i in range(R):
		for j in range(C):
			print('O', end='')
		print()
elif N == 1:
	for i in range(R):
		for j in range(C):
			print(orglist[i][j], end='')
		print()
elif N % 4 == 1:
	for i in range(R):
		for j in range(C):
			print(blist_new[i][j], end='')
		print()
else:
	for i in range(R):
		for j in range(C):
			print(blist[i][j], end='')
		print()


import sys
buf = sys.stdin.readlines()
(R, C, N) = map(int, buf[0].split())
grid = [['.' for i in range(C + 2)]]
for line in buf[1:]:
	grid.append(['.'] + list(map(lambda x: x.strip(), line.strip())) + ['.'])
grid.append(['.' for i in range(C + 2)])

def dump_grid(g):
	print('\n'.join([''.join(row[1:C + 1]) for row in g[1:R + 1]]))

def all_bombs():
	g = [['O' for i in range(C + 2)] for j in range(R + 2)]
	return g
if N == 1:
	dump_grid(grid)
	sys.exit()
if N % 2 == 0:
	dump_grid(all_bombs())
	sys.exit()
step = 3
last = []
while True:
	buff = all_bombs()
	for i in range(1, R + 1):
		for j in range(1, C + 1):
			if grid[i][j] == 'O':
				buff[i][j] = '.'
				buff[i + 1][j] = '.'
				buff[i - 1][j] = '.'
				buff[i][j + 1] = '.'
				buff[i][j - 1] = '.'
	grid = buff
	if step == N or grid == last:
		dump_grid(grid)
		sys.exit()
	elif step == 5 and (N - 5) % 4 == 0:
		dump_grid(grid)
		sys.exit()
	elif step == 7 and (N - 7) % 4 == 0:
		dump_grid(grid)
		sys.exit()
	last = grid
	step += 2


class Celula:

	def __init__(self, conteudo):
		self.conteudo = conteudo
		self.tempo = 0

	def setconteudo(self, cont):
		self.conteudo = cont

	def incrtempo(self):
		self.tempo = self.tempo + 1

	def limpa(self):
		self.conteudo = '.'
		self.tempo = 0

def vizinhos(r, c, x, y):
	v = []
	if x - 1 >= 0:
		v.append([x - 1, y])
	if x + 1 <= r - 1:
		v.append([x + 1, y])
	if y - 1 >= 0:
		v.append([x, y - 1])
	if y + 1 <= c - 1:
		v.append([x, y + 1])
	return v

def detonebombs(grid, r, c):
	for a in range(r):
		for b in range(c):
			if grid[a][b].tempo == 3:
				grid[a][b].limpa()
				v = vizinhos(r, c, a, b)
				for w in range(len(v)):
					viz = v[w]
					if grid[viz[0]][viz[1]].tempo < 3:
						grid[viz[0]][viz[1]].limpa()

def plantbombs(grid, r, c):
	for a in range(r):
		for b in range(c):
			grid[a][b].setconteudo('O')

def incrtempogrid(grid, r, c):
	for a in range(r):
		for b in range(c):
			if grid[a][b].conteudo == 'O':
				grid[a][b].incrtempo()

def printgrid(grid):
	for a in range(r):
		l = ''
		for b in range(c):
			l = l + grid[a][b].conteudo
		print(l)
(r, c, n) = input().strip().split(' ')
(r, c, n) = [int(r), int(c), int(n)]
line = ''
grid = []
for cont in range(r):
	line = input()
	v_line = []
	for i in range(len(line)):
		cel = Celula(line[i])
		v_line.append(cel)
	grid.append(v_line)
if n > 10:
	n = (n - 3) % 4 + 3
if n == 1:
	printgrid(grid)
elif n == 2:
	plantbombs(grid, r, c)
	printgrid(grid)
elif n == 3:
	incrtempogrid(grid, r, c)
	incrtempogrid(grid, r, c)
	plantbombs(grid, r, c)
	incrtempogrid(grid, r, c)
	detonebombs(grid, r, c)
	printgrid(grid)
else:
	incrtempogrid(grid, r, c)
	incrtempogrid(grid, r, c)
	plantbombs(grid, r, c)
	incrtempogrid(grid, r, c)
	detonebombs(grid, r, c)
	for z in range(4, n + 1):
		incrtempogrid(grid, r, c)
		if z % 2 == 0:
			plantbombs(grid, r, c)
		else:
			detonebombs(grid, r, c)
	printgrid(grid)


def cleared(G, i, j):
	if G[i][j] == 'O':
		return True
	if i > 0 and G[i - 1][j] == 'O':
		return True
	if i < len(G) - 1 and G[i + 1][j] == 'O':
		return True
	if j > 0 and G[i][j - 1] == 'O':
		return True
	if j < len(G[0]) - 1 and G[i][j + 1] == 'O':
		return True
	return False

def getNext(G):
	return list((list(('.' if cleared(G, i, j) else 'O' for j in range(len(G[0])))) for i in range(len(G))))

def solve(N, G):
	if N == 1:
		return G
	if N % 2 == 0:
		return [['O'] * len(G[0])] * len(G)
	N = (N - 1) // 2
	G_ = None
	while N > 0:
		(G__, G_, G) = (G_, G, getNext(G))
		if G == G__:
			N = (N - 1) % 2 + 1
		N -= 1
	return G
(R, C, N) = map(int, input().split())
G = list((list(input()) for _ in range(R)))
S = solve(N, G)
print('\n'.join((''.join(r) for r in S)))


(rows, cols, turns) = [int(s) for s in input().strip().split()]
turn1 = turns == 1
turns %= 4
neighbors = [(1, 0), (-1, 0), (0, 1), (0, -1)]
bombs = set()
for r in range(rows):
	row_str = input().strip()
	for (c, ch) in enumerate(row_str):
		if ch == 'O':
			bombs.add((r, c))
bombs_init = bombs.copy()
board = [['b'] * cols for _ in range(rows)]
for r in range(rows):
	for c in range(cols):
		if (r, c) in bombs:
			board[r][c] = 'e'
			for (dr, dc) in neighbors:
				if 0 <= r + dr < rows and 0 <= c + dc < cols:
					board[r + dr][c + dc] = 'e'
for r in range(rows):
	for c in range(cols):
		if board[r][c] == 'e':
			bombs.add((r, c))
for r in range(rows):
	for c in range(cols):
		if board[r][c] == 'b':
			for (dr, dc) in neighbors:
				try:
					bombs.remove((r + dr, c + dc))
				except KeyError:
					pass
if turns % 2 == 0:
	row = ''.join(['O'] * cols)
	for _ in range(rows):
		print(row)
elif turns == 1 and turn1:
	board = [['.'] * cols for _ in range(rows)]
	for (r, c) in bombs_init:
		board[r][c] = 'O'
	board = [''.join(r) for r in board]
	for r in board:
		print(r)
elif turns == 1:
	board = [['.'] * cols for _ in range(rows)]
	for (r, c) in bombs:
		board[r][c] = 'O'
	board = [''.join(r) for r in board]
	for r in board:
		print(r)
elif turns == 3:
	board = [['O'] * cols for _ in range(rows)]
	for (r, c) in bombs:
		board[r][c] = '.'
		for (dr, dc) in neighbors:
			if 0 <= r + dr < rows and 0 <= c + dc < cols:
				board[r + dr][c + dc] = '.'
	board = [''.join(r) for r in board]
	for r in board:
		print(r)


def cizx(a):
	for i in a:
		for j in i:
			print(j, end='')
		print()

def ciz2(a):
	t = []
	for i in a:
		t_x = []
		for j in i:
			if j == 0:
				t_x.append('.')
			else:
				t_x.append('O')
		t.append(t_x)
	return t

def patlat(a, i, j):
	n = len(a)
	m = len(a[0])
	s = len(i)
	for x in range(s):
		if i[x] >= 0 and i[x] < n and (j[x] >= 0) and (j[x] < m):
			a[i[x]][j[x]] = 0

def azalt(a):
	n = len(a)
	m = len(a[0])
	i_a = []
	j_a = []
	for i in range(n):
		for j in range(m):
			if a[i][j] == 0:
				continue
			elif a[i][j] == 1:
				i_a.append(i)
				j_a.append(j - 1)
				i_a.append(i)
				j_a.append(j + 1)
				i_a.append(i - 1)
				j_a.append(j)
				i_a.append(i + 1)
				j_a.append(j)
				a[i][j] -= 1
			else:
				a[i][j] -= 1
	patlat(a, i_a, j_a)
	return a

def koy(a):
	n = len(a)
	m = len(a[0])
	for i in range(n):
		for j in range(m):
			if a[i][j] == 0:
				a[i][j] = 3
(r, c, n) = input().strip().split()
(r, c, n) = [int(r), int(c), int(n)]
a = []
for i in range(r):
	t_a = []
	t_r = input().strip()
	for j in t_r:
		if j == '.':
			t_a.append(0)
		else:
			t_a.append(3)
	a.append(t_a)
t = []
for i in range(8):
	if i == 0:
		azalt(a)
		t.append(ciz2(a))
	else:
		azalt(a)
		if i % 2 == 1:
			koy(a)
		t.append(ciz2(a))
if n < 5:
	mm = n - 1
elif n % 4 == 0:
	mm = 7
else:
	mm = 3 + n % 4
cizx(t[mm])


import copy
t = input().split()
(r, c, n) = (int(t[0]), int(t[1]), int(t[2]))
bomb_grid = []
bombs = [['O' for x in range(c)] for y in range(r)]
for i in range(r):
	bomb_grid.append([c for c in input()])

def plant(grid):
	for i in range(r):
		for j in range(c):
			if grid[i][j] == 'O':
				grid[i][j] = 'x'
			else:
				grid[i][j] = 'O'
	return grid

def explode(grid):
	grid_c = copy.deepcopy(grid)
	for i in range(r):
		for j in range(c):
			if grid[i][j] == 'x':
				grid_c[i][j] = '.'
				if i < r - 1:
					grid_c[i + 1][j] = '.'
				if i > 0:
					grid_c[i - 1][j] = '.'
				if j < c - 1:
					grid_c[i][j + 1] = '.'
				if j > 0:
					grid_c[i][j - 1] = '.'
	return grid_c
if n < 4:
	if n == 1:
		for i in bomb_grid:
			print(''.join(i))
	elif n == 2:
		for i in bombs:
			print(''.join(i))
	elif n == 3:
		for i in explode(plant(bomb_grid)):
			print(''.join(i))
else:
	condition = n % 2
	state = (n - 3) / 2
	if condition != 0:
		if state % 2 == 0:
			for i in explode(plant(bomb_grid)):
				print(''.join(i))
		else:
			for i in explode(plant(explode(plant(bomb_grid)))):
				print(''.join(i))
	else:
		for i in bombs:
			print(''.join(i))


(R, C, N) = [int(a) for a in input().strip().split(' ')]
grid = []
qr = []
qc = []
for r in range(R):
	grid.append(list(input()))
for r in range(R):
	for c in range(C):
		if grid[r][c] == 'O':
			qr.append(r)
			qc.append(c)
n = 2
if N % 2 == 0:
	for r in range(R):
		stri = ''
		for c in range(C):
			stri += 'O'
		print(stri)
elif N == 1:
	for r in range(R):
		stri = ''
		for c in range(C):
			stri += grid[r][c]
		print(stri)
elif N > 1:
	while n <= 4 + N % 4:
		if n % 2 == 0:
			for r in range(R):
				for c in range(C):
					if grid[r][c] == '.':
						grid[r][c] = 'n'
					elif grid[r][c] == 'n':
						grid[r][c] = 'O'
		else:
			for q in range(len(qr)):
				tr = qr.pop(0)
				tc = qc.pop(0)
				if tr > 0 and grid[tr - 1][tc] == 'n':
					grid[tr - 1][tc] = '.'
				if tr < R - 1 and grid[tr + 1][tc] == 'n':
					grid[tr + 1][tc] = '.'
				if tc > 0 and grid[tr][tc - 1] == 'n':
					grid[tr][tc - 1] = '.'
				if tc < C - 1 and grid[tr][tc + 1] == 'n':
					grid[tr][tc + 1] = '.'
				grid[tr][tc] = '.'
			for r in range(R):
				for c in range(C):
					if grid[r][c] == 'n':
						grid[r][c] = 'O'
						qr.append(r)
						qc.append(c)
		n += 1
	for r in range(R):
		for c in range(C):
			if grid[r][c] == 'n':
				grid[r][c] = 'O'
	for r in range(R):
		stri = ''
		for c in range(C):
			stri += grid[r][c]
		print(stri)


def fill(g):
	r = len(g)
	c = len(g[0])
	g_new = [[g[i][j] for j in range(c)] for i in range(r)]
	for i in range(r):
		for j in range(c):
			if g[i][j] == 0:
				g_new[i][j] = 2
			else:
				g_new[i][j] -= 1
	return g_new

def boom(g):
	r = len(g)
	c = len(g[0])
	g_new = [[g[i][j] for j in range(c)] for i in range(r)]
	for i in range(r):
		for j in range(c):
			if g[i][j] == 1:
				for i2 in range(max(i - 1, 0), min(i + 2, r)):
					g_new[i2][j] = 0
				for j2 in range(max(j - 1, 0), min(j + 2, c)):
					g_new[i][j2] = 0
	return g_new

def display(g):
	for row in g:
		for x in row:
			if x == 0:
				print('.', end='')
			else:
				print('O', end='')
		print()
(r, c, n) = [int(x) for x in input().strip().split()]
g0 = [[0 for _ in range(c)] for _ in range(r)]
for i in range(r):
	row = input().strip()
	for j in range(c):
		x = row[j]
		if x == 'O':
			g0[i][j] = 2
g1 = boom(g0)
g2 = fill(g1)
g3 = boom(g2)
g4 = fill(g3)
g5 = boom(g4)
if n == 1:
	display(g1)
elif n % 2 == 0:
	display(g2)
elif n % 4 == 1:
	display(g5)
else:
	display(g3)


def index(x, y):
	return x * c + y

def printGrid(s):
	for i in range(r):
		print(''.join(s[i * c:(i + 1) * c]))

def blastBomb(b, x, y):
	if x >= 0 and x < r and (y >= 0) and (y < c):
		b[index(x, y)] = '.'

def blast(a, b):
	for i in range(r):
		for j in range(c):
			if a[index(i, j)] == 'O':
				blastBomb(b, i, j)
				blastBomb(b, i + 1, j)
				blastBomb(b, i - 1, j)
				blastBomb(b, i, j + 1)
				blastBomb(b, i, j - 1)
(r, c, n) = map(int, input().split())
s0 = ''
for _ in range(r):
	s0 += input()
s0 = list(s0)
s2 = ['.' if x == 'O' else 'O' for x in s0]
s3 = ['O'] * len(s0)
blast(s0, s3)
blast(s0, s2)
s5 = ['O'] * len(s0)
blast(s2, s5)
if n < 2:
	printGrid(s0)
elif n % 2 == 0:
	printGrid('O' * len(s0))
elif n % 4 == 3:
	printGrid(s3)
else:
	printGrid(s5)


import sys

def get_m():
	m = []
	(rows, _, t) = map(int, input().split())
	for _ in range(rows):
		s = input()
		m.append(list(map(lambda c: c == 'O', s)))
	return (m, t)

def fill(m):
	rval = []
	for row in m:
		rval.append([True] * len(row))
	return rval

def explode(m):
	rval = fill(m)
	for r in range(len(m)):
		for c in range(len(m[r])):
			if m[r][c]:
				for (i, j) in ((r, c), (r - 1, c), (r + 1, c), (r, c - 1), (r, c + 1)):
					if i >= 0 and i < len(m) and (j >= 0) and (j < len(m[r])):
						rval[i][j] = False
	return rval

def printout(m, file=sys.stdout):
	for row in m:
		v = map(lambda u: u and 'O' or '.', row)
		print(''.join(v), file=file)

def solve(m, t):
	f = fill(m)
	e1 = explode(m)
	e2 = explode(e1)
	if t < 2:
		return m
	t = (t - 2) % 4
	return [f, e1, f, e2][t]
(m, t) = get_m()
f = fill(m)
e = explode(m)
print('t=%d' % t, file=sys.stderr)
printout(solve(m, t))
for i in range(7):
	print(i, file=sys.stderr)
	printout(m, file=sys.stderr)
	m = explode(m)


(r, c, n) = map(int, input().split())
grid = [[0 if x == '.' else 2 for x in list(input())] for _ in range(r)]

def print_grid():
	for i in range(r):
		print(''.join(['O' if x > 0 else '.' for x in grid[i]]))

def forward_time(seconds=1):
	if seconds < 1:
		return
	explosions = []
	for i in range(r):
		for j in range(c):
			if grid[i][j] == 0:
				grid[i][j] = 3
			else:
				grid[i][j] -= 1
			if grid[i][j] == 0:
				explosions.append((i, j))
	for pos in explosions:
		explode(pos[0], pos[1])
	if seconds > 1:
		forward_time(seconds - 1)

def explode(i, j):
	if i - 1 >= 0:
		grid[i - 1][j] = 0
	if i + 1 < r:
		grid[i + 1][j] = 0
	if j - 1 >= 0:
		grid[i][j - 1] = 0
	if j + 1 < c:
		grid[i][j + 1] = 0
if n > 1:
	n = (n - 2) % 4 + 2
	forward_time(n - 1)
print_grid()


from sys import exit
from copy import deepcopy
(r, c, n) = [int(x) for x in input().split()]
a = []
for b in range(r):
	a.append([x for x in input()])
if n == 0 or n == 1:
	for b in a:
		print(''.join(b))
elif n % 2 == 0:
	for b in a:
		print(''.join(['O'] * c))
elif (n - 3) % 4 == 0:
	d = deepcopy(a)
	for b in range(len(a)):
		for c in range(len(a[b])):
			if a[b][c] == 'O':
				for e in [[1, 0], [-1, 0], [0, 1], [0, -1]]:
					if b + e[0] > -1 and b + e[0] < len(a) and (c + e[1] > -1) and (c + e[1] < len(a[b])):
						d[b + e[0]][c + e[1]] = 'O'
	for b in range(len(a)):
		for c in range(len(a[b])):
			if d[b][c] == 'O':
				d[b][c] = '.'
			else:
				d[b][c] = 'O'
	for b in d:
		print(''.join(b))
elif (n - 1) % 4 == 0:
	d = deepcopy(a)
	for b in range(len(a)):
		for c in range(len(a[b])):
			if a[b][c] == 'O':
				for e in [[1, 0], [-1, 0], [0, 1], [0, -1]]:
					if b + e[0] > -1 and b + e[0] < len(a) and (c + e[1] > -1) and (c + e[1] < len(a[b])):
						d[b + e[0]][c + e[1]] = 'O'
	for b in range(len(a)):
		for c in range(len(a[b])):
			if d[b][c] == 'O':
				d[b][c] = '.'
			else:
				d[b][c] = 'O'
	a = deepcopy(d)
	for b in range(len(a)):
		for c in range(len(a[b])):
			if d[b][c] == 'O':
				for e in [[1, 0], [-1, 0], [0, 1], [0, -1]]:
					if b + e[0] > -1 and b + e[0] < len(a) and (c + e[1] > -1) and (c + e[1] < len(a[b])):
						a[b + e[0]][c + e[1]] = 'O'
	for b in range(len(a)):
		for c in range(len(a[b])):
			if a[b][c] == 'O':
				a[b][c] = '.'
			else:
				a[b][c] = 'O'
	for b in a:
		print(''.join(b))


import copy

def _print(grid):
	for s in grid:
		print(s)

def blow(grid):
	cc = copy.deepcopy(grid)
	c = len(grid[0])
	for i in range(len(grid)):
		s = ''
		for j in range(c):
			if cc[i][j] == 'O' or (i > 0 and cc[i - 1][j] == 'O' or (i + 1 < len(grid) and cc[i + 1][j] == 'O') or (j > 0 and cc[i][j - 1] == 'O') or (j + 1 < c and cc[i][j + 1] == 'O')):
				s += 'O'
			else:
				s += '.'
		grid[i] = s
	return grid

def full(grid):
	c = len(grid[0])
	for i in range(len(grid)):
		grid[i] = 'O' * c
	return grid

def compliment(grid):
	c = len(grid[0])
	for i in range(len(grid)):
		s = ''
		for j in range(c):
			if grid[i][j] == '.':
				s += 'O'
			else:
				s += '.'
		grid[i] = s
	return grid
(r, c, n) = map(int, input().split())
initialgrid = []
for _ in range(r):
	initialgrid.append(input())
if n == 0 or n == 1:
	_print(initialgrid)
elif n & 1 == 0:
	_print(full(initialgrid))
elif n % 4 == 3:
	_print(compliment(blow(initialgrid)))
else:
	_print(compliment(blow(compliment(blow(initialgrid)))))


(r, c, n) = (int(num) for num in input().split())
grid = [input() for _ in range(r)]
flag = n % 4
if n == 0 or n == 1:
	for row in grid:
		print(row)
elif flag == 0 or flag == 2:
	row = 'O' * c
	for i in range(r):
		print(row)
else:
	grid = [''.join((['O', '.'][grid[max(i - 1, 0)][j] == 'O' or grid[min(i + 1, r - 1)][j] == 'O' or grid[i][max(j - 1, 0)] == 'O' or (grid[i][min(j + 1, c - 1)] == 'O') or (grid[i][j] == 'O')] for j in range(c))) for i in range(r)]
	if flag == 3:
		for row in grid:
			print(row)
	elif flag == 1:
		for i in range(r):
			print(''.join((['O', '.'][grid[max(i - 1, 0)][j] == 'O' or grid[min(i + 1, r - 1)][j] == 'O' or grid[i][max(j - 1, 0)] == 'O' or (grid[i][min(j + 1, c - 1)] == 'O') or (grid[i][j] == 'O')] for j in range(c))))


class Grid(object):

	def __init__(self, rows, cols):
		self.rows = rows
		self.cols = cols
		self.curr_row = 0
		self.curr_time = 0
		self.grid = []
		for i in range(rows):
			self.grid.append([])
			for j in range(cols):
				self.grid[i].append(Cell(i, j))
		for i in range(rows):
			for j in range(cols):
				self.add_nbors(i, j)

	def add_nbors(self, row, col):
		cell = self.grid[row][col]
		if row - 1 >= 0:
			nbor = self.grid[row - 1][col]
			cell.nbors.add(nbor)
		if row + 1 < self.rows:
			nbor = self.grid[row + 1][col]
			cell.nbors.add(nbor)
		if col - 1 >= 0:
			nbor = self.grid[row][col - 1]
			cell.nbors.add(nbor)
		if col + 1 < self.cols:
			nbor = self.grid[row][col + 1]
			cell.nbors.add(nbor)

	def add_row(self, row_str):
		for i in range(len(row_str)):
			if row_str[i] == 'O':
				self.grid[self.curr_row][i].det_time = self.curr_time + 3
		self.curr_row += 1

	def adv_time(self):
		self.curr_time += 1
		if self.curr_time == 1:
			return
		which = self.curr_time % 2
		if which == 0:
			self.replant()
		elif which == 1:
			self.detonate()

	def detonate(self):
		for i in range(self.rows):
			for j in range(self.cols):
				cell = self.grid[i][j]
				if cell.det_time == self.curr_time:
					for c in cell.nbors:
						if not c.det_time == self.curr_time:
							c.det_time = None
					cell.det_time = None

	def replant(self):
		for i in range(self.rows):
			for j in range(self.cols):
				cell = self.grid[i][j]
				if cell.det_time is None:
					cell.det_time = self.curr_time + 3

	def __repr__(self):
		grid = []
		for i in range(self.rows):
			row = []
			for j in range(self.cols):
				if self.grid[i][j].det_time is None:
					row.append('.')
				else:
					row.append('O')
			grid.append(''.join(row))
		return '\n'.join(grid)

class Cell(object):

	def __init__(self, row, col):
		self.row = row
		self.col = col
		self.nbors = set()
		self.det_time = None

	def __repr__(self):
		return str((self.row, self.col, self.det_time))

def go():
	(row, col, sec) = map(int, input().split())
	g = Grid(row, col)
	for i in range(row):
		g.add_row(input())
	if sec > 8:
		sec = (sec - 1) % 4 + 5
	while sec > g.curr_time:
		g.adv_time()
	print(g)

def test():
	grid = Grid(6, 7)
	grid.add_row('.......')
	grid.add_row('...O...')
	grid.add_row('....O..')
	grid.add_row('.......')
	grid.add_row('OO.....')
	grid.add_row('OO.....')
	for i in range(20):
		grid.adv_time()
		print(grid)
		print()
	return grid

def test2():
	grid = Grid(20, 30)
	grid.add_row('...........O......O.O...O.....')
	grid.add_row('............O...O.....O.....O.')
	grid.add_row('OO.........O...OOO.O...O.O...O')
	grid.add_row('.............O.OO....O...O....')
	grid.add_row('..O.............OO.....O......')
	grid.add_row('......................O......O')
	grid.add_row('.........O..OO...OO...O.......')
	grid.add_row('.O.........O.......O.O.OO.....')
	grid.add_row('..............O.......OO......')
	grid.add_row('OOO.....OO.........O........O.')
	grid.add_row('O..O.....OO...OOOO.O..........')
	grid.add_row('...O...........O..........OOO.')
	grid.add_row('.....O.OO......O...O..........')
	grid.add_row('.....O...............O....O.O.')
	grid.add_row('.......O.............O......OO')
	grid.add_row('.O...OO....O.O.....OO.....OO..')
	grid.add_row('O.O..OO.O.......O....O...O.O..')
	grid.add_row('....................O.OO..O...')
	grid.add_row('........OO....O......O........')
	grid.add_row('..........O.O............O....')
	return grid
import random

def create_rand(rows, cols):
	g = Grid(rows, cols)
	for i in range(rows):
		row = []
		for i in range(cols):
			num = random.randint(1, 8)
			if num < 3:
				row.append('O')
			else:
				row.append('.')
		g.add_row(''.join(row))
	return g
go()


def bombGrid(grid, R, C):
	newGrid = [['O'] * C for _ in range(R)]
	for r in range(R):
		for c in range(C):
			if grid[r][c] == '.':
				continue
			newGrid[r][c] = '.'
			if r > 0:
				newGrid[r - 1][c] = '.'
			if r < R - 1:
				newGrid[r + 1][c] = '.'
			if c > 0:
				newGrid[r][c - 1] = '.'
			if c < C - 1:
				newGrid[r][c + 1] = '.'
	return newGrid
(R, C, N) = map(int, input().strip().split())
grid = [input().strip() for _ in range(R)]
if N == 1:
	for row in grid:
		print(row)
elif N % 2 == 0:
	for i in range(R):
		print('O' * C)
else:
	newGrid = bombGrid(grid, R, C)
	if N % 4 == 1:
		newGrid = bombGrid(newGrid, R, C)
	for row in newGrid:
		print(''.join(row))


import copy

def draw_grid(grid):
	for row in grid:
		print(row)

def bum(grid):
	new_grid = [list('O' * cols) for _ in range(rows)]
	for (i, row) in enumerate(grid):
		for (j, cell) in enumerate(row):
			if cell == 'O':
				new_grid[i][j] = '.'
				if i >= 1:
					new_grid[i - 1][j] = '.'
				if j >= 1:
					new_grid[i][j - 1] = '.'
				if i < len(grid) - 1:
					new_grid[i + 1][j] = '.'
				if j < len(row) - 1:
					new_grid[i][j + 1] = '.'
	return [''.join(r) for r in new_grid]
(rows, cols, n) = map(int, input().strip().split(' '))
first_grid = [input().strip() for r in range(rows)]
third_grid = bum(first_grid)
fifth_grid = bum(third_grid)
full_grid = ['O' * cols for _ in range(rows)]
if n == 1:
	draw_grid(first_grid)
elif n % 2 == 0:
	draw_grid(full_grid)
elif n % 4 == 1:
	draw_grid(fifth_grid)
else:
	draw_grid(third_grid)


import sys
(R, C, N) = [int(i) for i in input().strip().split(' ')]
grid = []
for i in range(R):
	grid.append(input().strip())

def isBombed(grid, x, y):
	return grid[x][y] == 'O' or (x < len(grid) - 1 and grid[x + 1][y] == 'O') or (x > 0 and grid[x - 1][y] == 'O') or (y < len(grid[x]) - 1 and grid[x][y + 1] == 'O') or (y > 0 and grid[x][y - 1] == 'O')
fullgrid = [['O' for j in range(len(grid[i]))] for i in range(len(grid))]
state3sec = [['.' if isBombed(grid, x, y) else 'O' for y in range(len(grid[x]))] for x in range(len(grid))]
state5sec = [['.' if isBombed(state3sec, x, y) else 'O' for y in range(len(grid[x]))] for x in range(len(grid))]
states = {-1: grid, 1: fullgrid, 2: state3sec, 3: fullgrid, 0: state5sec}
state = states[-1] if N == 1 else states[(N - 1) % 4]
print('\n'.join((''.join((j for j in i)) for i in state)))


(r, c, n) = [int(x) for x in input().split()]
grid = []

def copyGrid(grid):
	grid_copied = [[0 for x in range(c)] for y in range(r)]
	for row in range(r):
		for col in range(c):
			grid_copied[row][col] = grid[row][col]
	return grid_copied

def compareGrids(a, b):
	if not a or not b:
		return False
	for row in range(r):
		for col in range(c):
			if a[row][col] != b[row][col]:
				return False
	return True
if n % 2 == 0:
	for row in range(r):
		print(''.join(['O' for x in range(c)]))
else:
	for i in range(r):
		row = []
		for letter in input():
			if letter == 'O':
				row.append(1)
			else:
				row.append(0)
		grid.append(row)
	grid_last_last = None
	grid_last = None
	for i in range(int(n / 2)):
		for row in range(r):
			for col in range(c):
				if grid[row][col] == 1:
					continue
				bombed = False
				if row - 1 >= 0:
					if grid[row - 1][col] == 1:
						bombed = True
				if row + 1 < r:
					if grid[row + 1][col] == 1:
						bombed = True
				if col - 1 >= 0:
					if grid[row][col - 1] == 1:
						bombed = True
				if col + 1 < c:
					if grid[row][col + 1] == 1:
						bombed = True
				if bombed:
					grid[row][col] = 0
				else:
					grid[row][col] = -1
		for row in range(r):
			for col in range(c):
				if grid[row][col] == -1:
					grid[row][col] = 1
				elif grid[row][col] == 1:
					grid[row][col] = 0
		if compareGrids(grid_last_last, grid):
			if (int(n / 2) - 1 - i) % 2 == 1:
				grid = grid_last
			break
		grid_last_last = grid_last
		grid_last = copyGrid(grid)
	for row in range(r):
		outStr = ''
		for col in range(c):
			if grid[row][col] == 1:
				outStr += 'O'
			else:
				outStr += '.'
		print(outStr)


import sys
import copy

def show(world):
	for row in world:
		print(''.join(['O' if x > 0 else '.' for x in row]))

def debug_show(world):
	if not debug_enabled:
		return None
	for row in world:
		print(''.join([str(x) if x > 0 else '.' for x in row]))
	print('-' * 50)

def debug_msg(msg):
	if debug_enabled:
		print(msg)

def plant_bombs(world):
	for y in range(height):
		for x in range(width):
			if world[y][x] == 0:
				world[y][x] = 3

def increment_time(world):
	new_world = copy.deepcopy(world)
	for y in range(height):
		for x in range(width):
			if world[y][x] > 0:
				new_world[y][x] = world[y][x] - 1
	for y in range(height):
		for x in range(width):
			if world[y][x] == 1:
				if y - 1 >= 0:
					new_world[y - 1][x] = 0
				if y + 1 < height:
					new_world[y + 1][x] = 0
				if x - 1 >= 0:
					new_world[y][x - 1] = 0
				if x + 1 < width:
					new_world[y][x + 1] = 0
	return new_world
(height, width, target_iterations) = [int(x) for x in input().strip().split(' ')]
iterations = 0
if target_iterations > 4:
	target_iterations = target_iterations % 4 + 4
debug_enabled = False
world = []
for y in range(height):
	world.append([0 if x == '.' else 3 for x in sys.stdin.readline().strip()])
debug_msg('[%d/%d]: random bombs...' % (iterations, target_iterations))
debug_show(world)
world = increment_time(world)
iterations += 1
debug_msg('[%d/%d]: wait one second...' % (iterations, target_iterations))
debug_show(world)
while target_iterations > iterations:
	world = increment_time(world)
	plant_bombs(world)
	iterations += 1
	debug_msg('[%d/%d]: plant and wait...' % (iterations, target_iterations))
	debug_show(world)
	if not target_iterations > iterations:
		break
	world = increment_time(world)
	iterations += 1
	debug_msg('[%d/%d]: just wait...' % (iterations, target_iterations))
	debug_show(world)
show(world)


q = input().split()
r = int(q[0])
c = int(q[1])
n = int(q[2])
x = ''
bf = []
bfn = []
bfl = []
for i in range(c):
	x = x + 'O'
for i in range(r):
	bf.append(input())
	bfn.append(list(x))
	bfl.append(list(x))
for i in range(r):
	for j in range(c):
		if bf[i][j] == 'O':
			bfn[i][j] = '.'
			if i != 0:
				bfn[i - 1][j] = '.'
			if i != r - 1:
				bfn[i + 1][j] = '.'
			if j != 0:
				bfn[i][j - 1] = '.'
			if j != c - 1:
				bfn[i][j + 1] = '.'
for i in range(r):
	for j in range(c):
		if bfn[i][j] == 'O':
			bfl[i][j] = '.'
			if i != 0:
				bfl[i - 1][j] = '.'
			if i != r - 1:
				bfl[i + 1][j] = '.'
			if j != 0:
				bfl[i][j - 1] = '.'
			if j != c - 1:
				bfl[i][j + 1] = '.'
if n == 0 or n == 1:
	for i in range(r):
		print(bf[i])
elif n % 2 == 0:
	for i in range(r):
		print(x)
elif n % 4 == 3:
	for i in range(r):
		for j in range(c):
			print(bfn[i][j], sep='', end='')
		print()
elif n % 4 == 1:
	for i in range(r):
		for j in range(c):
			print(bfl[i][j], sep='', end='')
		print()


def main():
	temp = input().strip().split(' ')
	(row, col, n) = [int(x) for x in temp]
	arrs = []
	arrb = [[0 for _ in range(col)] for _ in range(row)]
	if n > 5:
		if n & 1 == 0:
			n = 2
		elif (n - 1) % 4 == 0:
			n = 5
		else:
			n = 3
	for i in range(row):
		t = input().strip()
		arrs.append(t)
		for (j, c) in enumerate(t):
			if c == 'O':
				arrb[i][j] = 3
			else:
				arrb[i][j] = 5
	t = 1
	while t <= n:
		for i in range(row):
			for j in range(col):
				arrb[i][j] -= 1
				if arrb[i][j] == 0:
					arrb[i][j] = 4
					if i > 0:
						arrb[i - 1][j] = 4
					if i < row - 1:
						if arrb[i + 1][j] != 1:
							arrb[i + 1][j] = 8
					if j > 0:
						arrb[i][j - 1] = 4
					if j < col - 1:
						if arrb[i][j + 1] != 1:
							arrb[i][j + 1] = 8
				elif arrb[i][j] == 7:
					arrb[i][j] = 4
		if t == n:
			if t & 1 == 0:
				for _ in range(row):
					print('O' * col)
			else:
				for i in range(row):
					for j in range(col):
						if arrb[i][j] <= 3:
							print('O', end='')
						else:
							print('.', end='')
					print('\n', end='')
		t += 1
main()


(a, b, c) = [int(j) for j in input().strip().split(' ')]
k = []
for i in range(a):
	l = [z for z in input().strip().split()]
	s = l[0]
	v = [x for x in s]
	k.append(v)
p = []
for i in range(a):
	for j in range(b):
		if k[i][j] == 'O':
			p.append((i, j))
for i in range(0, len(p)):
	p.append((p[i][0] + 1, p[i][1]))
	p.append((p[i][0] - 1, p[i][1]))
	p.append((p[i][0], p[i][1] + 1))
	p.append((p[i][0], p[i][1] - 1))
k1 = []
k2 = []
w = []
for i in range(a):
	s = b * 'O'
	v = [x for x in s]
	k1.append(v)
for i in range(a):
	s = b * 'O'
	v = [x for x in s]
	k2.append(v)
for j in p:
	if j[0] >= 0 and j[0] < a and (j[1] >= 0) and (j[1] < b):
		k1[j[0]][j[1]] = '.'
if c == 1:
	for i in range(a):
		for j in range(b):
			print(k[i][j], end='')
		print()
if c == 2:
	for i in range(a):
		for j in range(b):
			print(k2[i][j], end='')
		print()
if c == 3:
	for i in range(a):
		for j in range(b):
			print(k1[i][j], end='')
		print()
k3 = []
for i in range(a):
	s = k1[i]
	v = [x for x in k1[i]]
	k3.append(v)
p = []
for i in range(a):
	for j in range(b):
		if k3[i][j] == 'O':
			p.append((i, j))
for i in range(0, len(p)):
	p.append((p[i][0] + 1, p[i][1]))
	p.append((p[i][0] - 1, p[i][1]))
	p.append((p[i][0], p[i][1] + 1))
	p.append((p[i][0], p[i][1] - 1))
k4 = []
for i in range(a):
	s = b * 'O'
	v = [x for x in s]
	k4.append(v)
for j in p:
	if j[0] >= 0 and j[0] < a and (j[1] >= 0) and (j[1] < b):
		k4[j[0]][j[1]] = '.'
k5 = []
for i in range(a):
	s = k4[i]
	v = [x for x in k4[i]]
	k5.append(v)
p = []
for i in range(a):
	for j in range(b):
		if k5[i][j] == 'O':
			p.append((i, j))
for i in range(0, len(p)):
	p.append((p[i][0] + 1, p[i][1]))
	p.append((p[i][0] - 1, p[i][1]))
	p.append((p[i][0], p[i][1] + 1))
	p.append((p[i][0], p[i][1] - 1))
k6 = []
for i in range(a):
	s = b * 'O'
	v = [x for x in s]
	k6.append(v)
for j in p:
	if j[0] >= 0 and j[0] < a and (j[1] >= 0) and (j[1] < b):
		k6[j[0]][j[1]] = '.'
if c > 3 and c % 2 == 0:
	for i in range(a):
		for j in range(b):
			print(k2[i][j], end='')
		print()
elif c > 3 and (c - 1) % 4 == 0:
	for i in range(a):
		for j in range(b):
			print(k4[i][j], end='')
		print()
elif c > 3 and (c - 1) % 4 != 0:
	for i in range(a):
		for j in range(b):
			print(k6[i][j], end='')
		print()


def display(bombs):
	for j in range(r):
		for k in range(c):
			if (j, k) in bombs[0]:
				grid[j][k] = 'O'
			if (j, k) in bombs[1]:
				grid[j][k] = 'O'
			if (j, k) in bombs[2]:
				grid[j][k] = '.'
	[print(''.join(i)) for i in grid]
(r, c, n) = [int(arr_temp) for arr_temp in input().strip().split(' ')]
grid = [list(input().strip()) for i in range(r)]
output = []
bombs = ['B', 'O']
bombType = 0
flip = 0
if n > 4:
	n = (n - 2) % 4 + 2
bombs = [set(), set(), set()]
for j in range(r):
	for k in range(c):
		if grid[j][k] != '.':
			bombs[1 - bombType].add(tuple((j, k)))
		else:
			bombs[2].add(tuple((j, k)))
for i in range(1, n):
	if flip == 0:
		flip = 1 - flip
		bombs[bombType] = bombs[2]
		bombs[2] = set()
		bombType = 1 - bombType
	else:
		flip = 1 - flip
		bombs[2] = bombs[bombType]
		temp = set()
		for (j, k) in bombs[bombType]:
			bombs[1 - bombType].discard((max(min(j + 1, r - 1), 0), max(min(k, c - 1), 0)))
			temp.add((max(min(j + 1, r - 1), 0), max(min(k, c - 1), 0)))
			bombs[1 - bombType].discard((max(min(j - 1, r - 1), 0), max(min(k, c - 1), 0)))
			temp.add((max(min(j - 1, r - 1), 0), max(min(k, c - 1), 0)))
			bombs[1 - bombType].discard((max(min(j, r - 1), 0), max(min(k + 1, c - 1), 0)))
			temp.add((max(min(j, r - 1), 0), max(min(k + 1, c - 1), 0)))
			bombs[1 - bombType].discard((max(min(j, r - 1), 0), max(min(k - 1, c - 1), 0)))
			temp.add((max(min(j, r - 1), 0), max(min(k - 1, c - 1), 0)))
		bombs[2].update(temp)
		bombs[bombType] = set()
display(bombs)


(R, C, N) = map(int, input().split())
grid = []
for _ in range(R):
	grid.append(input())
if N == 1:
	for line in grid:
		print(line)
elif N % 2 == 0:
	for _ in range(R):
		print('O' * C)
else:
	num = 1 if N // 2 % 2 == 1 else 2
	for _ in range(num):
		g = []
		for r in range(R):
			row = ''
			for c in range(C):
				if grid[r][c] == 'O':
					row += '.'
				else:
					blasted = False
					if r - 1 >= 0 and grid[r - 1][c] == 'O':
						blasted = True
					if r + 1 < R and grid[r + 1][c] == 'O':
						blasted = True
					if c - 1 >= 0 and grid[r][c - 1] == 'O':
						blasted = True
					if c + 1 < C and grid[r][c + 1] == 'O':
						blasted = True
					if blasted:
						row += '.'
					else:
						row += 'O'
			g.append(row)
		grid = g
	for line in grid:
		print(line)


def iter(m):
	m2 = [['O' for x in range(c)] for y in range(r)]
	for x in range(r):
		for y in range(c):
			if m[x][y] == 'O':
				m2[x][y] = '.'
				if x > 0:
					m2[x - 1][y] = '.'
				if x < r - 1:
					m2[x + 1][y] = '.'
				if y < c - 1:
					m2[x][y + 1] = '.'
				if y > 0:
					m2[x][y - 1] = '.'
	return m2
(r, c, n) = map(int, input().strip().split(' '))
m = []
for i in range(r):
	m += [list(input().strip())]
if n <= 1:
	print('\n'.join([''.join(x) for x in m]))
elif n % 2 == 0:
	print('\n'.join(['O' * c] * r))
else:
	m = iter(m)
	if n // 2 % 2 == 0:
		m = iter(m)
	print('\n'.join([''.join(x) for x in m]))


import sys
(r, c, k) = input().strip().split(' ')
(r, c, k) = [int(r), int(c), int(k)]
a = []
bomb_list = {}
bom_item = {}
pattern = {}
for a0 in range(0, r):
	row = list(input().strip())
	a.append(row)
bomb_list[3] = bom_item
pattern = {}
result = ''
for row in a:
	result += ''.join(row) + '\n'
	pattern[1] = result
result = ''
if k % 2 == 0:
	for row in range(r):
		result += 'O' * c + '\n'
	print(result)
else:
	for i in range(2, 10):
		bom_item = {}
		if i % 2 == 0:
			for row in range(0, r):
				item_row = []
				for col in range(0, len(a[row])):
					if ord(a[row][col]) == 79:
						item_row.append(col)
					else:
						a[row][col] = chr(79)
				if len(item_row) > 0:
					bom_item[row] = item_row
			bomb_list[i + 1] = bom_item
		elif i in bomb_list.keys():
			item_data = []
			for row in bomb_list[i].keys():
				for col in bomb_list[i][row]:
					a[row][col] = '.'
					if row + 1 < r:
						a[row + 1][col] = '.'
					if row - 1 >= 0:
						a[row - 1][col] = '.'
					if col + 1 < c:
						a[row][col + 1] = '.'
					if col - 1 >= 0:
						a[row][col - 1] = '.'
			result = ''
			for row in a:
				result += ''.join(row) + '\n'
			pattern[i] = result
	if k > 9:
		if (k - 7) % 4 == 0:
			row = pattern[7]
		elif (k - 9) % 4 == 0:
			row = pattern[9]
		print(row)
	else:
		print(pattern[k])


def cpy2D(arr):
	arr2 = []
	for r in arr:
		r2 = []
		for n in r:
			r2.append(n)
		arr2.append(r2)
	return arr2

def isBomb(i, j, grid):
	return grid[i][j] == 'O'

def isAroundBomb(i, j, grid):
	if i - 1 >= 0:
		if grid[i - 1][j] == 'O':
			return True
	if i + 1 < len(grid):
		if grid[i + 1][j] == 'O':
			return True
	if j - 1 >= 0:
		if grid[i][j - 1] == 'O':
			return True
	if j + 1 < len(grid[0]):
		if grid[i][j + 1] == 'O':
			return True

def detonate(grid):
	grid2 = cpy2D(grid)
	for i in range(len(grid)):
		for j in range(len(grid[0])):
			if isBomb(i, j, grid) or isAroundBomb(i, j, grid):
				grid2[i][j] = '.'
			else:
				grid2[i][j] = 'O'
	return grid2

def isSameArray(arr, arr2):
	for i in range(len(arr)):
		for j in range(len(arr[0])):
			if arr[i][j] != arr2[i][j]:
				return False
	return True

def getFullBombGrid(r, c):
	grid = []
	for i in range(r):
		col = []
		for j in range(c):
			col.append('O')
		grid.append(col)
	return grid

def printArray(arr):
	for r in arr:
		for n in r:
			print(n, end='')
		print('')
arr = input().split(' ')
r = int(arr[0])
c = int(arr[1])
n = int(arr[2])
grid1 = []
for i in range(r):
	row = list(input())
	grid1.append(row)
if n == 1:
	printArray(grid1)
elif n % 2 == 0:
	printArray(getFullBombGrid(r, c))
else:
	grid2 = detonate(grid1)
	grid3 = detonate(grid2)
	if isSameArray(grid1, grid3):
		if int(n / 2) % 2 == 0:
			printArray(grid1)
		else:
			printArray(grid2)
	elif int(n / 2) % 2 == 0:
		printArray(grid3)
	else:
		printArray(grid2)


def printgame(game):
	for i in range(len(game)):
		print(''.join(game[i]))
(R, C, N) = [int(arr_temp) for arr_temp in input().strip().split(' ')]
fullgame = [['O'] * C] * R
game = []
for i in range(R):
	game.append(list(input()))
if N == 1:
	printgame(game)
elif N % 2 == 0:
	printgame(fullgame)
else:
	if N % 4 == 3:
		N = 1
	else:
		N = 2
	for i in range(N):
		fullgame = [['O'] * C for _ in range(R)]
		for r in range(R):
			for c in range(C):
				if game[r][c] == 'O':
					fullgame[r][c] = '.'
					fullgame[max(0, r - 1)][c] = '.'
					fullgame[min(R - 1, r + 1)][c] = '.'
					fullgame[r][max(0, c - 1)] = '.'
					fullgame[r][min(C - 1, c + 1)] = '.'
		game = fullgame
	printgame(game)


(r, c, n) = input().strip().split(' ')
(r, c, n) = [int(r), int(c), int(n)]
matrix = []
for i in range(r):
	matrix.append(list(input()))
if n % 4 == 1:
	if n == 1:
		for i in range(r):
			print(''.join(matrix[i]))
	else:
		new = []
		for _ in range(r):
			new.append(list('O' * c))
		for i in range(r):
			for j in range(c):
				if matrix[i][j] == 'O':
					new[i][j] = '.'
					if i != 0:
						new[i - 1][j] = '.'
					if i != r - 1:
						new[i + 1][j] = '.'
					if j != 0:
						new[i][j - 1] = '.'
					if j != c - 1:
						new[i][j + 1] = '.'
				elif new[i][j] != '.':
					new[i][j] = 'O'
		new1 = []
		for _ in range(r):
			new1.append(list('O' * c))
		for i in range(r):
			for j in range(c):
				if new[i][j] == 'O':
					new1[i][j] = '.'
					if i != 0:
						new1[i - 1][j] = '.'
					if i != r - 1:
						new1[i + 1][j] = '.'
					if j != 0:
						new1[i][j - 1] = '.'
					if j != c - 1:
						new1[i][j + 1] = '.'
				elif new[i][j] != '.':
					new1[i][j] = 'O'
		for i in range(r):
			print(''.join(new1[i]))
if n % 2 == 0:
	for _ in range(r):
		print('O' * c)
if n % 4 == 3:
	new = []
	for _ in range(r):
		new.append(list('O' * c))
	for i in range(r):
		for j in range(c):
			if matrix[i][j] == 'O':
				new[i][j] = '.'
				if i != 0:
					new[i - 1][j] = '.'
				if i != r - 1:
					new[i + 1][j] = '.'
				if j != 0:
					new[i][j - 1] = '.'
				if j != c - 1:
					new[i][j + 1] = '.'
			elif new[i][j] != '.':
				new[i][j] = 'O'
	for i in range(r):
		print(''.join(new[i]))


(empty, bomb) = ('.', 'O')

def nextGrid(initialGrid):
	(r, c) = (len(initialGrid), len(initialGrid[0]))
	newGrid = [[bomb for j in range(c)] for i in range(r)]
	for i in range(r):
		for j in range(c):
			if initialGrid[i][j] == bomb:
				for neighbour in [(-1, 0), (1, 0), (0, -1), (0, 1), (0, 0)]:
					(x, y) = neighbour
					x += i
					y += j
					if x < 0 or y < 0 or x >= r or (y >= c):
						continue
					newGrid[x][y] = empty
	return newGrid

def printGrid(grid):
	(r, c) = (len(grid), len(grid[0]))
	for i in range(r):
		for j in range(c):
			print(grid[i][j], end='')
		print()
import sys
(r, c, n) = [int(el) for el in input().split()]
grid = [0 for i in range(r)]
for i in range(r):
	grid[i] = [el for el in input()]
if n == 1:
	printGrid(grid)
	sys.exit()
if n % 2 == 0:
	for i in range(r):
		print(bomb * c)
	sys.exit()
n //= 2
grid1 = nextGrid(grid)
grid2 = nextGrid(grid1)
if n % 2 == 0:
	printGrid(grid2)
else:
	printGrid(grid1)


(R, C, N) = map(int, input().strip().split())
if N >= 5:
	N = N % 4 + 4
lst = []
for row in range(R):
	line = input().strip()
	linelst = []
	for c in line:
		if c == '.':
			linelst.append(0)
		elif c == 'O':
			linelst.append(2)
	lst.append(linelst)
t0 = 1

def viewmap(bombmap):
	for row in lst:
		strrow = ''.join(map(str, row)).replace('0', '.').replace('1', 'O').replace('2', 'O').replace('3', 'O')
		print(strrow)

def fillbomb(lst):
	for i in range(len(lst)):
		for j in range(len(lst[0])):
			if lst[i][j] == 0:
				lst[i][j] = 1
			if lst[i][j] == 2:
				lst[i][j] = 3

def getneighbor(row, col):
	nei = set()
	if row != 0:
		nei.add((row - 1, col))
	if row != R - 1:
		nei.add((row + 1, col))
	if col != 0:
		nei.add((row, col - 1))
	if col != C - 1:
		nei.add((row, col + 1))
	return nei

def detonate(lst):
	toclear = set()
	for i in range(len(lst)):
		for j in range(len(lst[0])):
			if lst[i][j] == 3:
				for (ni, nj) in getneighbor(i, j):
					if lst[ni][nj] == 1:
						lst[ni][nj] = 0
				lst[i][j] = 0
	for i in range(len(lst)):
		for j in range(len(lst[0])):
			if lst[i][j] == 1:
				lst[i][j] = 2
for t in range(t0, N + 1):
	if t % 2 == 0:
		fillbomb(lst)
	else:
		detonate(lst)
viewmap(lst)


def outOfBounds(xPos, yPos, rows, cols):
	thing = xPos >= 0 and xPos < cols and (yPos >= 0) and (yPos < rows)
	return not thing

def printVector(arr, rows, cols):
	for i in range(rows):
		for j in range(cols):
			print(arr[i][j], end='')
		print('\n', end='')

def getExploded(inpt, rows, cols):
	arrAlt = [[ord('O') for _x in range(cols)] for _y in range(rows)]
	for i in range(rows):
		for j in range(cols):
			if inpt[i][j] == 'O':
				arrAlt[i][j] = ord('.')
				if not outOfBounds(j - 1, i, rows, cols):
					arrAlt[i][j - 1] = ord('.')
				if not outOfBounds(j + 1, i, rows, cols):
					arrAlt[i][j + 1] = ord('.')
				if not outOfBounds(j, i + 1, rows, cols):
					arrAlt[i + 1][j] = ord('.')
				if not outOfBounds(j, i - 1, rows, cols):
					arrAlt[i - 1][j] = ord('.')
	arrAlt_char = [[chr(x) for x in y] for y in arrAlt]
	return arrAlt_char
(rows, cols, seconds) = [int(x) for x in input().strip().split()]
arr = []
for arr_i in range(rows):
	arr.append([x for x in list(input().strip())])
arrFull = [['O' for x in range(cols)] for y in range(rows)]
if seconds == 1:
	printVector(arr, rows, cols)
else:
	case = seconds % 4
	if case == 1:
		printVector(getExploded(getExploded(arr, rows, cols), rows, cols), rows, cols)
	elif case == 3:
		printVector(getExploded(arr, rows, cols), rows, cols)
	else:
		printVector(arrFull, rows, cols)


(r, c, n) = input().strip().split(' ')
(r, c, n) = (int(r), int(c), int(n))
import copy
if n == 1:
	for i in range(r):
		print(str(input()))
elif n % 2 == 0:
	result = 'O' * c
	for i in range(r):
		print(result)
else:
	grid = [[0 for colums in range(c)] for rows in range(r)]
	i = 0
	for i in range(r):
		s = str(input())
		j = 0
		for char in s:
			grid[i][j] = -1 if char == '.' else 2
			j += 1
		i += 1
	time = 1
	while time != 6:
		if time == 3:
			grid_modulo_3 = copy.deepcopy(grid)
		elif time == 5:
			grid_modulo_5 = copy.deepcopy(grid)
		if time % 2 == 1:
			for i in range(r):
				for j in range(c):
					if grid[i][j] == -1:
						grid[i][j] = 2
					else:
						grid[i][j] -= 1
		else:
			for i in range(r):
				for j in range(c):
					if grid[i][j] == 1:
						grid[i][j] = -1
						if i + 1 < r and grid[i + 1][j] != 1:
							grid[i + 1][j] = -1
						if i - 1 >= 0 and grid[i - 1][j] != 1:
							grid[i - 1][j] = -1
						if j + 1 < c and grid[i][j + 1] != 1:
							grid[i][j + 1] = -1
						if j - 1 >= 0 and grid[i][j - 1] != 1:
							grid[i][j - 1] = -1
		time += 1
	if n % 4 == 1:
		grid = grid_modulo_5
	else:
		grid = grid_modulo_3
	for row in grid:
		current_result = ''
		for el in row:
			if el > 0:
				current_result += 'O'
			else:
				current_result += '.'
		print(current_result)


(r, c, n) = [int(m) for m in input().split()]
grid = []
for i in range(r):
	row = [x for x in input()]
	newRow = [0 if x == '.' else 2 for x in row]
	grid.append(newRow)
if n > 4:
	n = n % 4 + 4
i = 1
while i < n:
	for j in range(r):
		for k in range(c):
			grid[j][k] = grid[j][k] + 1
			if grid[j][k] == 4:
				grid[j][k] = 0
				if j > 0:
					grid[j - 1][k] = 0
				if j < r - 1 and grid[j + 1][k] != 3:
					grid[j + 1][k] = -1
				if k > 0:
					grid[j][k - 1] = 0
				if k < c - 1 and grid[j][k + 1] != 3:
					grid[j][k + 1] = -1
	i += 1
for j in range(r):
	for k in range(c):
		if grid[j][k] == 0:
			print('.', end='')
		else:
			print(chr(79), end='')
	print()


def detonate(s, row, column):
	copy = [[i for i in j] for j in s]
	if row > 1 and column > 1:
		for i in range(row):
			for j in range(column):
				if copy[i][j] == 3:
					copy[i][j] = 2
				if copy[i][j] == 1:
					copy[i][j] = 0
					if i == 0:
						if copy[i + 1][j] != 1:
							copy[i + 1][j] = 0
					elif 0 < i < row - 1:
						copy[i - 1][j] = 0
						if copy[i + 1][j] != 1:
							copy[i + 1][j] = 0
					else:
						copy[i - 1][j] = 0
					if j == 0:
						if copy[i][j + 1] != 1:
							copy[i][j + 1] = 0
					elif 0 < j < column - 1:
						copy[i][j - 1] = 0
						if copy[i][j + 1] != 1:
							copy[i][j + 1] = 0
					else:
						copy[i][j - 1] = 0
	elif row == 1 and column > 1:
		for j in range(column):
			if copy[0][j] == 3:
				copy[0][j] = 2
			if copy[0][j] == 1:
				copy[0][j] = 0
				if j == 0:
					if copy[0][j + 1] != 1:
						copy[0][j + 1] = 0
				elif 0 < j < column - 1:
					copy[0][j - 1] = 0
					if copy[0][j + 1] != 1:
						copy[0][j + 1] = 0
				else:
					copy[0][j - 1] = 0
	elif row > 1 and column == 1:
		for i in range(row):
			if copy[i][0] == 3:
				copy[i][0] = 2
			if copy[i][0] == 1:
				copy[i][0] = 0
				if i == 0:
					if copy[i + 1][0] != 1:
						copy[i + 1][0] = 0
				elif 0 < i < row - 1:
					copy[i - 1][0] = 0
					if copy[i + 1][0] != 1:
						copy[i + 1][0] = 0
				else:
					copy[i - 1][0] = 0
	return copy

def plant(s, row, column):
	copy = [[i for i in j] for j in s]
	for i in range(row):
		for j in range(column):
			if copy[i][j] == 0:
				copy[i][j] = 3
			else:
				copy[i][j] -= 1
	return copy
(row, column, time) = [int(i) for i in input().split()]
if row == 1 and column == 1:
	output = [0] if (n + 1) % 4 == 0 else [3]
else:
	s1 = [[2 if i == 'O' else 0 for i in list(input())] for _ in range(row)]
	s2 = plant(s1, row, column)
	s3 = detonate(s2, row, column)
	s4 = plant(s3, row, column)
	s5 = detonate(s4, row, column)
	s = [s2, s3, s4, s5]
	if time == 1:
		output = s1
	elif time == 2:
		output = s2
	else:
		output = s[(time - 2) % 4]
for i in output:
	print(''.join(['.' if j == 0 else 'O' for j in i]))


import sys

def pprint(arena):
	for y in range(R):
		for x in range(C):
			print(arena[y][x], end='')
		print('')

def checkfield(arena, x, y):
	if arena[y][x] == 'O':
		return False
	if y - 1 >= 0 and arena[y - 1][x] == 'O':
		return False
	if y + 1 < R and arena[y + 1][x] == 'O':
		return False
	if x + 1 < C and arena[y][x + 1] == 'O':
		return False
	if x - 1 >= 0 and arena[y][x - 1] == 'O':
		return False
	return True

def bomb(arena):
	old_arena = [row[:] for row in arena]
	for y in range(R):
		for x in range(C):
			if checkfield(old_arena, x, y):
				arena[y][x] = 'O'
			else:
				arena[y][x] = '.'
(R, C, N) = [int(i) for i in input().strip().split(' ')]
arena = []
for row in range(R):
	arena += [list(input().strip())]
first = [row[:] for row in arena]
bomb(arena)
second = [row[:] for row in arena]
bomb(arena)
third = [row[:] for row in arena]
if N % 2 == 0:
	for y in range(R):
		for x in range(C):
			print('O', end='')
		print('')
elif N == 1:
	pprint(first)
elif N % 4 == 1:
	pprint(third)
elif N % 4 == 3:
	pprint(second)


import sys
(r, c, n) = input().strip().split(' ')
(r, c, n) = [int(r), int(c), int(n)]

def to_string(s):
	st = ''
	for row in range(r):
		rowst = ''
		for col in range(c):
			rowst += 'O' if s[row][col] != '.' else '.'
		st += rowst + '\n'
	return st

def boom_grid(mask, grid):
	for row in range(r):
		for col in range(c):
			if mask[row][col] != '.':
				grid[row][col] = '.'
				grid[min(row + 1, r - 1)][col] = '.'
				grid[row][max(col - 1, 0)] = '.'
				grid[row][min(col + 1, c - 1)] = '.'
				grid[max(row - 1, 0)][col] = '.'
	return grid
grid = []
[grid.append(list(input().strip())) for _ in range(r)]
pattern = n % 4
res = []
if pattern == 0 or pattern == 2:
	res = to_string([['O' for _ in range(c)] for _ in range(r)])
elif pattern == 1 and n != pattern:
	gtemp2 = boom_grid(boom_grid(grid, [['O' for _ in range(c)] for _ in range(r)]), [['O' for _ in range(c)] for _ in range(r)])
	res = to_string(gtemp2)
elif pattern == 3:
	gtemp = boom_grid(grid, [['O' for _ in range(c)] for _ in range(r)])
	res = to_string(gtemp)
elif n == 1:
	res = to_string(grid)
print(res)


def chchar(x, n):
	if x[n] == o:
		return x
	elif n == 0:
		x = '+' + x[1:]
	elif n == len(x) - 1:
		x = x[:n] + '+'
	else:
		x = x[:n] + '+' + x[n + 1:]
	return x

def fill4(s):
	for i in range(len(s)):
		if s[i] == o:
			if i // c != 0:
				s = chchar(s, i - c)
			if i // c != r - 1:
				s = chchar(s, i + c)
			if i % c != 0:
				s = chchar(s, i - 1)
			if i % c != c - 1:
				s = chchar(s, i + 1)
	s = s.replace('+', o)
	s = s.replace('.', '*')
	s = s.replace(o, '.')
	s = s.replace('*', o)
	return s
(r, c, n) = input().strip().split()
(r, c, n) = (int(r), int(c), int(n))
o = chr(79)
s = ''
for i in range(r):
	s += input().strip()
if n == 0 or n == 1:
	pass
elif (n - 1) % 4 == 0:
	s = fill4(s)
	s = fill4(s)
elif n % 2 == 0:
	s = o * (r * c)
elif (n + 1) % 4 == 0:
	s = fill4(s)
for i in range(r):
	print(s[c * i:c * (i + 1)])


(r, c, n) = input().strip().split(' ')
(r, c, n) = (int(r), int(c), int(n))
grid = [['|'] * (c + 2)] + [['|'] + list(input().strip()) + ['|'] for i in range(r)] + [['|'] * (c + 2)]
if n == 1:
	for j in range(r):
		print(''.join(grid[j + 1][1:-1]))
elif n % 2 == 0:
	for j in range(r):
		print('O' * c)
else:
	dct = {}
	for j in range(r):
		for k in range(c):
			if grid[j + 1][k + 1] == '.':
				if not 'O' in grid[j + 1][k:k + 3] + [grid[j][k + 1]] + [grid[j + 2][k + 1]]:
					dct[j + 1, k + 1] = 1
	for j in range(r):
		row = ''
		for k in range(c):
			if (j + 1, k + 1) in dct:
				row += '.' if n % 4 == 1 else 'O'
			elif grid[j + 1][k + 1] == 'O':
				row += 'O' if n % 4 == 1 else '.'
			elif grid[j + 1][k + 1] == '.' and all((element not in dct for element in [(j + 1, k), (j + 1, k + 2), (j, k + 1), (j + 2, k + 1)])):
				row += 'O' if n % 4 == 1 else '.'
			else:
				row += '.'
		print(row)


class Board(object):

	def __init__(self, rows, columns):
		self.board = [[None for column in range(columns)] for row in range(rows)]
		self.explosion_queue = []

	def __str__(self):
		s = ''
		for row in self.board:
			s += ''.join([sq.state for sq in row]) + '\n'
		return s

	def enqueue_explosion(self, position):
		self.explosion_queue.append(position)
		self.explosion_queue.append((min(position[0] + 1, len(self.board) - 1), position[1]))
		self.explosion_queue.append((max(position[0] - 1, 0), position[1]))
		self.explosion_queue.append((position[0], min(position[1] + 1, len(self.board[0]) - 1)))
		self.explosion_queue.append((position[0], max(position[1] - 1, 0)))

	def fillBombs(self):
		for row in self.board:
			for sq in row:
				sq.setBomb()

	def explode(self):
		for pos in set(self.explosion_queue):
			self.board[pos[0]][pos[1]].explode()
		self.explosion_queue.clear()

	def update(self):
		for row in self.board:
			for sq in row:
				sq.tickUp()
		if self.explosion_queue:
			self.explode()

class Square(object):

	def __init__(self, state, row, col, board):
		self.state = state
		self.ticker = 0
		self.position = (row, col)
		self.board = board

	def resetSquare(self):
		self.ticker = 0

	def tickUp(self):
		if self.state == '.':
			return
		self.ticker += 1
		if self.ticker == 3:
			board.enqueue_explosion(self.position)

	def explode(self):
		self.ticker = 0
		self.state = '.'

	def setBomb(self):
		self.state = 'O'
(R, C, N) = [int(x) for x in input().strip().split(' ')]
board = Board(R, C)
for r in range(R):
	s = input().strip()
	for c in enumerate(s):
		board.board[r][c[0]] = Square(c[1], r, c[0], board)
t = 1
N = min(N, N % 4 + 4)
while t <= N:
	board.update()
	if t % 2 == 0:
		board.fillBombs()
	t += 1
print(board)


class Board(object):

	def __init__(self, rows, columns):
		self.board = [[None for column in range(columns)] for row in range(rows)]
		self.explosion_queue = []

	def __str__(self):
		s = ''
		for row in self.board:
			s += ''.join([sq.state for sq in row]) + '\n'
		return s

	def enqueue_explosion(self, position):
		self.explosion_queue.append(position)
		self.explosion_queue.append((min(position[0] + 1, len(self.board) - 1), position[1]))
		self.explosion_queue.append((max(position[0] - 1, 0), position[1]))
		self.explosion_queue.append((position[0], min(position[1] + 1, len(self.board[0]) - 1)))
		self.explosion_queue.append((position[0], max(position[1] - 1, 0)))

	def fillBombs(self):
		for row in self.board:
			for sq in row:
				sq.setBomb()

	def explode(self):
		for pos in set(self.explosion_queue):
			self.board[pos[0]][pos[1]].explode()
		self.explosion_queue.clear()

	def update(self):
		for row in self.board:
			for sq in row:
				sq.tickUp()
		if self.explosion_queue:
			self.explode()

class Square(object):

	def __init__(self, state, row, col, board):
		self.state = state
		self.ticker = 0
		self.position = (row, col)
		self.board = board

	def resetSquare(self):
		self.ticker = 0

	def tickUp(self):
		if self.state == '.':
			return
		self.ticker += 1
		if self.ticker == 3:
			board.enqueue_explosion(self.position)

	def explode(self):
		self.ticker = 0
		self.state = '.'

	def setBomb(self):
		self.state = 'O'
(R, C, N) = [int(x) for x in input().strip().split(' ')]
board = Board(R, C)
for r in range(R):
	s = input().strip()
	for c in enumerate(s):
		board.board[r][c[0]] = Square(c[1], r, c[0], board)
t = 1
N = min(N, N % 4 + 4)
while t <= N:
	board.update()
	if t % 2 == 0:
		board.fillBombs()
	t += 1
print(board)


class Board(object):

	def __init__(self, rows, columns):
		self.board = [[None for column in range(columns)] for row in range(rows)]
		self.explosion_queue = []

	def __str__(self):
		s = ''
		for row in self.board:
			s += ''.join([sq.state for sq in row]) + '\n'
		return s

	def enqueue_explosion(self, position):
		self.explosion_queue.append(position)
		self.explosion_queue.append((min(position[0] + 1, len(self.board) - 1), position[1]))
		self.explosion_queue.append((max(position[0] - 1, 0), position[1]))
		self.explosion_queue.append((position[0], min(position[1] + 1, len(self.board[0]) - 1)))
		self.explosion_queue.append((position[0], max(position[1] - 1, 0)))

	def fillBombs(self):
		for row in self.board:
			for sq in row:
				sq.setBomb()

	def explode(self):
		for pos in set(self.explosion_queue):
			self.board[pos[0]][pos[1]].explode()
		self.explosion_queue.clear()

	def update(self):
		for row in self.board:
			for sq in row:
				sq.tickUp()
		if self.explosion_queue:
			self.explode()

class Square(object):

	def __init__(self, state, row, col, board):
		self.state = state
		self.ticker = 0
		self.position = (row, col)
		self.board = board

	def resetSquare(self):
		self.ticker = 0

	def tickUp(self):
		if self.state == '.':
			return
		self.ticker += 1
		if self.ticker == 3:
			board.enqueue_explosion(self.position)

	def explode(self):
		self.ticker = 0
		self.state = '.'

	def setBomb(self):
		self.state = 'O'
(R, C, N) = [int(x) for x in input().strip().split(' ')]
board = Board(R, C)
for r in range(R):
	s = input().strip()
	for c in enumerate(s):
		board.board[r][c[0]] = Square(c[1], r, c[0], board)
t = 1
N = min(N, N % 4 + 4)
while t <= N:
	board.update()
	if t % 2 == 0:
		board.fillBombs()
	t += 1
print(board)


verbose = True
verbose = False
(r, c, n) = [int(i) for i in input().split()]
g = []
for ri in range(r):
	l = []
	i = input()
	for ci in range(c):
		if i[ci] == '.':
			l.append(0)
		else:
			l.append(3)
	g.append(l)

def print_g(g, value=False):
	for l in g:
		out = []
		for e in l:
			if value:
				out.append(str(e))
			elif e == 0:
				out.append('.')
			else:
				out.append('O')
		print(''.join(out))

def fill_g(g):
	for r in range(len(g)):
		for c in range(len(g[r])):
			if g[r][c] == 0:
				g[r][c] = 3
	return g

def unshared_copy(inList):
	if isinstance(inList, list):
		return list(map(unshared_copy, inList))
	return inList

def decrement_g(g):
	new_g = unshared_copy(g)
	for r in range(len(g)):
		for c in range(len(g[r])):
			new_g[r][c] = -1
	for r in range(len(g)):
		for c in range(len(g[r])):
			if g[r][c] > 1:
				if new_g[r][c] == -1:
					new_g[r][c] = g[r][c] - 1
			elif g[r][c] == 1:
				new_g[r][c] = 0
				if r > 0:
					new_g[r - 1][c] = 0
				if r + 1 < len(g):
					new_g[r + 1][c] = 0
				if c > 0:
					new_g[r][c - 1] = 0
				if c + 1 < len(g[r]):
					new_g[r][c + 1] = 0
			else:
				new_g[r][c] = 0
	return new_g
base_g = unshared_copy(g)
full_g = fill_g(unshared_copy(g))
three_g = decrement_g(fill_g(decrement_g(decrement_g(unshared_copy(g)))))
five_g = decrement_g(fill_g(decrement_g(unshared_copy(three_g))))
if n <= 1:
	print_g(base_g)
elif n % 2 == 0:
	print_g(full_g)
elif n % 4 == 1:
	print_g(five_g)
else:
	print_g(three_g)


def isValid(i, j):
	if i >= 0 and i < r and (j >= 0) and (j < c):
		if grid[i][j] != other_bomb:
			return True
	return False

def printMatrix():
	for i in range(r):
		for j in range(c):
			if grid[i][j] == '.':
				print('.', end='')
			else:
				print('O', end='')
		print()
(r, c, N) = map(int, input().strip().split(' '))
grid = []
for i in range(r):
	grid.append([x for x in input().strip()])
N = (N - 1) % 92 + 1
curr_bomb = 'O'
other_bomb = 'P'
for n in range(2, N + 1):
	if n % 2 == 0:
		(curr_bomb, other_bomb) = (other_bomb, curr_bomb)
		for i in range(r):
			for j in range(c):
				if grid[i][j] == '.':
					grid[i][j] = curr_bomb
	elif n % 2 == 1:
		for i in range(r):
			for j in range(c):
				if grid[i][j] == other_bomb:
					grid[i][j] = '.'
					if isValid(i + 1, j):
						grid[i + 1][j] = '.'
					if isValid(i - 1, j):
						grid[i - 1][j] = '.'
					if isValid(i, j + 1):
						grid[i][j + 1] = '.'
					if isValid(i, j - 1):
						grid[i][j - 1] = '.'
printMatrix()


def type2(grid, r, c):
	for i in range(r):
		for j in range(c):
			if grid[i][j] == '.':
				grid[i][j] = 'x'
	for i in range(r):
		for j in range(c):
			if grid[i][j] == 'O':
				grid[i][j] = '.'
				if j > 0 and grid[i][j - 1] != 'O':
					grid[i][j - 1] = '.'
				if j < c - 1 and grid[i][j + 1] != 'O':
					grid[i][j + 1] = '.'
				if i > 0 and grid[i - 1][j] != 'O':
					grid[i - 1][j] = '.'
				if i < r - 1 and grid[i + 1][j] != 'O':
					grid[i + 1][j] = '.'
	for i in range(r):
		for j in range(c):
			if grid[i][j] == 'x':
				grid[i][j] = 'O'
	return grid

def printgrid(grid, r, c):
	for i in range(r):
		for j in range(c):
			print(grid[i][j], end='')
		print()
(r, c, s) = map(int, input().split())
grid = []
for i in range(r):
	grid.append(list(input())[0:c])
if s == 1:
	printgrid(grid, r, c)
elif s % 2 == 0:
	for i in range(r):
		for j in range(c):
			print('O', end='')
		print()
else:
	grid = type2(grid, r, c)
	if (s + 1) % 4 == 0:
		printgrid(grid, r, c)
	else:
		grid = type2(grid, r, c)
		printgrid(grid, r, c)


def modify(g, i, j, new, old):
	for change in [(1, 0), (-1, 0), (0, 1), (0, -1)]:
		(x, y) = map(sum, zip(change, (i, j)))
		if 0 <= x < r and 0 <= y < c:
			if g[x][y] in old:
				g[x][y] = new

def output(g, bomb):
	for row in g:
		for c in row:
			if c in bomb:
				print('O', end='')
			else:
				print('.', end='')
		print()

def solve(r, c, n):
	if n % 2 == 0:
		for i in range(r):
			print('O' * c)
		return
	grid = []
	for row in range(r):
		grid.append([c for c in input().strip()])
	if n == 1:
		output(grid, bomb='O')
		return
	for i in range(r):
		for j in range(c):
			if grid[i][j] == 'O':
				modify(grid, i, j, 'S', '.')
	for i in range(r):
		for j in range(c):
			if grid[i][j] == '.':
				modify(grid, i, j, 'B', 'S')
	if n // 2 % 2 == 0:
		output(grid, bomb=('O', 'S'))
	else:
		output(grid, bomb='.')
(r, c, n) = map(int, input().split())
solve(r, c, n)


def print_mat(m):
	for row in m:
		print(''.join(row))
(r, c, n) = [int(i) for i in input().strip().split(' ')]
mat = list()
for i in range(r):
	mat.append(list(input().strip()))
all_mat = [['O' for i in range(c)] for j in range(r)]
new_mat_1 = [row[:] for row in all_mat]
for i in range(r):
	for j in range(c):
		if mat[i][j] == 'O':
			new_mat_1[i][j] = '.'
			if i != 0:
				new_mat_1[i - 1][j] = '.'
			if i != r - 1:
				new_mat_1[i + 1][j] = '.'
			if j != 0:
				new_mat_1[i][j - 1] = '.'
			if j != c - 1:
				new_mat_1[i][j + 1] = '.'
new_mat_2 = [row[:] for row in all_mat]
for i in range(r):
	for j in range(c):
		if new_mat_1[i][j] == 'O':
			new_mat_2[i][j] = '.'
			if i != 0:
				new_mat_2[i - 1][j] = '.'
			if i != r - 1:
				new_mat_2[i + 1][j] = '.'
			if j != 0:
				new_mat_2[i][j - 1] = '.'
			if j != c - 1:
				new_mat_2[i][j + 1] = '.'
if n % 2 == 0:
	print_mat(all_mat)
elif n == 1:
	print_mat(mat)
elif n % 4 == 1:
	print_mat(new_mat_2)
elif n % 4 == 3:
	print_mat(new_mat_1)


(r, c, n) = map(int, input().split())
(board, orign) = ([], [])
for i in range(r):
	board.append([])
	s = input()
	orign.append(s)
	for j in range(c):
		board[i].append(s[j])
full = [['O'] * c for _ in range(r)]
visit = [[False] * c for _ in range(r)]

def findBombers(board, r, c, visit):

	def dfs(board, i, j):
		if i < 0 or j < 0 or i == r or (j == c) or (board[i][j] == 'X') or visit[i][j]:
			return
		if board[i][j] == 'O':
			visit[i][j] = True
		if board[i][j] == '.':
			board[i][j] = 'X'
			return
		dfs(board, i + 1, j)
		dfs(board, i, j + 1)
		dfs(board, i - 1, j)
		dfs(board, i, j - 1)
	for i in range(r):
		for j in range(c):
			if board[i][j] == 'O':
				dfs(board, i, j)
	for i in range(r):
		for j in range(c):
			if board[i][j] == '.':
				board[i][j] = 'O'
			elif board[i][j] == 'O' or board[i][j] == 'X':
				board[i][j] = '.'
			visit[i][j] = False

def printboard(board):
	for i in range(r):
		for j in range(c):
			print(board[i][j], end='')
		print()
if n == 1:
	printboard(orign)
elif n % 2 == 0:
	printboard(full)
else:
	findBombers(board, r, c, visit)
	if n % 4 == 1:
		findBombers(board, r, c, visit)
	printboard(board)


import sys

class Cell(object):

	def __init__(self):
		self.has_bomb = False
		self.explode_time = -1
		self.to_explode = False

	def plant_bomb(self, time):
		self.has_bomb = True
		self.explode_time = time + 3

	def explode(self):
		self.to_explode = True

	def cleanup(self):
		self.to_explode = False
		self.has_bomb = False
		self.explode_time = -1

class Board(object):

	def __init__(self, R, C):
		self.R = R
		self.C = C
		self.board = [[Cell() for x in range(C)] for y in range(R)]

	def get(self, R, C):
		return self.board[R][C]

	def print_board(self):
		for y in range(len(self.board)):
			for x in range(len(self.board[y])):
				if self.board[y][x].has_bomb:
					print('O', end='')
				else:
					print('.', end='')
			print('')

	def check_board(self, time):
		for y in range(len(self.board)):
			for x in range(len(self.board[y])):
				if self.board[y][x].has_bomb and self.board[y][x].explode_time == time:
					self.board[y][x].explode()
					if x - 1 >= 0:
						self.board[y][x - 1].explode()
					if x + 1 < self.C:
						self.board[y][x + 1].explode()
					if y + 1 < self.R:
						self.board[y + 1][x].explode()
					if y - 1 >= 0:
						self.board[y - 1][x].explode()
		for y in range(len(self.board)):
			for x in range(len(self.board[y])):
				if self.board[y][x].to_explode:
					self.board[y][x].cleanup()

	def populate_board(self, initial_board):
		for y in range(len(initial_board)):
			for x in range(len(initial_board[y])):
				if initial_board[y][x] == 'O':
					self.board[y][x].plant_bomb(0)

	def plant_all(self, time):
		for y in range(len(self.board)):
			for x in range(len(self.board[y])):
				if not self.board[y][x].has_bomb:
					self.board[y][x].plant_bomb(time)

def main():
	time = 0
	(r, c, n) = map(int, input().split())
	if n > 4:
		n = n % 4 + 4
	board = Board(int(r), int(c))
	initial_board = sys.stdin.readlines()
	board.populate_board(initial_board)
	time += 1
	while time < n:
		time += 1
		if time % 2 == 0:
			board.plant_all(time)
		else:
			board.check_board(time)
	board.print_board()
main()


class Board(object):

	def __init__(self, rows, columns):
		self.board = [[None for column in range(columns)] for row in range(rows)]
		self.explosion_queue = []

	def __str__(self):
		s = ''
		for row in self.board:
			s += ''.join([sq.state for sq in row]) + '\n'
		return s

	def enqueue_explosion(self, position):
		self.explosion_queue.append(position)
		self.explosion_queue.append((min(position[0] + 1, len(self.board) - 1), position[1]))
		self.explosion_queue.append((max(position[0] - 1, 0), position[1]))
		self.explosion_queue.append((position[0], min(position[1] + 1, len(self.board[0]) - 1)))
		self.explosion_queue.append((position[0], max(position[1] - 1, 0)))

	def fillBombs(self):
		for row in self.board:
			for sq in row:
				sq.setBomb()

	def explode(self):
		for pos in set(self.explosion_queue):
			self.board[pos[0]][pos[1]].explode()
		self.explosion_queue.clear()

	def update(self):
		for row in self.board:
			for sq in row:
				sq.tickUp()
		if self.explosion_queue:
			self.explode()

class Square(object):

	def __init__(self, state, row, col, board):
		self.state = state
		self.ticker = 0
		self.position = (row, col)
		self.board = board

	def resetSquare(self):
		self.ticker = 0

	def tickUp(self):
		if self.state == '.':
			return
		self.ticker += 1
		if self.ticker == 3:
			board.enqueue_explosion(self.position)

	def explode(self):
		self.ticker = 0
		self.state = '.'

	def setBomb(self):
		self.state = 'O'
(R, C, N) = [int(x) for x in input().strip().split(' ')]
board = Board(R, C)
for r in range(R):
	s = input().strip()
	for c in enumerate(s):
		board.board[r][c[0]] = Square(c[1], r, c[0], board)
t = 1
N = min(N, N % 4 + 4)
while t <= N:
	board.update()
	if t % 2 == 0:
		board.fillBombs()
	t += 1
print(board)


def dual(grid):
	(R, C) = (len(grid), len(grid[0]))
	(old_grid, grid) = (grid, [C * ['O'] for r in range(R)])
	for r in range(R):
		for c in range(C):
			if old_grid[r][c] == '.':
				continue
			for r2 in range(r - 1, r + 2):
				for c2 in range(c - 1, c + 2):
					if r2 < 0 or c2 < 0 or R <= r2 or (C <= c2):
						continue
					if 1 < abs(r2 - r) + abs(c2 - c):
						continue
					grid[r2][c2] = '.'
	return grid
(R, C, N) = (int(x) for x in input().strip().split())
grid = [list(input().strip()) for r in range(R)]
if N == 0 or N == 1:
	pass
elif N % 2 == 0:
	grid = [C * ['O'] for r in range(R)]
elif N % 4 == 3:
	grid = dual(grid)
else:
	grid = dual(dual(grid))
for row in grid:
	print(''.join(row))


class Board(object):

	def __init__(self, rows, columns):
		self.board = [[None for column in range(columns)] for row in range(rows)]
		self.explosion_queue = []

	def __str__(self):
		s = ''
		for row in self.board:
			s += ''.join([sq.state for sq in row]) + '\n'
		return s

	def enqueue_explosion(self, position):
		self.explosion_queue.append(position)
		self.explosion_queue.append((min(position[0] + 1, len(self.board) - 1), position[1]))
		self.explosion_queue.append((max(position[0] - 1, 0), position[1]))
		self.explosion_queue.append((position[0], min(position[1] + 1, len(self.board[0]) - 1)))
		self.explosion_queue.append((position[0], max(position[1] - 1, 0)))

	def fillBombs(self):
		for row in self.board:
			for sq in row:
				sq.setBomb()

	def explode(self):
		for pos in set(self.explosion_queue):
			self.board[pos[0]][pos[1]].explode()
		self.explosion_queue.clear()

	def update(self):
		for row in self.board:
			for sq in row:
				sq.tickUp()
		if self.explosion_queue:
			self.explode()

class Square(object):

	def __init__(self, state, row, col, board):
		self.state = state
		self.ticker = 0
		self.position = (row, col)
		self.board = board

	def resetSquare(self):
		self.ticker = 0

	def tickUp(self):
		if self.state == '.':
			return
		self.ticker += 1
		if self.ticker == 3:
			board.enqueue_explosion(self.position)

	def explode(self):
		self.ticker = 0
		self.state = '.'

	def setBomb(self):
		self.state = 'O'
(R, C, N) = [int(x) for x in input().strip().split(' ')]
board = Board(R, C)
for r in range(R):
	s = input().strip()
	for c in enumerate(s):
		board.board[r][c[0]] = Square(c[1], r, c[0], board)
t = 1
N = min(N, N % 4 + 4)
while t <= N:
	board.update()
	if t % 2 == 0:
		board.fillBombs()
	t += 1
print(board)


import sys
import copy
(r, c, n) = map(int, input().strip().split())
grids = []
for i in range(r):
	grids.append([])
	for element in input().strip().split():
		for e in element:
			grids[i].append(e)
for loop in range((n + 1) % 256):
	if loop % 2 == 0 and (not loop == 0):
		for i in range(r):
			for j in range(c):
				if grids[i][j] == '.':
					grids[i][j] = 'O'
				else:
					grids[i][j] = '+'
	if loop % 2 == 1 and (not loop == 0):
		for i in range(r):
			for j in range(c):
				if grids[i][j] == '+':
					grids[i][j] = '.'
					if j > 0:
						if grids[i][j - 1] == 'O':
							grids[i][j - 1] = '.'
					if j < c - 1:
						if grids[i][j + 1] == 'O':
							grids[i][j + 1] = '.'
					if i > 0:
						if grids[i - 1][j] == 'O':
							grids[i - 1][j] = '.'
					if i < r - 1:
						if grids[i + 1][j] == 'O':
							grids[i + 1][j] = '.'
for i in range(r):
	for j in range(c):
		if grids[i][j] == '+':
			grids[i][j] = 'O'
for i in range(r):
	for j in range(c):
		sys.stdout.write(grids[i][j])
	sys.stdout.write('\n')


def printres(a):
	for x in a:
		print(''.join(x))
(r, c, n) = map(int, input().split())
a = []
for _ in range(r):
	a.append(list(input()))
a1 = [list('O' * c) for _ in range(r)]
a3 = [list('O' * c) for _ in range(r)]
for j in range(r):
	for x in range(c):
		if a[j][x] == 'O':
			a3[j][x] = '.'
			if j - 1 >= 0:
				a3[j - 1][x] = '.'
			if j + 1 < r:
				a3[j + 1][x] = '.'
			if x - 1 >= 0:
				a3[j][x - 1] = '.'
			if x + 1 < c:
				a3[j][x + 1] = '.'
a4 = [list('O' * c) for _ in range(r)]
for j in range(r):
	for x in range(c):
		if a3[j][x] == 'O':
			a4[j][x] = '.'
			if j - 1 >= 0:
				a4[j - 1][x] = '.'
			if j + 1 < r:
				a4[j + 1][x] = '.'
			if x - 1 >= 0:
				a4[j][x - 1] = '.'
			if x + 1 < c:
				a4[j][x + 1] = '.'
if n == 0 or n == 1:
	printres(a)
elif n % 2 == 0:
	printres(a1)
elif (n - 3) % 4 == 0 or n == 3:
	printres(a3)
else:
	printres(a4)


def write(grid):
	for row in range(len(grid)):
		for col in range(len(grid[0])):
			print(grid[row][col], end='')
		print()

def write_full(r, c):
	for row in range(r):
		for col in range(c):
			print('O', end='')
		print()

def in_bomb_neighborhood(grid, r, c):
	rows = len(grid)
	cols = len(grid[0])
	if grid[r][c] == 'O' or (r < rows - 1 and grid[r + 1][c] == 'O') or (r > 0 and grid[r - 1][c] == 'O') or (c < cols - 1 and grid[r][c + 1] == 'O') or (c > 0 and grid[r][c - 1] == 'O'):
		return True
	else:
		return False

def iterate(grid):
	rows = len(grid)
	cols = len(grid[0])
	new_grid = [['.' for c in range(cols)] for r in range(rows)]
	for r in range(rows):
		for c in range(cols):
			if in_bomb_neighborhood(grid, r, c):
				pass
			else:
				new_grid[r][c] = 'O'
	return new_grid
(r, c, n) = map(int, input().split())
initial_grid = list()
for row in range(r):
	initial_grid.append(list(input()))
second_grid = iterate(initial_grid)
third_grid = iterate(second_grid)
if n == 1:
	write(initial_grid)
elif n % 2 == 0:
	write_full(r, c)
elif n % 4 == 3:
	write(second_grid)
else:
	write(third_grid)


import sys

class Bomb:

	def __init__(self, game_tick):
		self.planted_at = game_tick

	def detonates(self, game_tick):
		return game_tick - self.planted_at >= 3

class GameController:

	def __init__(self, initial_board, turns):
		self.board = initial_board
		self.turns = turns
		self.tick = 0

	@classmethod
	def from_input(cls):
		(rows, cols, turns) = [int(i) for i in input().strip().split()]
		if turns > 2:
			turns = 2 + (turns - 2) % 4
		board = [[None] * cols for _ in range(rows)]
		for row in range(rows):
			line = input().strip()
			if len(line) != cols:
				raise ValueError('Invalid input row')
			for (col, ch) in enumerate(line):
				if ch == 'O':
					board[row][col] = Bomb(0)
		return GameController(board, turns)

	def enumerate_items(self):
		for row in range(len(self.board)):
			for (col, item) in enumerate(self.board[row]):
				yield (row, col, item)

	def simulate_turn(self):
		self.tick += 1
		if self.tick % 2 == 0:
			for (row, col, item) in self.enumerate_items():
				if item is None:
					self.board[row][col] = Bomb(self.tick)
		else:
			destroyed_bombs = set()
			for (row, col, item) in self.enumerate_items():
				if item is not None and item.detonates(self.tick):
					self.board[row][col] = None
					if row > 0 and self.board[row - 1][col] is not None:
						self.board[row - 1][col] = None
					if row < len(self.board) - 1 and self.board[row + 1][col] is not None:
						destroyed_bombs.add((row + 1, col))
					if col > 0 and self.board[row][col - 1] is not None:
						self.board[row][col - 1] = None
					if col < len(self.board[row]) - 1 and self.board[row][col + 1] is not None:
						destroyed_bombs.add((row, col + 1))
			for (row, col) in destroyed_bombs:
				self.board[row][col] = None

	def simulate(self):
		while self.tick < self.turns:
			self.simulate_turn()

	def to_string(self):
		return '\n'.join((''.join(('O' if item is not None else '.' for item in self.board[row])) for row in range(len(self.board))))
gc = GameController.from_input()
gc.simulate()
print(gc.to_string())


(R, C, N) = list(map(int, input().strip().split(' ')))
row_column = list()
for i in range(R):
	row = list(input().strip())
	row_column.append(row)

def on_board(r, c, R, C):
	return 0 <= r < R and 0 <= c < C

def get_full(board):
	for i in range(len(board)):
		for j in range(len(board[0])):
			board[i][j] = 'O'
	return board

def bomb_neighbors(board, should_bomb, i, j):
	if on_board(i - 1, j, R, C):
		should_bomb.append((i - 1, j))
	if on_board(i + 1, j, R, C):
		should_bomb.append((i + 1, j))
	if on_board(i, j - 1, R, C):
		should_bomb.append((i, j - 1))
	if on_board(i, j + 1, R, C):
		should_bomb.append((i, j + 1))

def get_third(board):
	should_bomb = list()
	for i in range(len(board)):
		for j in range(len(board[0])):
			if board[i][j] == '.':
				board[i][j] = 'O'
				continue
			if board[i][j] == 'O':
				should_bomb.append((i, j))
				bomb_neighbors(board, should_bomb, i, j)
	for pair in should_bomb:
		board[pair[0]][pair[1]] = '.'
	return board

def get_fifth(board):
	should_bomb = list()
	third = get_third(board)
	for i in range(len(third)):
		for j in range(len(third[0])):
			if third[i][j] == '.':
				third[i][j] = 'O'
				continue
			if third[i][j] == 'O':
				should_bomb.append((i, j))
				bomb_neighbors(board, should_bomb, i, j)
	for pair in should_bomb:
		board[pair[0]][pair[1]] = '.'
	return third

def get_board(board, N):
	if N == 0 or N == 1:
		return board
	if N % 2 == 0:
		return get_full(board)
	if (N - 3) % 4 == 0:
		return get_third(board)
	if (N - 5) % 4 == 0:
		return get_fifth(board)
	return None

def print_board(board):
	for i in range(len(board)):
		print(''.join(board[i]))
final = get_board(row_column, N)
print_board(final)


(r, c, n) = [int(x) for x in input().split()]
grid = [input() for _ in range(r)]
if not n % 2:
	print('\n'.join([''.join(['O' for _ in range(c)]) for _ in range(r)]))
elif n == 1:
	print('\n'.join(grid))
else:
	new_grid = []
	for y in range(r):
		new_grid.append([])
		for x in range(c):
			if grid[y][x] == 'O' or (x > 0 and grid[y][x - 1] == 'O') or (x < c - 1 and grid[y][x + 1] == 'O') or (y > 0 and grid[y - 1][x] == 'O') or (y < r - 1 and grid[y + 1][x] == 'O'):
				new_grid[y].append('.')
			else:
				new_grid[y].append('O')
	if n // 2 % 2:
		print('\n'.join([''.join(new_grid[y]) for y in range(r)]))
	else:
		print('\n'.join([''.join(['.' if new_grid[y][x] == 'O' or (x > 0 and new_grid[y][x - 1] == 'O') or (x < c - 1 and new_grid[y][x + 1] == 'O') or (y > 0 and new_grid[y - 1][x] == 'O') or (y < r - 1 and new_grid[y + 1][x] == 'O') else 'O' for x in range(c)]) for y in range(r)]))


def compute(R, C, l):
	for i in range(R):
		for j in range(C):
			if l[i][j] == 'O':
				l[i][j] = '$'
				if i > 0 and l[i - 1][j] != 'O':
					l[i - 1][j] = '$'
				if j > 0 and l[i][j - 1] != 'O':
					l[i][j - 1] = '$'
				if i < R - 1 and l[i + 1][j] != 'O':
					l[i + 1][j] = '$'
				if j < C - 1 and l[i][j + 1] != 'O':
					l[i][j + 1] = '$'
	for i in range(R):
		for j in range(C):
			if l[i][j] == '$':
				l[i][j] = '.'
			else:
				l[i][j] = 'O'
(R, C, N) = [int(x) for x in input().split()]
l = []
for i in range(R):
	l.append(list(input()))
if N % 2 == 0:
	for i in range(R):
		print('O' * C)
elif N == 1:
	for i in range(R):
		print(''.join(l[i]))
else:
	compute(R, C, l)
	if N % 4 == 3:
		for i in range(R):
			print(''.join(l[i]))
	else:
		compute(R, C, l)
		for i in range(R):
			print(''.join(l[i]))


class Board(object):

	def __init__(self, rows, columns):
		self.board = [[None for column in range(columns)] for row in range(rows)]
		self.explosion_queue = []

	def __str__(self):
		s = ''
		for row in self.board:
			s += ''.join([sq.state for sq in row]) + '\n'
		return s

	def enqueue_explosion(self, position):
		self.explosion_queue.append(position)
		self.explosion_queue.append((min(position[0] + 1, len(self.board) - 1), position[1]))
		self.explosion_queue.append((max(position[0] - 1, 0), position[1]))
		self.explosion_queue.append((position[0], min(position[1] + 1, len(self.board[0]) - 1)))
		self.explosion_queue.append((position[0], max(position[1] - 1, 0)))

	def fillBombs(self):
		for row in self.board:
			for sq in row:
				sq.setBomb()

	def explode(self):
		for pos in set(self.explosion_queue):
			self.board[pos[0]][pos[1]].explode()
		self.explosion_queue.clear()

	def update(self):
		for row in self.board:
			for sq in row:
				sq.tickUp()
		if self.explosion_queue:
			self.explode()

class Square(object):

	def __init__(self, state, row, col, board):
		self.state = state
		self.ticker = 0
		self.position = (row, col)
		self.board = board

	def resetSquare(self):
		self.ticker = 0

	def tickUp(self):
		if self.state == '.':
			return
		self.ticker += 1
		if self.ticker == 3:
			board.enqueue_explosion(self.position)

	def explode(self):
		self.ticker = 0
		self.state = '.'

	def setBomb(self):
		self.state = 'O'
(R, C, N) = [int(x) for x in input().strip().split(' ')]
board = Board(R, C)
for r in range(R):
	s = input().strip()
	for c in enumerate(s):
		board.board[r][c[0]] = Square(c[1], r, c[0], board)
t = 1
N = min(N, N % 4 + 4)
while t <= N:
	board.update()
	if t % 2 == 0:
		board.fillBombs()
	t += 1
print(board)


import sys
import copy

def print_grid(grid):
	transform_internal_grid_to_external_format(grid)
	for line_chars in grid:
		print(''.join(line_chars))

def transform_input_grid_to_internal_format(grid):
	for i in range(0, len(grid)):
		for j in range(0, len(grid[i])):
			if grid[i][j] == 'O':
				grid[i][j] = 3
			else:
				grid[i][j] = 0

def transform_internal_grid_to_external_format(grid):
	for i in range(0, len(grid)):
		for j in range(0, len(grid[i])):
			if grid[i][j] > 0:
				grid[i][j] = 'O'
			else:
				grid[i][j] = '.'

def read_input():
	firstline = sys.stdin.readline()
	splitted = firstline.split()
	(r, c, n) = (int(splitted[0]), int(splitted[1]), int(splitted[2]))
	grid = []
	for i in range(0, r):
		grid.append(list(sys.stdin.readline().rstrip('\n')))
	transform_input_grid_to_internal_format(grid)
	return (r, c, n, grid)

def explode_cell_unconditionally(grid, row, column):
	grid[row][column] = 0

def explode_cell_if_cell_exists_and_no_ready_bomb(grid, row, column):
	if row < len(grid) and row >= 0 and (column < len(grid[row])) and (column >= 0):
		if not grid[row][column] == 1:
			grid[row][column] = 0

def explode_bomb_at_coordinate(grid, row, column):
	explode_cell_unconditionally(grid, row, column)
	explode_cell_if_cell_exists_and_no_ready_bomb(grid, row - 1, column)
	explode_cell_if_cell_exists_and_no_ready_bomb(grid, row + 1, column)
	explode_cell_if_cell_exists_and_no_ready_bomb(grid, row, column - 1)
	explode_cell_if_cell_exists_and_no_ready_bomb(grid, row, column + 1)

def explode_necessary_bombs(grid):
	for i in range(0, len(grid)):
		for j in range(0, len(grid[i])):
			if grid[i][j] == 1:
				explode_bomb_at_coordinate(grid, i, j)

def fill_the_blanks(grid):
	for i in range(0, len(grid)):
		for j in range(0, len(grid[i])):
			if grid[i][j] == 0:
				grid[i][j] = 3

def decrease_times(grid):
	for i in range(0, len(grid)):
		for j in range(0, len(grid[i])):
			if grid[i][j] > 0:
				grid[i][j] -= 1

def run_simulation(r, c, n, grid):
	blank_fill_mode = True
	t = 0
	explode_necessary_bombs(grid)
	t += 1
	explode_necessary_bombs(grid)
	decrease_times(grid)
	if n == 1:
		return
	t += 1
	explode_necessary_bombs(grid)
	decrease_times(grid)
	fill_the_blanks(grid)
	if n == 2:
		return
	t += 1
	explode_necessary_bombs(grid)
	decrease_times(grid)
	if n == 3:
		return
	while t < n:
		t += 1
		explode_necessary_bombs(grid)
		decrease_times(grid)
		if blank_fill_mode:
			fill_the_blanks(grid)
		blank_fill_mode = not blank_fill_mode

def run_simulation_from_time(r, c, n, grid, t):
	blank_fill_mode = False
	while t < n:
		t += 1
		explode_necessary_bombs(grid)
		decrease_times(grid)
		if blank_fill_mode:
			fill_the_blanks(grid)
		blank_fill_mode = not blank_fill_mode

def print_internal_grid(grid):
	[print(''.join([str(item) for item in line])) for line in grid]
	print('')
(r, c, n, grid) = read_input()
if n <= 8:
	run_simulation(r, c, n, grid)
else:
	run_simulation(r, c, 8, grid)
	run_simulation_from_time(r, c, n, grid, n - n % 4)
print_grid(grid)


class Board(object):

	def __init__(self, rows, columns):
		self.board = [[None for column in range(columns)] for row in range(rows)]
		self.explosion_queue = []

	def __str__(self):
		s = ''
		for row in self.board:
			s += ''.join([sq.state for sq in row]) + '\n'
		return s

	def enqueue_explosion(self, position):
		self.explosion_queue.append(position)
		self.explosion_queue.append((min(position[0] + 1, len(self.board) - 1), position[1]))
		self.explosion_queue.append((max(position[0] - 1, 0), position[1]))
		self.explosion_queue.append((position[0], min(position[1] + 1, len(self.board[0]) - 1)))
		self.explosion_queue.append((position[0], max(position[1] - 1, 0)))

	def fillBombs(self):
		for row in self.board:
			for sq in row:
				sq.setBomb()

	def explode(self):
		for pos in set(self.explosion_queue):
			self.board[pos[0]][pos[1]].explode()
		self.explosion_queue.clear()

	def update(self):
		for row in self.board:
			for sq in row:
				sq.tickUp()
		if self.explosion_queue:
			self.explode()

class Square(object):

	def __init__(self, state, row, col, board):
		self.state = state
		self.ticker = 0
		self.position = (row, col)
		self.board = board

	def resetSquare(self):
		self.ticker = 0

	def tickUp(self):
		if self.state == '.':
			return
		self.ticker += 1
		if self.ticker == 3:
			board.enqueue_explosion(self.position)

	def explode(self):
		self.ticker = 0
		self.state = '.'

	def setBomb(self):
		self.state = 'O'
(R, C, N) = [int(x) for x in input().strip().split(' ')]
board = Board(R, C)
for r in range(R):
	s = input().strip()
	for c in enumerate(s):
		board.board[r][c[0]] = Square(c[1], r, c[0], board)
t = 1
N = min(N, N % 4 + 4)
while t <= N:
	board.update()
	if t % 2 == 0:
		board.fillBombs()
	t += 1
print(board)