type ar=record
a:array[0..100] of longint;
names:string;
h:longint;
end;
var ed:array[1..100] of ar;
atr:array[1..100] of longint;
s:string;
x,i,n,j,k:longint;
c:char;
procedure editor(s:string);
var i:longint;
begin
for i:=1 to x do
if ed[i].names=s then begin inc(ed[i].a[0]); ed[i].a[ed[i].a[0]]:=j; exit; end;
inc(x); ed[x].names:=s; ed[x].a[0]:=1; ed[x].a[ed[x].a[0]]:=j;
exit;
end;
procedure swap(x,y:longint);
var temp:longint;
begin temp:=x; x:=y; y:=temp; end;
procedure qsort(l,r:longint);
var mid,i,j:longint;
temp:ar;
begin
mid:=ed[(l+r) div 2].h;
i:=l; j:=r;
repeat
while ed[i].h>mid do inc(i);
while ed[j].h<mid do dec(j);
if i<=j then begin
if ed[i].h=ed[j].h then
begin
if ed[i].names>ed[j].names then
begin temp:=ed[i]; ed[i]:=ed[j]; ed[j]:=temp; end; end else begin
temp:=ed[i]; ed[i]:=ed[j]; ed[j]:=temp;
end;
inc(i); dec(j);
end;
until i>j;
if l<j then qsort(l,j);
if i<r then qsort(i,r);
end;
begin
assign(input,'publish.in'); reset(input);
assign(output,'publish.out'); rewrite(output);
readln(n);
for j:=1 to n do
begin
readln(s);
editor(s);
end;
fillchar(atr,sizeof(atr),0);
for i:=1 to n do
begin
for j:=1 to n do
begin
read(c);
if c='1' then inc(atr[j]);
end;
readln;
end;
for k:=1 to x do
for i:=1 to ed[k].a[0]-1 do
for j:=1 to ed[k].a[0] do
if atr[ed[k].a[i]]<atr[ed[k].a[j]] then swap(ed[k].a[i],ed[k].a[j]);
for k:=1 to x do
begin
i:=1;
while i<atr[ed[k].a[i]] do inc(i);
if i=1 then if atr[ed[k].a[1]]=0 then i:=0;
ed[k].h:=i;
end;
qsort(1,x);
for i:=1 to x do
writeln(ed[i].names,' ',ed[i].h);
close(output);
end.