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
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#include <bits/stdc++.h>

using namespace std;

typedef long long LL;

template<typename TH>
void debug_vars(const char* sdata, TH head){
    cerr << sdata << "=" << head << "\n";
}

template<typename TH, typename... TA>
void debug_vars(const char* sdata, TH head, TA... tail){
    while(*sdata != ',') cerr << *sdata++;
    cerr << "=" << head << ",";
    debug_vars(sdata+1, tail...);
}

#ifdef LOCAL
#define debug(...) debug_vars(#__VA_ARGS__, __VA_ARGS__)
#else
#define debug(...) (__VA_ARGS__)
#endif

/////////////////////////////////////////////////////////


// ech, bignumy
struct BigNum {
    static constexpr int Base = 1000000000;
    static constexpr int Size = 16;
    int data[Size];

    BigNum(){ fill(data, data+Size, 0); }
    BigNum(string str){
        fill(data, data+Size, 0);
        int pos = 0, coef = 1;
        reverse(str.begin(), str.end());
        for(char ch : str){
            if(coef == Base){ pos++; coef = 1; }
            if(pos == 16) break;
            data[pos] += coef*(ch-'0');
            coef *= 10;
        }
    }
    
    BigNum& operator+=(const BigNum& other){
        int rem = 0;
        for(int i = 0; i < Size; i++){
            rem += data[i] + other.data[i];
            data[i] = rem % Base;
            rem /= Base;
        }
        return *this;
    }

    int operator%(int mod) const {
        int res = 0;
        for(int i = Size-1; i >= 0; i--){
            res = (int)(((LL)res*Base + data[i]) % mod);
        }
        return res;
    }

    friend ostream& operator<<(ostream& os, const BigNum& bn){
        bool first = true;
        for(int i = Size-1; i >= 0; i--){
            if(bn.data[i] == 0 && first) continue;
            if(first){
                os << bn.data[i];
            } else {
                os << setfill('0') << setw(9) << bn.data[i];
            }
            first = false;
        }
        return os;
    }
};



constexpr const int MaxN = 205;
const int GoodMod = 8 * 9 * 5 * 7 * 11 * 13;  /* 360360 */
const vector<pair<int,int>> primePowers = {
    {2,128}, {3,81}, {5,125}, {7,49}, {11,121}, {13,169}, {17,17}, {19,19},
    {23,23}, {29,29}, {31,31}, {37,37}, {41,41}, {43,43}, {47,47}, {53,53},
    {59,59}, {61,61}, {67,67}, {71,71}, {73,73}, {79,79}, {83,83}, {89,89},
    {97,97}, {101,101}, {103,103}, {107,107}, {109,109}, {113,113},
    {127,127}, {131,131}, {137,137}, {139,139}, {149,149}, {151,151},
    {157,157}, {163,163}, {167,167}, {173,173}, {179,179}, {181,181},
    {191,191}, {193,193}, {197,197}, {199,199}
};
const BigNum LCM200 =
    BigNum("337293588832926264639465766794841407432394382"
           "785157234228847021917234018060677390066992000");
const int BruteBound = 300000;


int N, B, R;
bitset<MaxN> data[MaxN];
bitset<MaxN> reachable[MaxN];
bitset<MaxN> starting, ending;
bool hasCycle[MaxN][MaxN];  // [v][l] = czy v ma cykl/marszrute dlugosci l?
int gcdCyc[MaxN];   // [v] = gcd wszystkich cykli w dwuspojnej z v
bool goodRemainder[MaxN][MaxN];  // [m][i] = czy (duzy) wynik moze przystawac do i mod m?
bitset<MaxN> afterBrute;

int gcd(int a, int b){
    while(b){
        a %= b;
        swap(a, b);
    }
    return a;
}

void input(){
    ios_base::sync_with_stdio(false);
    cin >> N >> B >> R;
    for(int i = 0; i < N; i++){
        string s;
        cin >> s;
        reverse(s.begin(), s.end());
        data[i] = bitset<MaxN>(s);
    }

    for(int i = 0; i < B; i++){
        ending[i] = true;
    }
    for(int i = 0; i < R; i++){
        int v; cin >> v;
        starting[v-1] = true;
    }
}

// Brutfors palujacy male przypadki
void bruteforce(){
    int numSteps = 0;
    bitset<MaxN> cur(starting);
    while((cur|ending) != ending){
        numSteps++;
        bitset<MaxN> newCur;
        for(int i = 0; i < N; i++){
            if(cur[i]) newCur |= data[i];
        }
        cur = newCur;
        if(numSteps == BruteBound){
            afterBrute = cur;
            return;
        }
    }
    cout << numSteps << endl;
    exit(0);
}


void reachability(int v){
    bitset<MaxN> a, b;
    a[v] = true;
    do {
        b = a;
        for(int i = 0; i < N; i++){
            if(b[i]) a |= data[i];
        }
    } while(a != b);
    reachable[v] = a;
}




