import java.util.*;
import java.io.*;
class D{
static int []p,res[];
static int S,T,f,nodes;
static int INF=(int)1e9;
static StringBuilder sb;
static void augment(int u,int minEdge)
{
if(u==S)
{
f= minEdge;
return;
}
int v=p[u];
if(v!=-1)
{
augment
(v,
Math.
min(minEdge, res
[v
][u
]));
res[u][v]+=f;
res[v][u] -= f;
}
}
static int print(int u)
{
if(u==T)
return 0;
int ans=0;
for(int v=0;v<nodes;v++)
if(p[u]==v)
{
if(v!=T && u!=S) {
sb.append(u+" "+v+"\n");
ans++;
}
ans+=print(v);
res[v][u]--;
break;
}
return ans;
}
{
Scanner sc
=new Scanner
(System.
in); int n=sc.nextInt();
int k=sc.nextInt();
nodes=n+2;
res=new int [nodes][nodes];
char [][]s=new char [n][n];
int []b=new int [k];
S=0;
T=nodes-1;
for(int i=0;i<k;i++)
res[S][sc.nextInt()]=1;
for(int i=0;i<k;i++)
{
int x=sc.nextInt();
res[x][T]=1;
b[i]=x;
}
for(int i=1;i<=n;i++)
{
s[i-1]=sc.next().toCharArray();
for(int j=1;j<=n;j++)
if(s[i-1][j-1]=='1')
res[i][j]=1;
}
p=new int [nodes];
int mf=0;
while(true)
{
int []dist=new int [nodes];
f=0;
dist[S]=0;
q.add(S);
while(!q.isEmpty())
{
int u=q.poll();
if(u==T)
break;
for(int v=0;v<nodes;v++)
if(dist[v]==-1 && res[u][v]>0)
{
dist[v]=dist[u]+1;
p[v]=u;
q.add(v);
}
}
augment(T, INF);
if(f==0)
break;
else
mf+=f;
}
if(mf<k)
out.println("NO");
else
{
sb=new StringBuilder();
out.println("YES");
int ans=0;
while(k-->0)
{
q.add(T);
int []dist=new int [nodes];
p=new int[nodes];
dist[T]=0;
while(!q.isEmpty())
{
int u=q.poll();
for(int v=0;v<nodes;v++)
if(res[u][v]>0 && dist[v]==-1)
{
q.add(v);
p[v]=u;
dist[v]=dist[u]+1;
}
}
ans+=print(S);
}
out.println(ans);
out.println(sb);
}
out.close();
}
static class Scanner {
}
}
while (st == null || !st.hasMoreTokens())
return st.nextToken();
}
}
return Long.
parseLong(next
()); }
return br.readLine();
}
public boolean ready
() throws IOException {return br.
ready();}
}
}
aW1wb3J0IGphdmEudXRpbC4qOwppbXBvcnQgamF2YS5pby4qOwpjbGFzcyBEewoKCXN0YXRpYyBpbnQgW11wLHJlc1tdOwoJc3RhdGljIGludCBTLFQsZixub2RlczsKCXN0YXRpYyBpbnQgSU5GPShpbnQpMWU5OwoJc3RhdGljIFByaW50V3JpdGVyIG91dDsKCXN0YXRpYyBTdHJpbmdCdWlsZGVyIHNiOwoJc3RhdGljIHZvaWQgYXVnbWVudChpbnQgdSxpbnQgbWluRWRnZSkKCXsKCQkKCQlpZih1PT1TKSAKCQl7CgkJCWY9IG1pbkVkZ2U7CgkJCXJldHVybjsKCQl9CiAKCQlpbnQgdj1wW3VdOwogCgkJaWYodiE9LTEpCgkJewoJCQlhdWdtZW50KHYsTWF0aC5taW4obWluRWRnZSwgcmVzW3ZdW3VdKSk7CiAKCQkJcmVzW3VdW3ZdKz1mOwoJCQlyZXNbdl1bdV0gLT0gZjsKIAoJCX0KIAoJfQoJc3RhdGljIGludCBwcmludChpbnQgdSkKCXsKCQkKCQkKCQlpZih1PT1UKQoJCQlyZXR1cm4gMDsKCQlpbnQgYW5zPTA7CgkJZm9yKGludCB2PTA7djxub2Rlczt2KyspCgkJCWlmKHBbdV09PXYpCgkJCXsKCQkJCQoJCQkJaWYodiE9VCAmJiB1IT1TKSB7CgkJCQkJc2IuYXBwZW5kKHUrIiAiK3YrIlxuIik7CgkJCQkJYW5zKys7CgkJCQl9CgkJCQkKCQkJCWFucys9cHJpbnQodik7CgkJCQkKCQkJCXJlc1t2XVt1XS0tOwoJCQkJYnJlYWs7CgkJCX0KCQlyZXR1cm4gYW5zOwoJfQoJCglwdWJsaWMgc3RhdGljIHZvaWQgbWFpbihTdHJpbmdbXSBhcmdzKSB0aHJvd3MgSU9FeGNlcHRpb24gCgl7CgkJCgkJU2Nhbm5lciBzYz1uZXcgU2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AgQnVmZmVyZWRSZWFkZXIgYnI7CiAKICAgICAgICBwdWJsaWMgU2Nhbm5lcihJbnB1dFN0cmVhbSBzKSB7CiAgICAgICAgICAgIGJyID0gbmV3IEJ1ZmZlcmVkUmVhZGVyKG5ldyBJbnB1dFN0cmVhbVJlYWRlcihzKSk7CiAgICAgICAgfQogCiAgICAgICAgcHVibGljIFNjYW5uZXIoRmlsZVJlYWRlciBzKSB7CiAgICAgICAgICAgIGJyID0gbmV3IEJ1ZmZlcmVkUmVhZGVyKHMpOwogICAgICAgIH0KIAogICAgICAgIHB1YmxpYyBTdHJpbmcgbmV4dCgpIHRocm93cyBJT0V4Y2VwdGlvbiB7CiAgICAgICAgICAgIHdoaWxlIChzdCA9PSBudWxsIHx8ICFzdC5oYXNNb3JlVG9rZW5zKCkpCiAgICAgICAgICAgICAgICBzdCA9IG5ldyBTdHJpbmdUb2tlbml6ZXIoYnIucmVhZExpbmUoKSk7CiAgICAgICAgICAgIHJldHVybiBzdC5uZXh0VG9rZW4oKTsKICAgICAgICB9CiAKICAgICAgICBwdWJsaWMgaW50IG5leHRJbnQoKSB0aHJvd3MgSU9FeGNlcHRpb24gewogICAgICAgICAgICByZXR1cm4gSW50ZWdlci5wYXJzZUludChuZXh0KCkpOwogICAgICAgIH0KIAogICAgICAgIHB1YmxpYyBsb25nIG5leHRMb25nKCkgdGhyb3dzIElPRXhjZXB0aW9uIHsKICAgICAgICAgICAgcmV0dXJuIExvbmcucGFyc2VMb25nKG5leHQoKSk7CiAgICAgICAgfQogCiAgICAgICAgcHVibGljIFN0cmluZyBuZXh0TGluZSgpIHRocm93cyBJT0V4Y2VwdGlvbiB7CiAgICAgICAgICAgIHJldHVybiBici5yZWFkTGluZSgpOwogICAgICAgIH0KICAgICAgICBwdWJsaWMgYm9vbGVhbiByZWFkeSgpIHRocm93cyBJT0V4Y2VwdGlvbiB7cmV0dXJuIGJyLnJlYWR5KCk7fQogICAgICAgIHB1YmxpYyBkb3VibGUgbmV4dERvdWJsZSgpIHRocm93cyBJT0V4Y2VwdGlvbiB7cmV0dXJuIERvdWJsZS5wYXJzZURvdWJsZShuZXh0KCkpO30KICAgICAgIAogICAgfQoKfQ==