#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7, inf = 1061109567;
const long long infll = 4557430888798830399;
const int N = 1e5 + 5, P = 17;
string s;
int n, p, a[P][P], f[(1 << P) + 5], dp[(1 << P) + 5], nxt[N][P], cnt[P];
bool bad[(1 << P) + 5][P][P];
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> p >> s;
  for (int i = 0; i < s.length(); i++) cnt[s[i] - 'a']++;
  for (int i = 0; i < p; i++)
    for (int j = 0; j < p; j++) cin >> a[i][j];
  memset(nxt, -1, sizeof nxt);
  for (int i = n - 1; i >= 0; i--) {
    for (int j = 0; j < p; j++) nxt[i][j] = nxt[i + 1][j];
    nxt[i][s[i] - 'a'] = i;
  }
  for (int i = 0; i < n; i++) {
    vector<pair<int, int>> v;
    for (int j = 0; j < p; j++)
      if (nxt[i + 1][j] != -1) v.push_back(pair<int, int>(nxt[i + 1][j], j));
    sort(v.begin(), v.end());
    int msk = 0;
    for (int j = 0; j < v.size(); j++) {
      if (!a[s[i] - 'a'][v[j].second]) {
        int tmp1 = s[i] - 'a', tmp2 = v[j].second;
        if (tmp1 > tmp2) swap(tmp1, tmp2);
        bad[msk][tmp1][tmp2] = 1;
      }
      msk |= (1 << v[j].second);
    }
  }
  for (int i = 0; i < p; i++)
    for (int j = i; j < p; j++) {
      for (int msk = 0; msk < (1 << p); msk++)
        if (bad[msk][i][j] && !(msk & (1 << i)) && !(msk & (1 << j))) {
          for (int k = 0; k < p; k++)
            if (k != i && k != j) {
              bad[msk | (1 << k)][i][j] = 1;
            }
        }
    }
  for (int msk = 0; msk < (1 << p); msk++) {
    f[msk] = 1;
    for (int i = 0; i < p; i++)
      for (int j = i; j < p; j++)
        if (!(msk & (1 << i)) && !(msk & (1 << j))) {
          if (bad[msk][i][j]) f[msk] = 0;
        }
  }
  for (int i = 0; i < (1 << p); i++) {
    if (!f[i]) continue;
    if (i == 0) {
      dp[i] = 1;
      continue;
    }
    dp[i] = 0;
    for (int j = 0; j < p; j++)
      if ((i & (1 << j)) && dp[i ^ (1 << j)]) {
        dp[i] = 1;
        break;
      }
  }
  int ans = inf;
  for (int i = 0; i < (1 << p); i++)
    if (dp[i]) {
      int len = s.length();
      for (int j = 0; j < p; j++)
        if (i & (1 << j)) len -= cnt[j];
      ans = min(ans, len);
    }
  cout << ans;
  return 0;
}
