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
#include <bitset>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <vector>

using namespace std;

class BigNumber {
 public:
  BigNumber() : v_(1, 0) {}
  BigNumber(const int n) : v_(1, n) {}

  /*
  void Print() const {
    cerr << v_.back();
    for (int i = v_.size() - 2; i >= 0; --i) {
      const long long  a = v_[i] ? v_[i] : 1;
      long long b = kBlock / 10;
      while (a < b) {
        cerr << '0';
        b /= 10;
      }
      cerr << v_[i];
    }
    cerr << endl;
  }
  */

  void Printf() const {
    printf("%lld", v_.back());
    for (int i = v_.size() - 2; i >= 0; --i) {
      const long long  a = v_[i] ? v_[i] : 1;
      long long b = kBlock / 10;
      while (a < b) {
        printf("0");
        b /= 10;
      }
      printf("%lld", v_[i]);
    }
    printf("\n");
  }

  BigNumber operator/(const int n) const {
    BigNumber ret = *this;
    ret /= n;
    return ret;
  }

  BigNumber operator+(const int n) const {
    BigNumber ret = *this;
    ret += n;
    return ret;
  }

  BigNumber operator*(const int n) const {
    BigNumber ret = *this;
    ret *= n;
    return ret;
  }

  BigNumber operator/=(const int n) {
    long long carry = 0;
    for (int i = v_.size() - 1; i >= 0; --i) {
      long long a = v_[i] + carry;
      carry = (a % n) * kBlock;
      v_[i] = a / n;
    }
    CutLeadingZeros();
    return *this;
  }

  BigNumber operator+=(const BigNumber& n) {
    long long carry = 0;
    int i = 0;
    while (carry || i < n.v_.size()) {
      if (i >= v_.size()) v_.push_back(0);
      long long a = v_[i] + carry;
      if (i < n.v_.size()) a += n.v_[i];
      carry = a / kBlock;
      v_[i] = a % kBlock;
      ++i;
    }
    return *this;
  }

  BigNumber operator+=(const int n) {
    long long carry = 0;
    for (int i = 0; i < v_.size(); ++i) {
      long long a = v_[i] + n + carry;
      carry = a / kBlock;
      v_[i] = a % kBlock;
    }
    if (carry) v_.push_back(carry);
    return *this;
  }

  BigNumber operator*=(const int n) {
    long long carry = 0;
    for (int i = 0; i < v_.size(); ++i) {
      long long a = v_[i] * n + carry;
      carry = a / kBlock;
      v_[i] = a % kBlock;
    }
    if (carry) v_.push_back(carry);
    return *this;
  }

  int operator%(const int n) const {
    // cerr << '%' << n << endl;
    // cerr << v_.size() << endl;
    long long ret = 0;
    long long delta = 1;
    for (int i = 0; i < v_.size(); ++i) {
      // cerr << v_[i] << endl;
      ret += (delta * v_[i]) % n;
      ret %= n;
      delta *= kBlock;
      delta %= n;
    }
    // cerr << ret << endl;
    return ret;
  }

  bool operator!=(const BigNumber& rhs) const { return v_ != rhs.v_; }

  bool operator==(const int n) const { return v_.size() == 1 && v_[0] == n; }

 private:
  void CutLeadingZeros() {
    while (v_.size() > 1 && v_.back() == 0) v_.resize(v_.size() - 1);
  }

  static const long long kBlock = 1000LL * 1000LL * 1000LL * 1000LL * 1000LL;
  vector<long long> v_;
};

// 24 0.2/1.0s
// 27 TLE
const int kBrute = 1 << 24;
const int kMaxN = 200;
const int kInfinity = kMaxN + 1;

bitset<kMaxN> bad;
bitset<kMaxN> robots;
int n;
BigNumber power;
BigNumber shift(0);
BigNumber step(1);
vector<bitset<kMaxN> > can, can_power;

vector<bitset<kMaxN> > Multiply(const vector<bitset<kMaxN> >& a, const vector<bitset<kMaxN> >& b) {
  vector<bitset<kMaxN> > ret(n);
  for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) if (a[i].test(j)) ret[i] |= b[j];
  return ret;
}

vector<bitset<kMaxN> > Power(const vector<bitset<kMaxN> >& b, BigNumber exp) {
  /*
  cerr << "Computing power ";
  exp.Print();
  */
  if (exp == 1) return b;
  vector<bitset<kMaxN> > half = Power(b, exp / 2);
  vector<bitset<kMaxN> > full = Multiply(half, half);
  if (exp % 2) return Multiply(full, b); else return full;
}

void CheckAndAdvance() {
  if ((robots & bad).none()) {
    shift.Printf();
    exit(0);
  }
  /*
  cerr << "Advancing ";
  step.Print();
  cerr << "Power is ";
  power.Print();
  */
  if (power != step) {
    can_power = Power(can, step);
    power = step;
  }
  bitset<kMaxN> robots_new;
  // cerr << "can_power size " << can_power.size() << endl;
  for (int j = 0; j < n; ++j) if (robots.test(j)) robots_new |= can_power[j];
  robots = robots_new;
  // cerr << "Increasing shift." << endl;
  shift += step;
  /*
  cerr << "New shift " << flush;
  shift.Print();
  */
}

long long Gcd(long long a, long long b) {
  // cerr << "Gcd " << a << ' ' << b << endl;
  while (true) {
    if (!a) return b;
    b %= a;
    if (!b) return a;
    a %= b;
  }
}

