#include <bits/stdc++.h>
using namespace std;
inline int read() {
  long long x = 0, f = 1;
  char c = getchar();
  for (; !isdigit(c); c = getchar())
    if (c == '-') f = -1;
  for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
  return x * f;
}
const int MAXN = 300010;
const int INF = 1e9;
int N, P;
char str[MAXN + 1];
int a[MAXN + 1], c[99];
int d[99][99];
bool f[MAXN + 1], g[MAXN + 1], h[MAXN + 1];
inline void solve(int x, int y) {
  for (int i = 0; i < (1 << P); i++) f[i] = 0;
  int last = 0;
  bool vis = 0;
  for (int i = N; i >= 1; i--) {
    if (a[i] == x && vis) {
      if (last & (1 << x)) continue;
      f[last] = 1;
      vis = 0;
    }
    last |= (1 << a[i]);
    if (a[i] == y) last = 0, vis = 1;
  }
  for (int i = 0; i < P; i++)
    for (int j = 0; j < (1 << P); j++)
      if (j & (1 << i)) f[j] |= f[j ^ (1 << i)];
  return;
}
int main() {
  N = read(), P = read();
  scanf("%s", str + 1);
  for (int k = 0; k < (1 << P); k++) g[k] = 1;
  for (int i = 1; i <= N; i++) a[i] = str[i] - 'a', c[a[i]]++;
  for (int i = 0; i < P; i++)
    for (int j = 0; j < P; j++) d[i][j] = read();
  for (int i = 0; i < P; i++)
    for (int j = 0; j < P; j++)
      if (!d[i][j]) {
        solve(i, j);
        for (int k = 0; k < (1 << P); k++) {
          if (!(k & (1 << i)) && !(k & (1 << j)) && f[k])
            g[((1 << P) - 1) ^ k] = 0;
        }
      }
  h[(1 << P) - 1] = 1;
  int ans = N;
  for (int i = (1 << P) - 1; i >= 0; i--) {
    if (h[i]) {
      for (int j = 0; j < P; j++)
        if ((i & (1 << j)) && g[i ^ (1 << j)]) h[i ^ (1 << j)] = 1;
    }
  }
  for (int i = 0; i < (1 << P); i++) {
    if (!h[i]) continue;
    int sum = 0;
    for (int j = 0; j < P; j++)
      if (i & (1 << j)) sum += c[j];
    ans = min(ans, sum);
  }
  printf("%d\n", ans);
  return 0;
}
