/*
I. 331.
Uzonyi 000 Akos 8. évf.
Budapest, Budai Ciszterci Szent Imre Gimnázium
uzonyi.akos@gmail.com
*/

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.List;
import java.util.Scanner;
import java.util.Vector;

public class I334
{
	public static void main(String[] args) throws IOException
	{
		Scanner fileIn = new Scanner(new File(args[0]));
		int n = fileIn.nextInt();
		int k = fileIn.nextInt() - 1;
		fileIn.nextLine();
		char[] data = new char[n * n];
		int index = 0;
		for (int i = 0; i < n; i++)
		{
			String line = fileIn.nextLine();
			for (char c : line.toCharArray())
			{
				data[index++] = c;
			}
		}
		Table t = new Table(n, k, data);
		for (char c : fileIn.nextLine().toCharArray())
		{
			t.action(c);
		}
		fileIn.close();
		
		PrintStream ps = new PrintStream(new FileOutputStream(new File(args[1])));
		t.printData(ps);
		ps.close();
	}
}

class Table
{
	private char[][] content;
	private int size;
	private int holePos; //the x coordinate of hole, when it is faced down
	private int holeSide = 0; //the current side index, that contains the hole (bottom = 0)
	
	public Table(int size, int holePos, char[] data)
	{
		this.size = size;
		this.holePos = holePos;
		content = new char[size][size];
		for (int x = 0; x < size; x++)
		{
			for (int y = 0; y < size; y++)
			{
				content[x][y] = data[x + y * size];
			}
		}
	}
	
	public void action(char c)
	{
		char[][] newContent = new char[size][size];
		if (c == 'B')
		{
			++holeSide;
		}
		else if (c == 'J')
		{
			--holeSide;
		}
		else if (c == 'F')
		{
			holePos = size - holePos - 1;
			holeSide = holeSide == 0 ? 2 : (holeSide == 2 ? 0 : holeSide);
		}
		while (holeSide < 0) holeSide += 4;
		while (holeSide >= 4) holeSide -= 4;
		
		for (int x = 0; x < size; x++)
		{
			for (int y = 0; y < size; y++)
			{
				if (c == 'J')
				{
					newContent[size - y - 1][x] = content[x][y];
				}
				else if (c == 'B')
				{
					newContent[y][size - x - 1] = content[x][y];
				}
				else if (c == 'F')
				{
					newContent[x][size - y - 1] = content[x][y];
				}
				else
				{
					throw new IllegalArgumentException();
				}
			}
		}
		content = newContent;
		applyGravitiy();
	}

	private void applyGravitiy()
	{
		for (int x = 0; x < size; x++)
		{
			if (holeSide == 0 && x == holePos)
			{
				for (int i = 0; i < size; i++)
				{
					content[x][i] = '.';
				}
			}
			else
			{
				List<Character> chars = new Vector<Character>();
				for (char c : content[x])
				{
					if (c != '.') chars.add(0, c);
				}
				content[x] = new char[size];
				for (int i = 0; i < size; i++)
				{
					content[x][size - i - 1] = i < chars.size() ? chars.get(i) : '.';
				}
			}
		}
	}
	
	public void printData(PrintStream ps)
	{
		int p = 0;
		int k = 0;
		int z = 0;
		for (int x = 0; x < size; x++)
		{
			for (int y = 0; y < size; y++)
			{
				char c = content[x][y];
				if (c == 'P')
				{
					++p;
				}
				else if (c == 'K')
				{
					++k;
				}
				else if (c == 'Z')
				{
					++z;
				}
			}
		}
		ps.println("Piros: " + p + " darab");
		ps.println("Kek: " + k + " darab");
		ps.println("Zold: " + z + " darab");
		for (int y = 0; y < size; y++)
		{
			for (int x = 0; x < size; x++)
			{
				ps.print(content[x][y]);
			}
			ps.println();
		}
	}
}