long long Gcd(const BigNumber& a, const long long b) {
  assert(b != 0);
  return Gcd(a % b, b);
}

BigNumber Lcm(const BigNumber& a, const long long b) {
  return a * (b / Gcd(a, b));
}

long long Lcm(const long long a, const long long b) {
  return a * (b / Gcd(a, b));
}

int main() {
  int b;
  int r;
  scanf("%d%d%d", &n, &b, &r);
  can.resize(n);
  for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) {
    char c;
    do {
      scanf("%c", &c);
    } while (c != '0' && c != '1');
    if (c == '1') can[i].set(j);
  }
  power = 1;
  can_power = can;

  for (int i = 0; i < r; ++i) {
    int robot;
    scanf("%d", &robot);
    robots.set(robot - 1);
  }

  for (int i = b; i < n; ++i) bad.set(i);

  /*
  const int brute_limit = kBrute / n;
  for (int i = 0; i < brute_limit; ++i) {
  }
  printf("-1\n");
  */

  vector<vector<pair<int, int> > > dist(n, vector<pair<int, int> >(n, make_pair(kInfinity, kInfinity)));
  for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) if (can[i][j]) dist[i][j] = make_pair(1, kInfinity);

  {
    bool change = true;
    while (change) {
      change = false;
      for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) for (int k = 0; k < n; ++k) {
        pair<int, int> better = make_pair(dist[j][i].first + dist[i][k].first, min(dist[j][i].second, dist[i][k].second));
        if (j == i && dist[j][i].first < kInfinity) better = min(better, make_pair(dist[i][k].first, dist[j][i].first));
        if (i == k && dist[i][k].first < kInfinity) better = min(better, make_pair(dist[j][i].first, dist[i][k].first));
        if (better < dist[j][k]) {
          dist[j][k] = better;
          change = true;
        }
      }
    }
  }

  /*
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      if (dist[i][j].first == kInfinity) cerr << "- "; else cerr << dist[i][j].first << ',' << dist[i][j].second << ' ';
    }
    cerr << endl;
  }
  */

  vector<vector<bool> > avoid(n + 1);
  for (int i = 1; i <= n; ++i) avoid[i].resize(i);

  for (int i = n - 1; i >= b; --i) {
    int max_reach = robots.test(i) ? 0 : kInfinity;
    for (int j = 0; j < n; ++j) if (robots.test(j)) if (dist[j][i].first < kInfinity) if (max_reach == kInfinity || dist[j][i].first > max_reach) max_reach = dist[j][i].first;
    // If max_reach == kInfinity, we can simply forget about this guy.
    if (max_reach < kInfinity) {
      // cerr << "Max reach: " << max_reach << endl;
      // Just store the congruences (slighly bluffy here).
      for (int j = 0; j < n; ++j) if (robots.test(j)) {
        const int distance = dist[j][i].first;
        if (distance < kInfinity) {
          const int period = dist[j][i].second;
          if (period < kInfinity) {
            avoid[period][(shift + distance) % period] = true;
            // cerr << i << ": avoid " << period << ' ' << (shift + distance) % period << endl;
          }
        }
      }
      // We can simply forward time and then forget about this guy.
      while (max_reach-- >= 0) CheckAndAdvance();
    }
  }

  // Now we just need to satisfy the congruences.
  {
    bool change = true;
    while (change) {
      change = false;
      for (int i = 1; i <= n; ++i) for (int j = 0; j < i; ++j) for (int k = i; k <= n; k += i) {
        bool all_avoid = true;
        for (int l = j; l < k; l += i) all_avoid &= avoid[k][l];
        if (avoid[i][j] != all_avoid) {
          change = true;
          if (all_avoid) avoid[i][j] = true; else for (int l = j; l < k; l += i) avoid[k][l] = true;
        }
      }
    }
  }
  for (int i = 1; i <= n; ++i) {
    bool contradiction = true;
    for (int j = 0; j < i; ++j) {
      // if (avoid[i][j]) cerr << "Avoid " << i << ' ' << j << endl;
      contradiction &= avoid[i][j];
    }
    if (contradiction) {
      printf("-1\n");
      return 0;
    }
  }

  step = 1;
  vector<int> choice(n + 1, -1);
  for (int i = n; i >= 1; --i) {
    // cerr << "Checking modulo " << i << endl;
    if (choice[i] != -1) {
      if (shift % i != choice[i]) {
        cerr << "Choice mismatch." << endl;
        printf("-1\n");
        return 0;
      }
    } else {
      bool freedom = true;
      for (int j = 0; j < i; ++j) if (avoid[i][j]) freedom = false;
      if (!freedom) {
        for (int j = 0; j < i; ++j) if (avoid[i][shift % i]) CheckAndAdvance(); else break;
        if (avoid[i][shift % i]) {
          cerr << "This should have been detected earlier." << endl;
          printf("-1\n");
          return 0;
        }
        /*
        shift.Print();
        cerr << "Remainder by " << i << " is " << shift % i << endl;
        */
        for (int j = 1; j <= i; ++j) if (i % j == 0) choice[j] = shift % j;
        /*
        cerr << "Old step ";
        step.Print();
        cerr << "Computing LCM with " << i << endl;
        */
        step = Lcm(step, i);
        /*
        cerr << "New step ";
        step.Print();
        */
      }
    }
  }

  CheckAndAdvance();

  cerr << "This shouldn't be happenning, really." << endl;
  printf("-1\n");
  // shift.Printf();

  return 0;
}