var a,b:array[0..110]of string;
s:array[0..110,0..110]of longint;
f,p,d:array[0..110]of longint;
n,m,i,j:longint;
s1:string;
function find(s:string):longint;
begin
for j:=1 to m do if b[j]=s then exit(j);
inc(m); b[m]:=s; exit(m);
end;
procedure init;
begin
readln(n); m:=0;
for i:=1 to n do readln(a[i]);
for i:=1 to n do p[i]:=find(a[i]);
for i:=1 to n do
begin
readln(s1);
for j:=1 to length(s1) do if s1[j]='1' then inc(s[p[j],j]);
end;
for i:=1 to m do
begin
fillchar(d,sizeof(d),0);
for j:=1 to n do inc(d[s[i,j]]);
for j:=n downto 1 do d[j]:=d[j]+d[j+1];
for j:=1 to n do if d[j]>=j then f[i]:=j;
end;
end;
procedure work;
begin
for i:=1 to m do
for j:=i+1 to m do if (f[i]<f[j])or(f[i]=f[j])and(b[i]>b[j]) then
begin
n:=f[i]; f[i]:=f[j]; f[j]:=n;
s1:=b[i]; b[i]:=b[j]; b[j]:=s1;
end;
for i:=1 to m do writeln(b[i],' ',f[i]);
end;
begin
assign(input,'publish.in');reset(input);
assign(output,'publish.out');rewrite(output);
init;
work;
close(input);close(output);
end.