program project1;
var name,b,s:array[0..101] of ansistring;
tot,h:array[0..101] of longint;
n,t:longint;
procedure init;
var i,j:longint;
begin
assign(input,'publish.in');reset(input);
assign(output,'publish.out');rewrite(output);
readln(n);
for i:=1 to n do readln(name[i]);
for i:=1 to n do readln(s[i]);
fillchar(tot,sizeof(tot),0);
for i:=1 to n do
for j:=1 to n do
if s[j][i]='1' then inc(tot[i]);
t:=0;
end;
function find(x:longint):boolean;
var i:longint;
begin
for i:=1 to t do
if b[i]=name[x] then exit(true);
exit(false);
end;
procedure main;
var i,j,k,temp,ttemp,y:longint;ss:ansistring;
begin
for i:=1 to n do
if not find(i) then
begin
inc(t);
b[t]:=name[i];
end;
for i:=1 to t do
begin
temp:=0;
for j:=1 to n do if name[j]=b[i] then inc(temp);
for j:=1 to temp do
begin
ttemp:=0;
for k:=1 to n do if name[k]=b[i] then
if tot[k]>=j then inc(ttemp);
if ttemp>=j then h[i]:=j else break;
end;
end;
for i:=1 to t do
for j:=i+1 to t do if (h[i]<h[j])or(h[i]=h[j])and(b[i]>b[j]) then
begin
ss:=b[i];b[i]:=b[j];b[j]:=ss;
y:=h[i];h[i]:=h[j];h[j]:=y;
end;
for i:=1 to t do
writeln(b[i],' ',h[i]);
close(input);
close(output);
end;
begin
init;
main;
end.