#include <bits/stdc++.h>
using namespace std;
long long read() {
  long long x = 0, F = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') F = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = x * 10 + c - '0';
    c = getchar();
  }
  return x * F;
}
int add(int a, int b) {
  return (a + b >= 1000000007) ? a + b - 1000000007 : a + b;
}
int dec(int a, int b) { return (a - b < 0) ? a - b + 1000000007 : a - b; }
int mul(int a, int b) { return (1LL * a * b) % 1000000007; }
int fst_pow(int a, int b) {
  int res = 1;
  while (b) {
    if (b & 1) res = mul(res, a);
    a = mul(a, a), b >>= 1;
  }
  return res;
}
int n, a, b;
vector<int> G[2000000 + 5], P[2000000 + 5], T[2000000 + 5], tmp;
char s[2000000 + 5];
int stk[2000000 + 5], low[2000000 + 5], dfn[2000000 + 5], tp;
int bel[2000000 + 5], gval[2000000 + 5], bcnt, ct;
int num[2000000 + 5], cnt[2000000 + 5], L[2000000 + 5], R[2000000 + 5];
int fac[2000000 + 5], ifac[2000000 + 5], ans;
int gcd(int a, int b) { return (!b) ? a : gcd(b, a % b); }
void tarjan(int x) {
  stk[++tp] = x, dfn[x] = low[x] = ++ct;
  for (int i = 0; i < G[x].size(); i++) {
    int v = G[x][i];
    if (!dfn[v]) {
      tarjan(v);
      low[x] = min(low[x], low[v]);
    } else if (!bel[v])
      low[x] = min(low[x], dfn[v]);
  }
  if (low[x] == dfn[x]) {
    int v;
    bcnt++;
    do {
      v = stk[tp], bel[v] = bcnt, tp--;
      gval[bcnt] = gcd(gval[bcnt], num[v]);
    } while (v != x);
    T[bcnt].resize(gval[bcnt]);
  }
}
void prepare() {
  fac[0] = 1;
  for (int i = 1; i <= n; i++) fac[i] = mul(fac[i - 1], i);
  ifac[n] = fst_pow(fac[n], 1000000007 - 2);
  for (int i = n; i >= 1; i--) ifac[i - 1] = mul(ifac[i], i);
}
int Comb(int a, int b) {
  if (b > a || a < 0 || b < 0) return 0;
  return mul(fac[a], mul(ifac[b], ifac[a - b]));
}
int main() {
  n = read(), a = read(), b = read();
  for (int i = 1; i <= n; i++) {
    scanf("%s", s + 1);
    for (int j = 1; j <= n; j++)
      if (s[j] == '1') G[i].push_back(j);
  }
  for (int i = 1; i <= n; i++) {
    num[i] = read();
    scanf("%s", s);
    for (int j = 0; j < num[i]; j++) P[i].push_back(s[j] - '0');
  }
  for (int i = 1; i <= n; i++)
    if (!dfn[i]) tarjan(i);
  for (int i = 1; i <= n; i++)
    for (int j = 0; j < num[i]; j++) T[bel[i]][j % gval[bel[i]]] |= P[i][j];
  for (int i = bcnt; i > 1; i--) {
    int tg = gcd(gval[i], gval[i - 1]);
    tmp.clear(), tmp.resize(tg);
    for (int j = 0; j < gval[i]; j++) tmp[j % tg] |= T[i][j];
    for (int j = 0; j < gval[i - 1]; j++) T[i - 1][j] |= tmp[j % tg];
  }
  for (int i = 1; i <= bcnt; i++)
    for (int j = 0; j < gval[i]; j++) cnt[i] += T[i][j];
  for (int i = 1; i <= n; i++) {
    R[i] = cnt[bel[i]] * (num[i] / gval[bel[i]]);
    for (int j = 0; j < num[i]; j++) L[i] += P[i][j];
  }
  prepare();
  for (int i = 1; i <= n; i++) {
    int A = 0, B = 0;
    for (int j = 1; j <= n; j++)
      if (L[j] > R[i])
        A++;
      else if (R[j] > R[i] || (R[i] == R[j] && j > i))
        B++;
    if (A >= a) continue;
    for (int j = min(b - 1, min(B, a - 1 - A)); j >= b - A - 1 && j >= 0; j--)
      ans = add(ans, mul(Comb(B, j), Comb(A, b - j - 1)));
  }
  printf("%d\n", ans);
}
