#include <bits/stdc++.h>
using namespace std;
int n, p, A[18][18], cnt[100005][18], pre[18], ans = 1e9;
bool f[1 << 18], ban[1 << 18], cant[18][18][1 << 18];
char s[100005];
int main() {
  scanf("%d%d%s", &n, &p, s + 1);
  for (int i = 0; i < p; i++)
    for (int j = 0; j < p; j++) scanf("%d", &A[i][j]);
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < p; j++) cnt[i][j] = cnt[i - 1][j];
    cnt[i][s[i] -= 'a']++;
    for (int j = 0; j < p; j++)
      if (!A[s[i]][j] && pre[j] && pre[j] >= pre[s[i]]) {
        int st = 0;
        for (int k = 0; k < p; k++)
          if (cnt[i - 1][k] - cnt[pre[j]][k]) st |= 1 << k;
        cant[s[i]][j][st] = 1;
      }
    pre[s[i]] = i;
  }
  for (int i = 0; i < p; i++)
    for (int j = 0; j < p; j++)
      for (int st = 0; st < 1 << p; st++)
        if (cant[i][j][st]) {
          int S = ((1 << p) - 1) ^ (st | (1 << i) | (1 << j));
          for (int t = S;; t = (t - 1) & S) {
            ban[t | (1 << i) | (1 << j)] = 1;
            if (!t) break;
          }
        }
  f[(1 << p) - 1] = 1;
  for (int st = (1 << p) - 1; st >= 0; st--)
    if (f[st]) {
      int sum = 0;
      for (int i = 0; i < p; i++)
        if ((st & 1 << i)) {
          if (!ban[st ^ (1 << i)]) f[st ^ (1 << i)] = 1;
          sum += cnt[n][i];
        }
      ans = min(ans, sum);
    }
  printf("%d\n", ans);
}
