var
a,b,c,d,n,y,z,h:longint;
s,x:string;
o,w,v:array[0..300]of longint;
k,l:array[0..300]of string;
begin
assign(input,'publish.in');
assign(output,'publish.out');
reset(input);
rewrite(output);
readln(n);
for a:=1 to n do
readln(k[a]);
for a:=1 to n do
readln(l[a]);
for a:=1 to n do
begin
s:=l[a];
for b:=1 to n do
if (s[b]='1')and(a<>b) then
o[b]:=o[b]+1;
end;
for a:=1 to n do
for h:=n downto 0 do
begin
z:=0;
for b:=1 to n do
if k[a]=k[b] then
if o[b]>=h then
z:=z+1;
if z>=h then
begin
w[a]:=h;
break;
end;
end;
for a:=1 to n-1 do
for b:=a+1 to n do
if (w[a]>w[b])or((w[a]=w[b])and(k[a]<k[b])) then
begin
x:=k[a];
k[a]:=k[b];
k[b]:=x;
y:=w[a];
w[a]:=w[b];
w[b]:=y;
end;
s:='oooooooo';
for a:=n downto 1 do
if k[a]<>s then
begin
s:=k[a];
writeln(k[a],' ',w[a])
end;
close(input);
close(output);
end.