#include <bits/stdc++.h>
using namespace std;
const int N = 5005, M = N * N, P = 1000000007;
using ll = long long;
int n, m, dfn[N], low[N], l[N], ideg[N], clr[N], gg[N], a[N];
int g[N][N], sum[N], mx[N];
ll fac[N], ifac[N];
char s[M];
bool tt[M];
vector<bool> b[N], t[N];
vector<int> v[N];
int gcd(int x, int y) { return y ? gcd(y, x % y) : x; }
inline ll C(int n, int m) {
  return n < 0 || m < 0 || n < m ? 0 : fac[n] * ifac[m] % P * ifac[n - m] % P;
}
int tidx;
bool vis[N];
int stk[N], top;
void tarjan(int pos) {
  dfn[pos] = low[pos] = ++tidx;
  vis[stk[++top] = pos] = 1;
  for (int i = 1; i <= n; i++)
    if (g[pos][i]) {
      if (!dfn[i])
        tarjan(i), low[pos] = min(low[pos], low[i]);
      else if (vis[i])
        low[pos] = min(low[pos], dfn[i]);
    }
  if (dfn[pos] == low[pos]) {
    ++m;
    for (int x = 0, p; p = stk[top], x != pos; x = stk[top--])
      clr[p] = m, vis[p] = 0, gg[m] = gcd(gg[m], l[p]), v[m].push_back(p);
    for (int i = 1; i <= gg[m]; i++) t[m].push_back(0);
    for (auto &i : v[m])
      for (int j = 0; j < l[i]; j++)
        t[m][j % gg[m]] = t[m][j % gg[m]] | b[i][j];
  }
}
int aa, bb;
ll qpow(ll a, ll b) {
  ll ret = 1;
  while (b) {
    if (b & 1) ret = ret * a % P;
    a = a * a % P;
    b >>= 1;
  }
  return ret;
}
int ss[N];
int main() {
  ios::sync_with_stdio(false);
  cin >> n >> aa >> bb;
  fac[0] = 1;
  for (int i = 1; i <= n; i++) fac[i] = fac[i - 1] * i % P;
  ifac[n] = qpow(fac[n], P - 2);
  for (int i = n; i > 0; --i) ifac[i - 1] = ifac[i] * i % P;
  for (int i = 1; i <= n; i++) {
    cin >> (s + 1);
    for (int j = 1; j <= n; j++) g[i][j] = (s[j] == '1');
  }
  for (int i = 1; i <= n; i++) {
    cin >> l[i] >> (s + 1);
    for (int j = 1; j <= l[i]; j++)
      b[i].push_back(s[j] - '0'), sum[i] += s[j] - '0';
  }
  for (int i = 1; i <= n; i++)
    if (!dfn[i]) tarjan(i);
  for (int i = m; i > 1; --i) {
    int t1 = i, t2 = i - 1, dd = gcd(gg[t1], gg[t2]);
    for (int j = 0; j < dd; j++) tt[j] = 0;
    for (int j = 0; j < gg[t1]; j++) tt[j % dd] |= t[t1][j];
    for (int j = 0; j < gg[t2]; j++) t[t2][j] = t[t2][j] | tt[j % dd];
  }
  for (int i = 1; i <= m; i++)
    for (auto j : t[i]) ss[i] += j;
  for (int i = 1; i <= n; i++) mx[i] = l[i] / gg[clr[i]] * ss[clr[i]];
  ll ans = 0;
  for (int i = 1; i <= n; i++) {
    int xx = 0, yy = 0;
    for (int j = 1; j <= n; j++)
      if (i != j) {
        if (sum[j] > mx[i])
          ++xx;
        else if (mx[i] < mx[j] || (mx[i] == mx[j] && i > j))
          ++yy;
      }
    if (xx >= aa) continue;
    for (int j = 0; j <= min(aa - xx - 1, yy); j++)
      ans = (ans + C(yy, j) * C(xx, bb - 1 - j)) % P;
  }
  cout << ans << endl;
  return 0;
}
