var
a,b,c,d,n,y:longint;
s,x:string;
o,w:array[0..30]of longint;
k,l:array[0..30]of string;
begin
assign(input,'publish.in');
assign(input,'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
w[a]:=1000;
for a:=1 to n do
for b:=1 to n do
if (k[a]=k[b])and(o[b]<=w[a]) then
w[a]:=o[b];
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.