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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
#include <stdint.h>
#include <cmath>
#include <csignal>
#include <bitset>
#include <map>
#include <vector>
#include <iostream>
#include <iomanip>
#include <sstream>

using namespace std;

typedef unsigned int uint;

#define self (*this)


class DecBigInt {
public:
  typedef uint32_t uint1d_t; // digit type
  typedef int32_t  int1d_t;  // signed digit type
  typedef uint64_t uint2d_t; // double-digit type
  typedef int64_t  int2d_t;  // signed double-digit type
  
private:
  std::vector<uint1d_t> v; // digits
  bool n;                  // sign (true if negative)
  
public:
  static uint1d_t const RADIX = 1000000000;        // digits system base
  static uint     const RADIX_DEC_DIGITS = 9;      // decimal digits per actual digit
  static uint     const RADIX_BITS = 30;           // bits per actual digit
  static uint     const MIN_KARTASUBA_DIGITS = 32; // empirical result
  
  // constructors/destructor
  DecBigInt() { clear(); }
  template<typename some_t> DecBigInt(some_t a) { self = a; }
  ~DecBigInt() { }
  
  // clear
  DecBigInt & clear() { v.clear(); n = false; return self; }
  
  // integer assignment
  #define UINT_ASSIGN(uint_t) \
    DecBigInt & operator=(uint_t const &a) { \
      clear(); for(uint_t t = a; t; t /= RADIX) v.push_back(t % RADIX); return self; \
    }
  #define INT_ASSIGN(int_t) \
    DecBigInt & operator=(int_t const &a) { \
      if(a < 0) { return (self = -a).neg(); } \
      clear(); for(int_t t = a; t; t /= RADIX) v.push_back(t % RADIX); return self; \
    }
  UINT_ASSIGN(uint1d_t);
  UINT_ASSIGN(uint2d_t);
  INT_ASSIGN(int1d_t);
  INT_ASSIGN(int2d_t);
  #undef UINT_ASSIGN
  #undef INT_ASSIGN
  
  // string assignment
  DecBigInt & operator=(std::string const &a) { std::istringstream ss(a); ss >> self; return self; }
  DecBigInt & operator=(char const * const &a) { return (self = std::string(a)); }
  
  // floating-point value assignment
  #define NUM_ASSIGN(num_t) \
    DecBigInt & operator=(num_t const &a) { \
      clear(); \
      num_t r = floor(fabs(a)); \
      while(r > 0.5) { \
        v.push_back((fmod(r, RADIX))); \
        r = floor(r / RADIX); \
      } \
      n = (a < 0); \
      return self; \
    }
  NUM_ASSIGN(float)
  NUM_ASSIGN(double)
  NUM_ASSIGN(long double)
  #undef NUM_ASSIGN
  
  // copying
  DecBigInt & operator=(DecBigInt const &a) { v = a.v; n = a.n; return self; }
  
  // swap
  DecBigInt & swap(DecBigInt &a) { std::swap(n, a.n); v.swap(a.v); return self; }
  
  // toggle sign
  DecBigInt & neg() { n = not n; return self; }
  
  // absolute value
  DecBigInt abs() const { DecBigInt ret(self); ret.n = false; return ret; }
  
  // unary + and - (note: unary + is useful for making a copy)
  DecBigInt operator+() const { return self; }
  DecBigInt operator-() const { DecBigInt ret(self); ret.neg(); return ret; }
  
  // digits count
  uint digits() const { return v.size(); }
  
  // signum
  int sgn_nz() const { return 1 - 2 * n; } // non-zero version
  int sgn() const { return (self ? sgn_nz() : 0); }
  
  // cast to bool (true iff non-zero)
  operator bool() const { return not v.empty(); }
  
  // cast to integer or floating-point types
  #define ARITHMETIC_CAST(some_t) \
    operator some_t() const { \
      some_t ret = 0; \
      for(uint i = digits(); i--;) ret = ret * RADIX + v[i]; \
      return (n ? -ret : ret); \
    }
  ARITHMETIC_CAST(uint1d_t);
  ARITHMETIC_CAST(int1d_t);
  ARITHMETIC_CAST(uint2d_t);
  ARITHMETIC_CAST(int2d_t);
  ARITHMETIC_CAST(float);
  ARITHMETIC_CAST(double);
  ARITHMETIC_CAST(long double);
  #undef ARITHMETIC_CAST
  
