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
#include <cstdio>
#include <vector>

int n, b, r;

bool matrix[4][256][256];
std::vector<int> robots;

bool Check(int number) {
  for (int robot : robots) {
    for (int i = b + 1; i <= n; i++) {
      if (matrix[number][robot][i]) {
        return false;
      }
    }
  }
  return true;
}

void Multiply(int a, int b, int c) {
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      matrix[c][i][j] = false;
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int k = 1; k <= n; k++) {
      for (int j = 1; j <= n; j++) {
        matrix[c][i][j]
            = matrix[c][i][j] or (matrix[a][i][k] and matrix[b][k][j]);
      }
    }
  }
}

int main() {
  scanf("%d%d%d", &n, &b, &r);
  for (int i = 1; i <= n; i++) {
    matrix[0][i][i] = true;
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      char c;
      scanf(" %c", &c);
      matrix[1][i][j] = (c == '1');
    }
  }
  for (int i = 0; i < r; i++) {
    int a;
    scanf("%d", &a);
    robots.push_back(a);
  }
  const int max_computation = (100 * 1000 * 1000) / (n * n * n);
  if (Check(1)) {
    printf("%d\n", 0);
    return 0;
  }
  for (int i = 1; i < max_computation; i++) {
    Multiply((i + 3) % 4, i % 4, (i + 1) % 4);
    if (Check((i + 1) % 4)) {
      printf("%d\n", i);
      return 0;
    }
  }
  printf("-1\n");
  return 0;
}