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
#include<bits/stdc++.h>
#define PII pair<int,int>
#define f first
#define s second
#define VI vector<int>
#define LL long long
#define MP make_pair
#define LD long double
#define PB push_back
#define ALL(V) V.begin(),V.end()
#define abs(x) max((x),-(x))
#define PDD pair<LD,LD> 
#define VPII vector< PII > 
#define siz(V) ((int)V.size())
#define FOR(x, b, e)  for(int x=b;x<=(e);x++)
#define FORD(x, b, e) for(int x=b;x>=(e);x--)
#define REP(x, n)     for(int x=0;x<(n);x++)
#define mini(a,b) a=min(a,b)
#define maxi(a,b) a=max(a,b)
using namespace std;
int n,a,b,c,d,k,m,q,z;
char ch;
const int S=201;
bitset<S> start,x;
bitset<S> in[202];
main()
{
scanf("%d%d%d",&n,&m,&q);
REP(i,n)
	{
	REP(j,n)
		{
		scanf(" %c",&ch);
		in[i][j]=ch=='1';
		}
	}
REP(i,q)
	{
	scanf("%d",&a);
	start[a-1]=1;
	}

int X=1000000*30/pow(n,0.6)+3;
REP(i,X)
	{
//	cerr<<i<<" "<<start<<endl;
	x.reset();
	bool ok=1;
	FOR(i,m,n-1)
		{
		if(start[i]){ok=0;break;}
		}
	if(ok)
		{
		printf("%d\n",i);
		return 0;
		}
	REP(i,n)
		{
		if(start[i])x|=in[i];
		}	
	start=x;
	}

puts("-1");
}