  // cast to string
  operator std::string() const { std::ostringstream ss; ss << self; return ss.str(); }
  
  // pop leading zeros
  DecBigInt & fix() { while(self and (v.back() == 0)) v.pop_back(); if(not self) n = false; return self; }
  
  // compare absolute values (positive: greater, 0: equal, negative: lower)
  int cmp_abs(DecBigInt const &a) const {
    int sd = digits(), ad = a.digits();
    if(sd != ad) return sd - ad;
    for(uint i = sd; i--;) if(v[i] != a.v[i]) return (v[i] > a.v[i] ? 1 : -1);
    return 0;
  }
  
  // compare DecBigInts
  int cmp(DecBigInt const &a) const {
    if(n == a.n) return sgn_nz() * cmp_abs(a);
    /* else */   return a.n - n;
  }
  
  // compare absulute value against small unsigned number
  int cmp_abs(uint1d_t const &a) const {
    if(not self)     return (a ? -1 : 0);
    if(digits() > 2) return true;
    uint2d_t sv = self;
    return (sv == a ? 0 : (sv > a ? 1 : -1));
  }
  
  // compare against small unsigned number
  int cmp(uint1d_t const &a) const {
    if(self.n) return -1;
    /* else */ return cmp_abs(a);
  }
  
  // compare against small signed number
  int cmp(int1d_t const &a) const {
    if(a >= 0) return (n ? -1 : cmp(uint1d_t(a)));
    /* else */ return (n ? -cmp_abs(uint1d_t(-a)) : 1);
  }
  
  // comparison operators
  template<typename some_t> bool operator< (some_t rhs) const { return cmp(rhs) <  0; }
  template<typename some_t> bool operator<=(some_t rhs) const { return cmp(rhs) <= 0; }
  template<typename some_t> bool operator==(some_t rhs) const { return cmp(rhs) == 0; }
  template<typename some_t> bool operator!=(some_t rhs) const { return cmp(rhs) != 0; }
  template<typename some_t> bool operator>=(some_t rhs) const { return cmp(rhs) >= 0; }
  template<typename some_t> bool operator> (some_t rhs) const { return cmp(rhs) >  0; }
  
  // increment absolute value
  DecBigInt & inc_abs() {
    uint i = 0, sd = digits();
    for(; i < sd; ++i) if(++v[i] == RADIX) v[i] = 0; else break;
    if(i == sd) v.push_back(1);
    return self;
  }
  
  // decrement absolute value
  DecBigInt & dec_abs() {
    if(not self) { v.push_back(1); return neg(); }
    // assertion: some digit is non-zero for each non-zero number
    for(uint i = 0; true; i++) if(--v[i] >= RADIX) v[i] = RADIX - 1; else break;
    return fix();
  }
  
  // incrementation/decrementation operators
  DecBigInt & operator++() { return (n ? dec_abs() : inc_abs()); }
  DecBigInt & operator--() { return (n ? inc_abs() : dec_abs()); }
  DecBigInt operator++(int) { DecBigInt ret(self); ++self; return ret; }
  DecBigInt operator--(int) { DecBigInt ret(self); --self; return ret; }
  
  // fix single digit
  static uint1d_t digit_fix(uint1d_t const &a) {
    if(a < RADIX)                 return a;
    /* else */ if(int1d_t(a) > 0) return a - RADIX;
    /* else */                    return a + RADIX;
  }
  
  // add absolute values
  DecBigInt & add_abs(DecBigInt const &a) {
    uint1d_t t = 0;
    uint sd = digits(), ad = a.digits();
    if(sd < ad) { v.resize(ad, 0); sd = ad; }
    uint i = 0;
    for(; i < ad; ++i) v[i] = digit_fix(t = (t >= RADIX) + v[i] + a.v[i]);
    for(; i < sd; ++i) v[i] = digit_fix(t = (t >= RADIX) + v[i]);
    if(t >= RADIX) v.push_back(1);
    return self;
  }
  
  // subtract absolute values
  DecBigInt & sub_abs(DecBigInt const &a) {
    int1d_t t = 0;
    uint sd = digits(), ad = a.digits();
    uint i = 0;
    for(; i < ad; ++i) v[i] = digit_fix(t = -(t < 0) + v[i] - a.v[i]);
    for(; i < sd; ++i) v[i] = digit_fix(t = -(t < 0) + v[i]);
    return fix();
  }
  
