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

const size_t LIM = 200;
typedef bitset<LIM> BS;

struct Bitmat { // column-major internally
  BS bs[LIM];

  // the operator is row-major
  BS::reference operator[](const pair<size_t, size_t> X) {
    return bs[X.second][X.first];
  }

  void print(size_t n = LIM) {
    for(size_t i = 0; i < n; ++i) {
      for(size_t j = 0; j < n; ++j)
        printf("%d", int((*this)[{i, j}]));
      printf("\n");
    }
    printf("\n");
  }

/*
  void inplaceTranspose() {
    for(size_t i = 0; i < LIM; ++i)
      for(size_t j = i+1; j < LIM; ++j) {
        auto a = (*this)[{i, j}];
        auto b = (*this)[{j, i}];

        // swap a with b
        a = a^b;
        b = b^a;
        a = a^b;
      }
  }

  void inplaceSqr() {
    Bitmat M(*this), T(*this);
    T.inplaceTranspose();

    for(size_t i = 0; i < LIM; ++i)
      for(size_t j = 0; j < LIM; ++j)
        (*this)[{i, j}] = (T.bs[i] & M.bs[j]).any();
  }
*/
};

struct Bitvec { // horizontal (transposed)
  BS bs;

  BS::reference operator[](size_t x) {
    return bs[x];
  }

  void print(size_t n = LIM) {
    for(size_t i = 0; i < n; ++i)
      printf("%d", int((*this)[i]));
    printf("\n\n");
  }

  Bitvec & operator*=(const Bitmat &M) {
    Bitvec w(*this);
    for(size_t i = 0; i < LIM; ++i)
      (*this)[i] = (w.bs & M.bs[i]).any();
    return *this;
  }
};

int main() {
  int n, b, r;
  scanf("%d%d%d", &n, &b, &r);

  Bitmat M;
  {
    char buf[LIM+1];
    for(size_t i = 0; i < n; ++i) {
      scanf("%s", buf);
      for(size_t j = 0; buf[j]; ++j)
        M[{i, j}] = (buf[j] == '1');
    }
  }

  Bitvec v;
  for(size_t i = 0; i < r; ++i) {
    int q;
    scanf("%d", &q);
    v[q-1] = 1;
  }

//  v.print(n);
//  M.print(n);

  unordered_set<BS> us;
  BS mask;
  for(size_t i = 0; i < b; ++i)
    mask[i] = 1;
  mask.flip();

  while(!us.count(v.bs)) {
    us.insert(v.bs);

    v *= M;

    if((v.bs & mask).none()) {
      printf("%d\n", int(us.size()));
      return 0;
    }
  }

  printf("-1\n");

/*
  Bitmat Mp(M);
  for(size_t i = 0; i < 200; ++i) {
    if(i < 10)
      Mp.print(n);
    Mp.inplaceSqr();
  }
  // Mp == M**2**200
*/

  return 0;
}