// Arup Guha
// 4/10/2021
// Solution to 2021 USACO April Bronze Problem: Acowdemia II

import java.util.*;

public class b {

	public static void main(String[] args) {
	
		// Get basic data.
		Scanner stdin = new Scanner(System.in);
		int numP = stdin.nextInt();
		int n = stdin.nextInt();
		
		// Store names and map names to their index.
		HashMap<String,Integer> nameMap = new HashMap<String,Integer>();
		String[] names = new String[n];
		for (int i=0; i<n; i++) {
			names[i] = stdin.next();
			nameMap.put(names[i], i);
		}
			
		// Read in the paper authors in order.
		String[][] papers = new String[numP][n];
		for (int i=0; i<numP; i++)
			for (int j=0; j<n; j++)
				papers[i][j] = stdin.next();
				
		// Default answers.
		char[][] res = new char[n][n];
		for (int i=0; i<n; i++) {
			Arrays.fill(res[i], '?');
			res[i][i] = 'B';
		}
		
		// Go through each paper for a clue.
		for (int i=0; i<numP; i++) {
		
			// A cut point is where a later names comes before an earlier name.
			ArrayList<Integer> cut = new ArrayList<Integer>();
			for (int j=0; j<n-1; j++)
				if (papers[i][j].compareTo(papers[i][j+1]) > 0)
					cut.add(j+1);
				
			// Go through each group.
			for (int x=0; x<cut.size(); x++) {
			
				// Get the range of this group.
				int next = x == cut.size()-1 ? n : cut.get(x+1);
				int start = cut.get(x);
				
				// All people in index after are more senior to people in index before.
				for (int after=start; after<next; after++) {
					for (int before=0; before<start; before++) {
						int oldIdx = nameMap.get(papers[i][after]);
						int newIdx = nameMap.get(papers[i][before]);
						res[oldIdx][newIdx] = '1';
						res[newIdx][oldIdx] = '0';
					}
				}
			}
		}
		
		// Ta da!
		for (int i=0; i<n; i++)
			System.out.println(new String(res[i]));
	}
}