  // reverse (minuend-based) subtract absolute values
  DecBigInt & min_abs(DecBigInt const &a) {
    int1d_t t = 0;
    uint sd = digits(), ad = a.digits();
    if(sd < ad) { v.resize(ad, 0); sd = ad; }
    for(uint i = 0; i < sd; ++i) v[i] = digit_fix(t = -(t < 0) - v[i] + a.v[i]);
    return fix();
  }
  
  // addition
  DecBigInt & operator+=(DecBigInt const &rhs) {
    if(n == rhs.n)       return add_abs(rhs);
    int c = cmp_abs(rhs);
    if(c == 0)           return clear();
    else if(c > 0)       return sub_abs(rhs);
    else /* if(c < 0) */ return min_abs(rhs).neg();
  }
  
  // subtraction
  DecBigInt & operator-=(DecBigInt const &rhs) {
    if(n != rhs.n)       return add_abs(rhs);
    int c = cmp_abs(rhs);
    if(c == 0)           return clear();
    else if(c > 0)       return sub_abs(rhs);
    else /* if(c < 0) */ return min_abs(rhs).neg();
  }
  
  // addition and subtraction operators
  DecBigInt operator+(DecBigInt const &rhs) const { return ((+self) += rhs); }
  DecBigInt operator-(DecBigInt const &rhs) const { return ((+self) -= rhs); }
  
  // multi-digit right keep
  DecBigInt & rdkeep(int const &ds) {
    if(uint(ds) >= digits()) return self;
    v.erase(v.begin() + ds, v.end());
    return fix();
  }
  
  // multi-digit right shift
  DecBigInt & rdshift(int const &ds) {
    if(ds < 0) return ldshift(-ds);
    if(uint(ds) >= digits()) return clear();
    v.erase(v.begin(), v.begin() + ds);
    return self;
  }
  
  // multi-digit left shift
  DecBigInt & ldshift(int const &ds) {
    if(ds < 0) return rdshift(-ds);
    v.insert(v.begin(), ds, 0);
    return self;
  }
  
  // add absolute value at offset
  DecBigInt & add_abs_at(DecBigInt const &a, uint const &off = 0) {
    if(&a == this) return add_abs_at(+a, off);
    uint1d_t t = 0;
    uint sd = digits(), ad = a.digits() + off;
    if(sd < ad) { v.resize(ad, 0); sd = ad; }
    uint i = off;
    for(; i < ad; ++i) v[i] = digit_fix(t = (t >= RADIX) + v[i] + a.v[i - off]);
    for(; i < sd; ++i) v[i] = digit_fix(t = (t >= RADIX) + v[i]);
    if(t >= RADIX) v.push_back(1);
    return self;
  }
  
  // multiply times digit
  DecBigInt & operator*=(uint1d_t const &rhs) {
    if(rhs == 0) return clear();
    uint2d_t t = 0;
    uint2d_t mul = rhs;
    uint sd = digits();
    uint i = 0;
    for(; i < sd; ++i) v[i] = (t = (t / RADIX) + v[i] * mul) % RADIX;
    t /= RADIX; if(t) v.push_back(t);
    return self;
  }
  DecBigInt operator*(uint1d_t const &rhs) const { return ((+self) *= rhs); }
  
  // multiplication
  DecBigInt operator*(DecBigInt const &rhs) const {
    if(not rhs) return DecBigInt();
    DecBigInt ret;
    uint sd = digits(), rd = rhs.digits(), dd = std::min(sd, rd);
    if(dd > MIN_KARTASUBA_DIGITS) {
      // Kartasuba algorithm
      uint hd = dd / 2;
      DecBigInt sh(self); sh.n = false; sh.rdshift(hd);
      DecBigInt sl(self); sl.n = false; sl.rdkeep (hd);
      DecBigInt rh(rhs);  rh.n = false; rh.rdshift(hd);
      DecBigInt rl(rhs);  rl.n = false; rl.rdkeep (hd);
      DecBigInt kh(sh * rh);
      DecBigInt kl(sl * rl);
      sl += sh;
      rl += rh;
      DecBigInt km(sl * rl); km -= kh; km -= kl;
      kh.ldshift(hd * 2);
      km.ldshift(hd);
      ret = kh + km + kl;
    } else {
      for(uint i = 0; i < sd; ++i) ret.add_abs_at(rhs * v[i], i);
    }
    ret.n = n xor rhs.n;
    return ret;
  }
  
