#include <bits/stdc++.h>
using namespace std;
long long ksm(long long a, long long b) {
  if (!b) return 1;
  long long ns = ksm(a, b >> 1);
  ns = ns * ns % 1000000007;
  if (b & 1) ns = ns * a % 1000000007;
  return ns;
}
stack<int> s;
int low[10005], dfn[10005], k[10005];
bool vis[100005];
int idcnt = 0;
vector<int> cur[5005];
vector<int> eg[5005];
int dfcnt = 0;
void dfs(int u) {
  dfn[u] = low[u] = ++dfcnt;
  s.push(u);
  vis[u] = true;
  for (auto v : eg[u]) {
    if (!dfn[v]) {
      dfs(v);
      low[u] = min(low[u], low[v]);
    } else if (vis[v])
      low[u] = min(low[u], dfn[v]);
  }
  if (dfn[u] == low[u]) {
    idcnt++;
    while (int t = s.top()) {
      vis[t] = false;
      s.pop();
      k[t] = idcnt;
      cur[idcnt].push_back(t);
      if (t == u) break;
    }
  }
}
long long jc[5005], bjc[5005];
vector<int> fl[5005];
int n, a, b;
int gcd(int a, int b) {
  if (!b) return a;
  return gcd(b, a % b);
}
char inp[1000005];
int dd[5005];
vector<int> cfl[5005];
int cnt[5005][2];
long long c(int a, int b) {
  if (b < 0 || b > a) return 0;
  return jc[a] * bjc[b] % 1000000007 * bjc[a - b] % 1000000007;
}
bool cmp(int a, int b) { return cnt[a][0] + cnt[a][1] < cnt[b][0] + cnt[b][1]; }
int main() {
  jc[0] = bjc[0] = 1;
  for (int i = 1; i < 5005; i++) jc[i] = jc[i - 1] * i % 1000000007;
  for (int i = 1; i < 5005; i++) bjc[i] = ksm(jc[i], 1000000007 - 2);
  cin >> n >> a >> b;
  for (int i = 1; i <= n; i++) {
    scanf("%s", inp + 1);
    for (int j = 1; j <= n; j++)
      if (inp[j] == '1') eg[i].push_back(j);
  }
  for (int i = 1; i <= n; i++) {
    int sz;
    scanf("%d", &sz);
    fl[i].resize(sz);
    scanf("%s", inp);
    for (int j = 0; j < sz; j++) fl[i][j] = inp[j] - '0';
  }
  for (int i = 1; i <= n; i++)
    if (!dfn[i]) dfs(i);
  for (int i = idcnt; i >= 1; i--) {
    int g = 0;
    for (auto v : cur[i]) g = gcd(g, fl[v].size());
    dd[i] = g;
    cfl[i].resize(dd[i]);
    for (auto v : cur[i])
      for (int j = 0; j < fl[v].size(); j++)
        if (fl[v][j]) cfl[i][j % g] = 1;
  }
  for (int i = idcnt; i >= 2; i--) {
    int ns = gcd(dd[i], dd[i - 1]);
    vector<int> dfl(ns);
    for (int j = 0; j < dd[i]; j++)
      if (cfl[i][j]) dfl[j % ns] = 1;
    for (int j = 0; j < dd[i - 1]; j++)
      if (dfl[j % ns]) cfl[i - 1][j] = 1;
  }
  for (int i = 1; i <= idcnt; i++)
    for (auto v : cur[i]) {
      for (int j = 0; j < fl[v].size(); j++)
        if (fl[v][j] == 0 && cfl[i][j % dd[i]] == 1) fl[v][j] = -1;
    }
  for (int i = 1; i <= n; i++) {
    for (auto x : fl[i]) {
      if (x == 1)
        cnt[i][0]++;
      else if (x == -1)
        cnt[i][1]++;
    }
  }
  vector<int> id;
  for (int i = 1; i <= n; i++) id.push_back(i);
  sort(id.begin(), id.end(), cmp);
  long long ans = 0;
  for (int i = n - 1; i >= 0; i--) {
    int u = 0, v = 0;
    int x = id[i];
    for (int j = i + 1; j < n; j++) {
      int y = id[j];
      if (cnt[y][0] > cnt[x][0] + cnt[x][1])
        u++;
      else
        v++;
    }
    int nu = max(0, u - (a - b));
    for (int s = nu; s <= u; s++)
      ans += c(u, s) * c(v, b - 1 - s), ans %= 1000000007;
  }
  cout << ans << endl;
  return 0;
}
