package manila; import java.io.*; import java.util.*; /** * @author Tomoya Taniguchi */ public class C { public static void main(String[] args)throws Exception { BufferedReader r = new BufferedReader(new FileReader("prince.in")); StringTokenizer st = new StringTokenizer(r.readLine(),","); int n = Integer.parseInt(st.nextToken()); int m = Integer.parseInt(st.nextToken()); String[] word = new String[n]; for(int i = 0;i < n;i++) word[i] = r.readLine(); char[][][] cube = new char[m][][]; for(int i = 0;i < m;i++) { cube[i] = new char[m][]; for(int j = 0;j < m;j++) cube[i][j] = new char[m]; } Hashtable ht = new Hashtable(); for(int i = 0;i < m;i++) { String line = r.readLine(); for(int j = 0;j < m;j++) { for(int k = 0;k < m;k++) { cube[i][j][k] = line.charAt((m+1)*j+k); Character ch = new Character(cube[i][j][k]); if(!ht.containsKey(ch)) ht.put(ch,new ArrayList()); ArrayList ar = (ArrayList)ht.get(ch); ar.add(new Point(i,j,k)); } } } for(int i = 0;i < n;i++) { if(word[i].length() >= m)break; ArrayList ar = (ArrayList)ht.get(new Character(word[i].charAt(0))); ArrayList ar2 = (ArrayList)ht.get(new Character(word[i].charAt(1))); if(ar == null || ar2 == null) continue; Iterator it = ar.iterator(); while(it.hasNext()) { Point p = (Point)it.next(); Iterator ite = ar2.iterator(); while(ite.hasNext()) { Point p2 = (Point)ite.next(); if(Point.distance(p,p2) <= 1) { int Point = p.x; int sx = p.x; int sy = p.y; int sz = p.z; int dx = p2.x - p.x; int dy = p2.y - p.y; int dz = p2.z - p.z; boolean flag; char w = ' '; if((sx - dx < 0 || m <= sx - dx) ||(sy - dy < 0 || m <= sy - dy) ||(sz - dz < 0 || m <= sz - dz)) flag = true; else { flag = false; w = cube[sx-dx][sy-dy][sz-dz]; } for(int j = 0;j < word[i].length();j++) { if(sx < 0 || m <= sx)break; if(sy < 0 || m <= sy)break; if(sz < 0 || m <= sz)break; if(word[i].charAt(j) != cube[sx][sy][sz]) break; sx += dx; sy += dy; sz += dz; if(j == word[i].length() - 1) { if(flag) { if(sx < 0 || m <= sx || sy < 0 || m <= sy || sz < 0 || m <= sz) break; System.out.print(cube[sx][sy][sz]); } else { System.out.print(w); } } } } } } } System.out.println(""); } } class Point { public int x; public int y; public int z; public Point(int x,int y,int z) { this.x = x; this.y = y; this.z = z; } public static int distance(Point a,Point b) { return Math.max(Math.max(Math.abs(a.x - b.x) ,Math.abs(a.y - b.y)),Math.abs(a.z - b.z)); } }