#include <bits/stdc++.h>
using namespace std;
int n, p, nxt[111111][22], msk[111111][22], ans, num[22], cur;
int f[111111], fake[111111], t[111111];
char s[111111];
int fl[22][22];
void FWT_or(int a[], int flg) {
  for (int i = 2; i <= (1 << p); i <<= 1) {
    for (int P = i >> 1, j = 0; j < (1 << p); j += i) {
      for (int k = j; k < j + P; k++) {
        a[k + P] += a[k] * flg;
      }
    }
  }
}
int main() {
  scanf("%d%d", &n, &p);
  scanf("%s", s);
  for (int i = 0; i < p; i++) {
    for (int j = 0; j < p; j++) {
      scanf("%d", &fl[i][j]);
    }
  }
  for (int i = 0; i < n; i++) num[s[i] - 'a']++;
  for (int i = 0; i < p; i++) nxt[n - 1][i] = n;
  for (int i = n - 2; i >= 0; i--) {
    for (int j = 0; j < p; j++) {
      nxt[i][j] = nxt[i + 1][j];
    }
    nxt[i][s[i + 1] - 'a'] = i + 1;
  }
  for (int i = n - 1; i >= 0; i--) {
    for (int j = 0; j < p; j++) {
      if (nxt[i][j] == i + 1)
        msk[i][j] = 0;
      else
        msk[i][j] = msk[i + 1][j] | (1 << (s[i + 1] - 'a'));
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < p; j++) {
      if (!fl[s[i] - 'a'][j] && nxt[i][j] < n) {
        f[msk[i][j]] = f[msk[i][j]] | ((1 << (s[i] - 'a')) | (1 << j));
      }
    }
  }
  for (int i = 0; i < (1 << p); i++) {
    int hmsk = (1 << p) - 1 - i;
    for (int j = hmsk; j; j = (j - 1) & hmsk) {
      if (((i | j) & f[i]) == f[i]) continue;
      fake[i | j] = 1;
    }
    if ((i & f[i]) != f[i]) fake[i] = 1;
  }
  for (int i = 1; i < (1 << p); i++) {
    t[i] = 1;
    if (fake[i]) continue;
    for (int j = 0; j < p; j++) {
      if (i & (1 << j)) {
        if (!t[i ^ (1 << j)]) t[i] = 0;
      }
    }
  }
  ans = 1e9;
  for (int i = 0; i < (1 << p); i++) {
    if (!t[i]) {
      cur = 0;
      for (int j = 0; j < p; j++) {
        if (!(i & (1 << j))) cur += num[j];
      }
      ans = min(ans, cur);
    }
  }
  printf("%d\n", ans);
  return 0;
}
