/*
	© MATTO MATTI 2016
	http://mattomatti.com/pl/a35f
	napisane przy użyciu Visual Studio Community 2015
	2016-02-21 v 1.0
*/

#include <iostream>

using namespace std;

char getLeftCharFromTable(char lookfor, char** tablica, int width, int height){
	for(int x = 0; x < height; x++)
		for(int y = 0; y < width; y++)
			if(tablica[x][y] == lookfor)
				return tablica[x][(y == 0) ? width - 1 : y - 1];
	return lookfor;
}

char* cipher(char* txt, char** tablica, int width, int height){
	char* wynik = new char[strlen(txt)];
	for(int i = 0; wynik[i]; i++)
		wynik[i] = getLeftCharFromTable(txt[i], tablica, width, height);
	wynik[strlen(txt)] = '\0';
	return wynik;
}

char getRightCharFromTable(char lookfor, char** tablica, int width, int height){
	for(int x = 0; x < height; x++)
		for(int y = 0; y < width; y++)
			if(tablica[x][y] == lookfor)
				return tablica[x][(y == width - 1) ? 0 : y + 1];
	return lookfor;
}

char* decipher(char* txt, char** tablica, int width, int height){
	char* wynik = new char[strlen(txt)];
	for(int i = 0; wynik[i]; i++)
		wynik[i] = getRightCharFromTable(txt[i], tablica, width, height);
	wynik[strlen(txt)] = '\0';
	return wynik;
}

int main () {
	int w, h;
	cin >> w >> h;
	char** tablica = new char*[h];
	for(int i = 0; i < h; i++){
		tablica[i] = new char[w];
		for(int j = 0; j < w; j++)
			cin >> tablica[i][j];
	}
	char* txt = new char[128];
	cin.sync();
	cin.getline(txt, 128);
	char* txtc = cipher(txt, tablica, w, h);
	cout << txtc << endl;
	char* txtd = decipher(txtc, tablica, w, h);
	cout << txtd << endl;
	for(int i = 0; i < h; i++)
		delete[] tablica[i];
	delete[] tablica, txtc, txtd;
	system("pause");
	return 0;
}