> File Name: 5811.cpp
> Author: jiangyuzhu
> Mail: 834138558@qq.com
> Created Time: 2016/8/17 11:19:57
************************************************************************/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<algorithm>
using namespace std;
const int maxn = 1e3 + 5, oo = 0x3f3f3f3f;
int a[2][maxn];
bool vis[maxn];
int mp[maxn][maxn];
int n, m;
struct EDGE{
int to, next;
}edge[maxn * maxn];
int head[maxn];
int tid[2][maxn];
int tot;
int in[maxn];
int dp[maxn];
int b[maxn];
void init()
{
memset(in, 0, sizeof(in));
memset(head, -1, sizeof(head));
tot = 0;
}
void addedge(int from, int to)
{
in[to]++;
edge[tot].to = to;
edge[tot].next = head[from];
head[from] = tot++;
}
bool Topology(int k)
{
int tp = 0;
queue<int>q;
for(int i = 0; i < n; i++){
if(in[i] == 0 && vis[i] != k){
q.push(i);
}
}
while(!q.empty()){
int u = q.front();q.pop();
tid[k][tp++] = u;
for(int i = head[u]; i != -1; i = edge[i].next){
in[edge[i].to]--;
if(!in[edge[i].to]) q.push(edge[i].to);
}
}
if(k) return tp == n - m;
else return tp == m;
}
char s[maxn];
int main (void)
{
while(~scanf("%d%d", &n, &m) && (n + m)){
init();
getchar();
for(int i = 0; i < n; i++){
gets(s);
for(int j = 0; j < 2 * n; j += 2){
mp[i][j / 2] = s[j] - '0';
}
vis[i] = false;
dp[i] = oo;
}
int tota = 0, totb = 0;
int x;
for(int i = 0; i < m; i++){
scanf("%d", &x);x--;
a[0][tota++] = x;
vis[x] = true;
}
for(int i = 0; i < n; i++){
if(!vis[i]){
a[1][totb++] = i;
}
}
for(int i = 0; i < n; i++){
for(int j = 0 ; j < n; j++){
if(mp[i][j] && vis[i] == vis[j]){
addedge(j, i);
}
}
}
if(!Topology(0) || !Topology(1)){
puts("NO");
continue;
}
int ans;
for(int i = 0; i < n - m; i++){
ans = -1;
for(int j = 0; j < m; j++){
if(mp[tid[0][j]][tid[1][i]]){
ans = j;
break;
}
}
if(ans == -1) ans = m;
for(int j = ans + 1; j < m; j++){
if(mp[tid[1][i]][tid[0][j]]){
ans = -1;
break;
}
}
if(ans == -1) continue;
*upper_bound(dp, dp + n - m, ans) = ans;
}
printf("YES %d\n", lower_bound(dp, dp + n - m, oo) - dp);
}
return 0;
}