#include <bits/stdc++.h>
int min(int a, int b) { return a < b ? a : b; }
int main() {
  static char cc[100000 + 1], ok[1 << 17], dp[1 << 17];
  static int adj[17][17], kk[17], prev[17];
  static long long bb[1 << 17][5];
  int n, p, h, i, j, x, y, z, b, ans;
  scanf("%d%d%s", &n, &p, cc);
  for (x = 0; x < p; x++)
    for (y = 0; y < p; y++) scanf("%d", &adj[x][y]);
  memset(prev, -1, p * sizeof *prev);
  for (i = 0; i < n; i++) {
    x = cc[i] - 'a';
    kk[x]++;
    for (y = 0; y < p; y++)
      if (!adj[x][y] && (j = prev[y]) != -1) {
        int xy;
        b = 0;
        for (z = 0; z < p; z++)
          if (prev[z] > j) b |= 1 << z;
        xy = x < y ? x * p + y : y * p + x;
        bb[b][xy / 64] |= 1LL << xy % 64;
      }
    prev[x] = i;
  }
  for (x = 0; x < p; x++)
    for (b = 0; b < 1 << p; b++)
      if ((b & 1 << x) != 0) {
        int b_ = b ^ 1 << x;
        for (h = 0; h < 5; h++) bb[b][h] |= bb[b_][h];
      }
  for (b = 0; b < 1 << p; b++) {
    ok[b] = 1;
    for (x = 0; x < p; x++)
      for (y = x; y < p; y++)
        if (!adj[x][y] && (b & 1 << x) == 0 && (b & 1 << y) == 0) {
          int xy = x * p + y;
          if ((bb[b][xy / 64] & 1LL << xy % 64) != 0) {
            ok[b] = 0;
            goto out;
          }
        }
  out:;
  }
  dp[0] = 1;
  ans = n;
  for (b = 0; b < 1 << p; b++) {
    int len;
    if (!dp[b]) continue;
    len = 0;
    for (x = 0; x < p; x++)
      if ((b & 1 << x) == 0) len += kk[x];
    ans = min(ans, len);
    for (x = 0; x < p; x++)
      if ((b & 1 << x) == 0 && ok[b | 1 << x]) dp[b | 1 << x] = 1;
  }
  printf("%d\n", ans);
  return 0;
}
