1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <bits/stdc++.h>
using namespace std;

#define fru(j,n) for(int j=0; j<(n); ++j)
#define tr(it,v) for(auto it=(v).begin(); it!=(v).end(); ++it)
#define x first
#define y second
#define pb push_back
#define mp make_pair
#define ALL(G) (G).begin(),(G).end()

typedef long long ll;
typedef double D;
typedef pair<int,int> pii;
typedef vector<int> vi;

const int inft = 1000000009;
const int MAXN = 205,BASE=50,MAXV=1000006;

char str[MAXN][MAXN];
struct maska{
	ll a[4];
	maska(){fru(i,4)a[i]=0;}
	void mark(int j){a[j/BASE]+=1LL<<(j%BASE);}
	bool get(int j){return a[j/BASE]&(1LL<<(j%BASE));}
	struct maska operator=(struct maska A){
		fru(i,4)a[i]=A.a[i];
	}
};
struct maska oruj(struct maska a1,struct maska a2){
	struct maska c;
	fru(i,4)c.a[i]=a1.a[i]| a2.a[i];
	return c;
}
struct maska M[MAXN];
void solve() {
	int n,b,r;
	scanf("%d%d%d",&n,&b,&r);
	fru(i,n){
		scanf("%s",str[i]);
	}
	fru(i,n)fru(j,n)if(str[i][j]=='1')M[i].mark(j);
	int czas=0;
	struct maska wyn;
	fru(i,r){
		int a;
		scanf("%d",&a);
		wyn.mark(a-1);
	}
	while(czas<MAXV){
	//	printf("czas %d\n",czas);
	//	fru(i,n)if(wyn.get(i))printf("%d ",i+1);puts("");
		bool ok=1;
		for(int b1=b;b1<n;b1++)if(wyn.get(b1)){ok=0;break;}
		if(ok){printf("%d\n",czas);return ;}
		struct maska temp;
		fru(i,n)if(wyn.get(i)){
			temp=oruj(temp,M[i]);
		}
		wyn=temp;
		czas++;
	}
	puts("-1");
}

int main() {
	solve();
	return 0;
}