#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <map>
using namespace std;
string name[110];
int namenum,pnum[110],ppos[110][110],val[110];
map<string,int> ans;
map<string,int>::iterator iter,itere;
bool check(int namepos,int H)
{
int i,to,c=0;
for (i=0;i<pnum[namepos];i++)
{
to=ppos[namepos][i];
if (val[to]>=H)
c++;
}
if (c>=H)
return(1);
return(0);
}
int main(void)
{
freopen("publish.in","r",stdin);
freopen("publish.out","w",stdout);
int i,j,n,temp;
bool in;
string ch;
cin>>n;
for (i=1;i<=n;i++)
{
cin>>ch;
in=false;
for (j=0;j<namenum;j++)
{
if (name[j]==ch)
{
in=true;
ppos[j][pnum[j]++]=i;
}
}
if (!in)
{
name[namenum]=ch;
ppos[namenum++][pnum[namenum]++]=i;
}
}
for (i=1;i<=n;i++)
for (j=1;j<=n;j++)
{
scanf("%1d",&temp);
val[j]+=temp;
}
for (i=0;i<namenum;i++)
{
for (j=0;j<=pnum[i];j++)//calculate H
if (!check(i,j))
break;
ans[name[i]]=j-1;
}
itere=ans.end();
for (i=100;i>=0;i--)//mei ju H
for (iter=ans.begin();iter!=itere;iter++)
if ((*iter).second==i)
cout<<(*iter).first<<' '<<(*iter).second<<endl;
return(0);
}