var i,j,k,n,temp,h,pp,flag:longint;
mark,t,p,q:array[1..100] of longint;
s:array[1..1000] of string;
a,b:array[1..100] of string;
temp1:string;
begin
assign(input,'publish.in');
reset(input);
assign(output,'publish.out');
rewrite(output);
readln(n);
for i:=1 to n do
begin
readln(a[i]);
s[i]:=a[i];
end;
for i:=1 to n do
begin
readln(b[i]);
for j:=1 to n do
if (b[i,j]='1')
then inc(t[j]);
end;
for i:=1 to n do
begin
for j:=0 to 100 do
begin
h:=0;
for k:=1 to n do
if (a[k]=a[i])and(t[k]>=j)
then inc(h);
if (h>=j)
then p[i]:=j;
end;
end;
for i:=1 to n do
for j:=i+1 to n do
if (p[i]<p[j])or((p[i]=p[j])and(a[i]>a[j]))
then begin
temp:=p[i];
p[i]:=p[j];
p[j]:=temp;
temp1:=a[i];
a[i]:=a[j];
a[j]:=temp1;
end;
pp:=0;
for i:=1 to n do
begin
flag:=0;
for j:=1 to i-1 do
if a[i]=a[j]
then begin
flag:=1;
q[i]:=q[j];
break;
end;
if flag=0
then begin
inc(pp);
q[i]:=pp;
end;
end;
fillchar(mark,sizeof(mark),0);
for i:=1 to n do
if mark[q[i]]<>1
then begin
writeln(a[i],' ',p[i]);
mark[q[i]]:=1;
end;
close(input);
close(output);
end.