#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10, maxp = 17;
int A[maxp][maxp];
char s[maxn], t[maxn];
set<int> S;
int cnt[maxp];
int g[maxp][maxp][1 << maxp];
int f[1 << maxp];
int n, maskp, p, ans;
int main() {
  scanf("%d%d", &n, &p);
  scanf("%s", s);
  maskp = (1 << p) - 1;
  for (int i = 0; i < p; i++)
    for (int j = 0; j < p; j++) scanf("%d", &A[i][j]);
  for (int i = 0; i < n; i++) {
    int mask = 0, x = s[i] - 'a', lastpos = 1;
    cnt[x]++;
    for (auto y : S) {
      int p = s[-y] - 'a';
      if (p == x) lastpos = y;
      if (!A[x][p]) g[x][p][mask] = g[p][x][mask] = 1;
      mask |= (1 << p);
    }
    if (lastpos != 1) S.erase(lastpos);
    S.insert(-i);
  }
  for (int i = 0; i < p; i++)
    for (int j = i; j < p; j++)
      for (int k = 0; k <= maskp; k++)
        if (g[i][j][k]) {
          int maski = 1 << i, maskj = 1 << j;
          for (int mask = ((~k) & maskp), t = mask & -mask; mask;
               mask ^= t, t = mask & -mask)
            if (t != maski && t != maskj) g[i][j][k | t] = 1;
        }
  ans = n;
  f[0] = 1;
  for (int k = 0; k <= maskp; k++) {
    int m = 0, flg = 1;
    if (!f[k]) continue;
    for (int i = 0; i < p; i++)
      for (int j = i; j < p; j++)
        if (!(k & (1 << i)) && !(k & (1 << j)))
          if (g[i][j][k]) flg = 0;
    if (!flg) continue;
    for (int i = 0; i < p; i++)
      if (!(k & (1 << i))) m += cnt[i];
    ans = min(ans, m);
    for (int i = 0; i < p; i++)
      if (!(k & (1 << i))) f[k ^ (1 << i)] = 1;
  }
  printf("%d\n", ans);
  return 0;
}
