#include <bits/stdc++.h>
using namespace std;
bitset<1 << 17> bad = 0;
bitset<1 << 17> badnw = 0;
int n, p;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> p;
  vector<vector<int> > a(p, vector<int>(p));
  string s;
  cin >> s;
  for (int i = 0; i < p; i++)
    for (int j = 0; j < p; j++) cin >> a[i][j];
  vector<vector<int> > f(p);
  vector<vector<int> > pf(n + 1, vector<int>(p, 0));
  for (int i = 1; i <= n; i++) {
    pf[i] = pf[i - 1];
    pf[i][s[i - 1] - 'a']++;
    f[s[i - 1] - 'a'].push_back(i - 1);
  }
  for (int i = 0; i < p; i++) {
    for (int j = 0; j < p; j++) {
      if (!a[i][j]) {
        badnw = 0;
        badnw.flip();
        int next = 0;
        for (int c = 0; c < f[i].size(); c++) {
          while (next < f[j].size() && f[j][next] <= f[i][c]) next++;
          if (next == f[j].size()) continue;
          int mask = (1 << p) - 1;
          bool good = true;
          for (int k = 0; k < p; k++) {
            if (pf[f[j][next]][k] - pf[f[i][c] + 1][k]) {
              mask ^= (1 << k);
              good &= (i != k && j != k);
            }
          }
          if (good) {
            badnw[mask] = 0;
          }
        }
        for (int m = (1 << p) - 1; m >= 0; m--) {
          if (!badnw[m]) {
            for (int k = 0; k < p; k++) {
              if (j != k && i != k && (m & (1 << k))) badnw[m ^ (1 << k)] = 0;
            }
          }
        }
        badnw.flip();
        bad |= badnw;
      }
    }
  }
  bitset<1 << 17> dp = 0;
  dp[(1 << p) - 1] = 1;
  int ans = n;
  for (int i = (1 << p) - 2; i >= 0; i--) {
    if (bad[i]) {
      continue;
    }
    for (int add = 0; add < p; add++) {
      if (!(i & (1 << add))) {
        dp[i] = dp[i] | dp[i | (1 << add)];
      }
    }
    if (dp[i]) {
      int siz = 0;
      for (int have = 0; have < p; have++) {
        if ((1 << have) & i) siz += f[have].size();
      }
      ans = min(ans, siz);
    }
  }
  cout << ans << "\n";
  return 0;
}
