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
#include <bits/stdc++.h>
using namespace std;

typedef bitset<200> State;

vector<State> graph;

State advance(const State& state) {
  State next;
  for (int i=0; i<graph.size(); ++i)
    if (state[i])
      next |= graph[i];
  return next;
}

int main() {
  int n, b, r;
  cin >> n >> b >> r;
  graph.resize(n);
  for (int i = 0;i <n;++i) {
    string buf;
    cin >> buf;
    reverse(buf.begin(),buf.end());
    graph[i] = State(buf);
  }
  State start;
  for (int i=0; i<r;++i){
    int a;
    cin >> a;
    --a;
    start[a] = true;
  }
  State finish;
  for (int i = 0;i<b;++i)
    finish[i] = true;
  State slower = start;
  long long counter = 0;
  while (true) {
    if ((finish | start) == finish)
      break;
    //cerr << counter << " " << start.to_string() << endl;
    start = advance(start);
    ++counter;
    if ((finish | start) == finish)
      break;
    //cerr << counter << " " << start.to_string() << endl;

    start = advance(start);
    ++counter;
    slower = advance(slower);
    if (slower == start) {
      cout << -1 << endl;
      return 0;
    }
  }
  cout << counter << endl;
  return 0;
}