void find_cycles(){
    vector<bitset<MaxN>> matrices[2];
    matrices[0].resize(N); matrices[1].resize(N);
    for(int i = 0; i < N; i++) matrices[0][i][i] = true;
    
    int now = 1, prv = 0;
    for(int len = 0; len <= N; len++){
        for(int i = 0; i < N; i++){
            hasCycle[i][len] = (matrices[prv][i][i]);
        }

        for(int i = 0; i < N; i++){
            matrices[now][i].reset();
        }

        for(int i = 0; i < N; i++){
            for(int j = 0; j < N; j++){
                if(matrices[prv][i][j]) matrices[now][i] |= data[j];
            }
        }

        swap(now, prv);
    }
}


void reachable_cycles(){
    for(int i = 0; i < N; i++){
        gcdCyc[i] = 0;
        for(int len = 1; len <= N; len++){
            if(hasCycle[i][len]) gcdCyc[i] = gcd(gcdCyc[i], len);
        }
    }

    for(int i = 0; i < N; i++){
        for(int j = 0; j < N; j++){
            if(reachable[i][j] && reachable[j][i]){
                gcdCyc[i] = gcd(gcdCyc[i], gcdCyc[j]);
            }
        }
    }

    for(int i = 0; i < N; i++){
        debug(i, gcdCyc[i]);
    }
}


void proc_possible_rems(int mod){
    bitset<MaxN> available, remStart;
    for(int i = 0; i < N; i++){
        if(gcdCyc[i] == 0 || mod % gcdCyc[i] == 0) available[i] = true;
    }
    remStart = (available & afterBrute);
    //debug(mod, available);

    vector<bitset<MaxN>> possibleMods(2*N+1);
    possibleMods[0] = remStart;

    for(int m = 0; m < 2*N; m++){
        for(int i = 0; i < N; i++){
            if(possibleMods[m][i]){
                possibleMods[m+1] |= (data[i] & available);
            }
        }
        //debug(possibleMods[m+1]);
    }

    for(int i = 0; i < mod; i++){
        int x = (BruteBound + 2*N-i) % mod;
        goodRemainder[mod][x] = ((possibleMods[2*N-i] & (~ending)).none());
    }
}


BigNum solve_ctr(vector<pair<int,int>> dat){
    BigNum res("0"), mod("1");

    for(auto P : dat){
        int m = P.first, a = P.second;
        int cur = res % m, incr = mod % m;
        debug(cur, incr, a, m);

        while(cur != a){
            res += mod;
            cur = (cur+incr) % m;
        }

        BigNum tmp(mod);
        for(int i = 0; i < m-1; i++) mod += tmp;
    }
    return res;
}


void try_take_remainder(int rem){  // reszta mod GoodMod = 360360
    vector<bitset<MaxN>> rems(N+1);
    for(int i = 0; i <= N; i++) rems[i].set();

    for(int mod = 1; mod <= N; mod++){
        // liczymy reszte wynikajaca 'z ustawy'
        int g = gcd(mod, GoodMod);
        int r = rem % g;
        if(!goodRemainder[g][r]) return;   // no nie da sie no
        
        // wyznaczamy potege liczby pierwszej "poza" GoodMod
        int d = mod / g, e;
        if(d == 1) continue;
        
        do {
            e = d;
            d = gcd(d*d, mod);
        } while(d != e);

        while(r < mod){
            if(!goodRemainder[mod][r]) rems[d][r%d] = false;
            r += g;
        }
    }
    debug(rem);

    vector<pair<int,int>> CTRMods;

    for(auto& ppow : primePowers){
        int p = ppow.first, pw = 1;
        while(pw*p <= N) pw *= p;
        int g = gcd(pw, GoodMod);

        bool anyFound = false;
        for(int r = 0; r < pw; r++){
            if(r%g != rem%g) continue;
            bool ok = true;
            int ttt = 1;
            while(ttt <= pw){
                if(!rems[ttt][r%ttt]){ ok = false; break; }
                ttt *= p;
            }
            if(ok){
                anyFound = true;
                CTRMods.emplace_back(pw, r);
                break;
            }
        }

        if(!anyFound) return;
    }

    cerr << "HAHA!" << endl;
    for(int i = 0; i < (int)CTRMods.size(); i++){
        cerr << CTRMods[i].first << ": " << CTRMods[i].second << endl;
    }

    BigNum res = solve_ctr(CTRMods);
    res += LCM200;
    cout << res << endl;

    exit(0);
}


int main(){
    input();
//#warning no bruteforce
    bruteforce();
    for(int i = 0; i < N; i++) reachability(i);
    find_cycles();
    reachable_cycles();
    for(int x = 1; x <= N; x++) proc_possible_rems(x);

    /*for(int mod = 1; mod <= N; mod++){
        printf("%d: ", mod);
        for(int r = 0; r < mod; r++){
            printf("%d", goodRemainder[mod][r] ? 1 : 0);
        }
        printf("\n");
    }*/

    for(int r = 0; r < GoodMod; r++){
        try_take_remainder(r);
    }

    cout << -1 << endl;
    return 0;
}