#include <bits/stdc++.h>
using namespace std;
bool debug = 0;
int n, m, k;
int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
string direc = "RDLU";
long long ln, lk, lm;
void etp(bool f = 0) {
  puts(f ? "YES" : "NO");
  exit(0);
}
void addmod(int &x, int y, int mod = 1000000007) {
  x += y;
  if (x >= mod) x -= mod;
  assert(x >= 0 && x < mod);
}
void et() {
  puts("-1");
  exit(0);
}
long long fastPow(long long x, long long y, int mod = 1000000007) {
  long long ans = 1;
  while (y > 0) {
    if (y & 1) ans = (x * ans) % mod;
    x = x * x % mod;
    y >>= 1;
  }
  return ans;
}
long long gcd1(long long x, long long y) {
  long long z = y;
  while (x % y != 0) {
    z = x % y;
    x = y;
    y = z;
  }
  return z;
}
int fac[5005], inv[5005];
void initfac(int n) {
  fac[0] = inv[0] = 1;
  for (int(i) = 1; (i) <= (int)(n); (i)++) {
    fac[i] = (long long)fac[i - 1] * i % 1000000007;
    inv[i] = fastPow(fac[i], 1000000007 - 2);
  }
}
int C(int n, int m) {
  if (n < m) return 0;
  return (long long)fac[n] * inv[m] % 1000000007 * inv[n - m] % 1000000007;
}
vector<int> mp[5005];
int A, B;
string gs[5005];
int mem[5005];
int dfn[5005], sta[5005], low[5005], color, ii, cnt, belong[5005];
bool ins[5005];
vector<int> bccBlock[5005];
void tarjan(int x, int p) {
  dfn[x] = low[x] = ++ii;
  sta[++cnt] = x;
  ins[x] = 1;
  for (auto child : mp[x]) {
    if (!dfn[child]) {
      tarjan(child, x);
      low[x] = min(low[x], low[child]);
    } else if (ins[child]) {
      low[x] = min(low[x], dfn[child]);
    }
  }
  if (dfn[x] == low[x]) {
    ++color;
    int t;
    do {
      t = sta[cnt];
      cnt--;
      ins[t] = 0;
      belong[t] = color;
    } while (dfn[t] != low[t]);
  }
}
vector<pair<int, int> > es;
int conGcd[5005];
vector<bool> bccBl[5005];
void addGcd(int &x, int y) {
  if (x == 0)
    x = y;
  else
    x = gcd1(x, y);
}
int mx[5005], mn[5005], deg[5005];
bool ald[5005][5005];
void meg(int bccI, int bccJ) {
  int cg = gcd1(bccBl[bccI].size(), bccBl[bccJ].size());
  vector<bool> tmp(cg, 0);
  for (int(i) = 0; (i) < (int)(bccBl[bccI].size()); (i)++)
    if (bccBl[bccI][i] == 1) tmp[i % cg] = 1;
  for (int(i) = 0; (i) < (int)(bccBl[bccJ].size()); (i)++)
    if (tmp[i % cg] == 1) bccBl[bccJ][i] = 1;
}
void relaxGold(int i) {
  for (int(j) = 0; (j) < (int)(mem[i]); (j)++) {
    if (gs[i][j] == '1') {
      mx[i]++;
      mn[i]++;
    } else if (bccBl[belong[i]][j % conGcd[belong[i]]]) {
      mx[i]++;
    }
  }
}
int Ans = 0;
void calPart2(int w) {
  int c1 = 0, c2 = 0;
  for (int(i) = 1; (i) <= (int)(n); (i)++)
    if (i != w) {
      if (mn[i] > mx[w])
        c1++;
      else if (mx[i] > mx[w])
        c2++;
      else if (mx[i] == mx[w] && i < w)
        c2++;
    }
  int lj = max(0, B - 1 - c1);
  int rj = min(min(c2, B - 1), A - 1 - c1);
  for (int j = lj; j <= rj; j++) {
    addmod(Ans, (long long)C(c2, j) * C(c1, B - j - 1) % 1000000007);
  }
}
void fmain(int tid) {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> A >> B;
  debug = 0;
  for (int(i) = 1; (i) <= (int)(n); (i)++) {
    string s;
    cin >> s;
    if (i == 1 && s.size() > 10 && s[5] == '0' && A == 3670 && B == 3177)
      debug = 1;
    for (int(j) = 1; (j) <= (int)(n); (j)++)
      if (s[j - 1] == '1') {
        mp[i].push_back(j);
        es.push_back({i, j});
      }
  }
  for (int(i) = 1; (i) <= (int)(n); (i)++) cin >> mem[i] >> gs[i];
  for (int(i) = 1; (i) <= (int)(n); (i)++)
    if (belong[i] == 0) tarjan(i, 0);
  for (int(i) = 1; (i) <= (int)(n); (i)++) {
    addGcd(conGcd[belong[i]], mem[i]);
    bccBlock[belong[i]].push_back(i);
  }
  for (int(i) = 1; (i) <= (int)(color); (i)++) {
    bccBl[i].resize(conGcd[i]);
    for (int gid : bccBlock[i]) {
      for (int(j) = 0; (j) < (int)(mem[gid]); (j)++)
        if (gs[gid][j] == '1') {
          bccBl[i][j % conGcd[i]] = 1;
        }
    }
  }
  for (auto edg : es) {
    int u = belong[edg.first], v = belong[edg.second];
    if (u == v) continue;
    if (ald[u][v]) continue;
    ald[u][v] = 1;
    deg[u]++;
  }
  vector<pair<int, int> > vpsorrt;
  for (int(i) = 1; (i) <= (int)(color); (i)++) vpsorrt.push_back({-deg[i], i});
  sort(vpsorrt.begin(), vpsorrt.end());
  for (int i = 0; i + 1 < vpsorrt.size(); i++) {
    assert(vpsorrt[i].first + 1 == vpsorrt[i + 1].first);
    meg(vpsorrt[i].second, vpsorrt[i + 1].second);
  }
  vpsorrt.clear();
  for (int(i) = 1; (i) <= (int)(n); (i)++) relaxGold(i);
  initfac(n);
  for (int(i) = 1; (i) <= (int)(n); (i)++) calPart2(i);
  cout << Ans << endl;
}
int main() {
  int t = 1;
  for (int(i) = 1; (i) <= (int)(t); (i)++) {
    fmain(i);
  }
  return 0;
}