  // in-place multiplication
  DecBigInt & operator*=(DecBigInt const &rhs) { return (self = self * rhs); }
  
  // power exponentiation
  DecBigInt & pow(uint const &exp) {
    if(exp == 0) return (self = 1);
    DecBigInt mul(self);
    for(uint e = exp - 1; e; e >>= 1) {
      if(e & 1) self *= mul;
      mul *= mul;
    }
    return self;
  }
  
  // modulo by small divisor
  int2d_t operator%(uint1d_t const &rhs) const {
    uint2d_t mul = RADIX % rhs;
    uint2d_t rem = 0;
    for(uint i = digits(); i--;) rem = (rem * mul + v[i]) % rhs;
    return (n ? -rem : rem);
  }
  
  // simplified in-place division by 2
  DecBigInt & div2() {
    static uint const c = RADIX / 2;
    int sd = digits();
    for(int i = 0; i < sd - 1; ++i) v[i] = (v[i] / 2) + (v[i + 1] & 1) * c;
    if(sd) v[sd - 1] /= 2;
    return fix();
  }
  
  // division algorithm
  DecBigInt & inplace_modulo(DecBigInt const &div, DecBigInt &quo) {
    if(not div) raise(SIGFPE);
    bool sn = n; n = false;
    DecBigInt d(div); d.n = false;
    quo.clear();
    DecBigInt m(1);
    int b = (digits() - d.digits() + 1) * RADIX_BITS;
    if(b > 0) {
      DecBigInt h(2); h.pow(b);
      for(d *= h, m = h; self and b >= 0; d.div2(), m.div2(), --b) {
        if(cmp_abs(d) >= 0) {
          sub_abs(d);
          quo.add_abs(m);
        }
      }
    }
    quo.n = div.n xor sn;
    n = sn;
    return self;
  }
  
  // modulo only (slightly faster)
  DecBigInt & inplace_modulo(DecBigInt const &div) {
    if(not div) raise(SIGFPE);
    bool sn = n; n = false;
    DecBigInt d(div); d.n = false;
    int b = (digits() - d.digits() + 1) * RADIX_BITS;
    if(b > 0) {
      DecBigInt h(2); h.pow(b);
      for(d *= h; self and b >= 0; d.div2(), --b) {
        if(cmp_abs(d) >= 0) sub_abs(d);
      }
    }
    n = sn;
    return self;
  }
  
  // division + modulo API
  std::pair<DecBigInt, DecBigInt> divmod(DecBigInt const &rhs) {
    std::pair<DecBigInt, DecBigInt> ret;
    ret.second = self;
    ret.second.inplace_modulo(rhs, ret.first);
    return ret;
  }
  
  // division and modulo operators
  DecBigInt & operator%=(DecBigInt const &rhs) { return inplace_modulo(rhs); }
  DecBigInt   operator% (DecBigInt const &rhs) const { return ((+self) %= rhs); }
  DecBigInt   operator/ (DecBigInt const &rhs) const { DecBigInt quo; (+self).inplace_modulo(rhs, quo); return quo; }
  DecBigInt & operator/=(DecBigInt const &rhs) { return (self = self / rhs); }
  
  // print to ostream
  friend std::ostream& operator<<(std::ostream& os, DecBigInt const &rhs) {
    if(not rhs) return os << '0';
    if(rhs.n) os << '-';
    os << rhs.v.back();
    for(uint i = rhs.digits() - 1; i--;) os << std::setw(RADIX_DEC_DIGITS) << std::setfill('0') << rhs.v[i];
    return os;
  }
  
  // read from istream
  friend std::istream& operator>>(std::istream& is, DecBigInt &rhs) {
    std::string word;
    is >> word;
    rhs.clear();
    if(word.empty()) return is;
    int l = 0;
    if(word[0] == '-') { l = 1; rhs.n = true; }
    for(int i = word.length() - 1; i >= l;) {
      uint1d_t t = 0, mul = 1;
      while(i >= l and mul < RADIX) { t += mul * (word[i--] - '0'); mul *= 10; }
      rhs.v.push_back(t);
    }
    rhs.fix();
    return is;
  }
};



int const N = 21;
typedef bitset<N> row;


struct RowCmp {
  bool operator()(row const &a, row const &b) const {
    for(int i = N; i --> 0;) if(a[i] ^ b[i]) return a[i];
    return false;
  }
};


