#include <bits/stdc++.h>
using namespace std;
const int iinf = 1 << 29;
const long long inf = 1ll << 60;
const long long mod = 1e9 + 7;
void GG() {
  cout << "-1\n";
  exit(0);
}
long long mpow(long long a, long long n, long long mo = mod) {
  long long re = 1;
  while (n > 0) {
    if (n & 1) re = re * a % mo;
    a = a * a % mo;
    n >>= 1;
  }
  return re;
}
long long inv(long long b, long long mo = mod) {
  if (b == 1) return b;
  return (mo - mo / b) * inv(mo % b) % mo;
}
const int maxn = 1e5 + 5;
bool bad[17][17][1 << 17];
bool A[17][17];
int nof[17];
bool ok[1 << 17];
int v[maxn];
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  ;
  int n, p;
  cin >> n >> p;
  for (int i = 0; i < n; ++i) {
    char c;
    cin >> c;
    v[i] = c - 'a';
    ++nof[v[i]];
  }
  for (int i = 0; i < p; ++i)
    for (int j = 0; j < p; ++j) cin >> A[i][j];
  for (int i = 0; i < p; ++i) {
    for (int j = i; j < p; ++j) {
      if (!A[i][j]) {
        int M = -1;
        for (int k = 0; k < n; ++k) {
          if (v[k] == i || v[k] == j) {
            if (M != -1) {
              bad[i][j][M] = 1;
            }
            M = 0;
          } else {
            if (M != -1) M |= (1 << v[k]);
          }
        }
        for (int msk = 1; msk < (1 << p); ++msk) {
          for (int it = 0; it < p; ++it) {
            if (msk & (1 << it)) {
              bad[i][j][msk] |= bad[i][j][msk ^ (1 << it)];
            }
          }
        }
      }
    }
  }
  int re = 0;
  for (int msk = 0; msk < (1 << p); ++msk) {
    bool flag = 1;
    for (int i = 0; i < p; ++i) {
      for (int j = i; j < p; ++j) {
        if (!(msk & (1 << i)) && !(msk & (1 << j))) {
          if (bad[i][j][msk]) {
            flag = 0;
            break;
          }
        }
      }
    }
    if (!flag) continue;
    ;
    if (msk) {
      flag = 0;
      for (int i = 0; i < p; ++i) {
        if (msk & (1 << i)) {
          flag |= ok[msk ^ (1 << i)];
        }
      }
    }
    if (flag) {
      ok[msk] = 1;
      int ans = 0;
      for (int i = 0; i < p; ++i) {
        if (msk & (1 << i)) ans += nof[i];
      };
      re = max(re, ans);
    }
  }
  cout << n - re << '\n';
}