struct Congruence {
  int val, mod, min;
  
  Congruence(): val(0), mod(1), min(0) { }
  Congruence(int const &val, int const &mod, int const &min): val(val), mod(mod), min(min) { }
  ~Congruence() { }
  
  void fix() { val %= mod; if(val < 0) val += mod; }
};


struct BigCongruence {
  DecBigInt val, mod;
  
  BigCongruence(): val(0), mod(1) { }
  BigCongruence(DecBigInt const &val, DecBigInt const &mod): val(val), mod(mod) { }
  BigCongruence(Congruence const &con): val(con.val), mod(con.mod) { }
  ~BigCongruence() { }
  
  void fix() { val %= mod; if(val < 0) val += mod; }
};


DecBigInt gcd(DecBigInt const &a, DecBigInt const &b) {
  return b ? gcd(b, a % b) : a;
}

void egcd(DecBigInt const &p, DecBigInt const &q, DecBigInt &pf, DecBigInt &qf, DecBigInt &r) {
  r = p;
                    pf = 1,  qf = 0;
  DecBigInt b = q, bpf = 0, bqf = 1;
  while(b) {
    DecBigInt d = r / b;
    DecBigInt t = r, tpf = pf, tqf = qf;
    r = b; pf = bpf; qf = bqf;
    b = t - b * d; bpf = tpf - bpf * d; bqf = tqf - bqf * d;
  }
}


BigCongruence merge(BigCongruence const &ca, BigCongruence cb) {
  BigCongruence cr;
  DecBigInt pa, pb;
  DecBigInt g = gcd(ca.mod, cb.mod);
  cb.mod /= g;
  cb.fix();
  egcd(ca.mod, cb.mod, pa, pb, g);
  cr.val = pa * ca.mod * cb.val + pb * cb.mod * ca.val; 
  cr.mod = ca.mod * cb.mod;
  cr.fix();
  return cr;
}


int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  
  int n, b, r;
  cin >> n >> b >> r;
  
  vector<row> mtx(n);
  
  for(int i = 0; i < n; ++i) {
    string s;
    cin >> s;
    for(int j = 0; j < n; ++j) mtx[i][j] = (s[j] == '1');
  }
  
  row bRow;
  for(int i = 0; i < b; ++i) bRow[i] = true;
  
  map<int, int> ok;
  bool allowStage2 = true;
  vector<Congruence> congs;
  
  for(int q = 0; q < r; ++q) {
    int t;
    cin >> t; --t;
    
    map< row, int, RowCmp, allocator< pair<row const, int> > > mp;
    row c = mtx[t];
    int i = 0;
    mp[c] = i;
    
    int p0 = -1;
    int pl = -1;
    int lastOk = -1;
    
    if((c & bRow) == c) {
      if(ok.find(i) == ok.end()) ok[i] = 1;
      else ++ok[i];
      lastOk = i;
    }
    
    for(; ++i;) {
      row d;
      for(int j = 0; j < n; ++j) if(c[j]) d |= mtx[j];
      c = d;
      
      if(mp.find(c) != mp.end()) {
        p0 = mp[c];
        pl = i - p0;
        break;
      }
      
      if((c & bRow) == c) {
        if(ok.find(i) == ok.end()) ok[i] = 1;
        else ++ok[i];
        lastOk = i;
      }
      
      mp[c] = i;
    }
    
    if(lastOk >= p0) {
      congs.push_back(Congruence((lastOk - p0) % pl, pl, p0));
    } else {
      allowStage2 = false;
    }
  }
  
  for(map<int, int>::iterator it = ok.begin(); it != ok.end(); ++it) {
    if(it->second == r) {
      cout << 1 + it->first << '\n';
      return 0;
    }
  }
  
  if(not allowStage2) {
    cout << "-1\n";
    return 0;
  }
  
  //TODO merge congruences
  BigCongruence rc;
  int min = 0;
  for(unsigned int i = 0; i < congs.size(); ++i) {
    rc = merge(rc, BigCongruence(congs[i]));
    min = max(min, congs[i].min);
  }
  
  DecBigInt ret = rc.val;
  DecBigInt q = (DecBigInt(min) + rc.mod - DecBigInt(1)) / rc.mod;
  cout << ret + q * rc.mod << '\n';
  
  return 0;
}