#include <bits/stdc++.h>
int N, P, A[17][17];
char S[100001];
int bad[1 << 17];
int dp[1 << 17];
int F[17];
int main() {
  scanf("%d %d %s", &N, &P, S);
  int all = 0;
  for (int i = 0; i < N; i++) {
    S[i] -= 'a';
    F[S[i]]++;
    all |= 1 << S[i];
  }
  for (int i = 0; i < P; i++) {
    for (int j = 0; j < P; j++) {
      scanf("%d", &A[i][j]);
    }
  }
  for (int i = 0; i < P; i++) {
    for (int j = 0; j < P; j++) {
      if (A[i][j]) continue;
      int k = 0;
      int m = 0;
      while (k < N && S[k] != i && S[k] != j) k++;
      while (k < N) {
        int l = S[k];
        k++;
        int m = 0;
        if (S[k] == i) {
          while (k < N && S[k] != j) m |= 1 << S[k++];
        } else {
          while (k < N && S[k] != i) m |= 1 << S[k++];
        }
        if (k != N) {
          bad[m] = 1;
        }
      }
    }
  }
  for (int i = 0; i < 1 << P; i++) {
    for (int j = 0; j < P; j++) {
      bad[i | 1 << j] |= bad[i];
    }
  }
  bad[all] = 0;
  dp[0] = 1;
  for (int i = 0; i < 1 << P; i++) {
    for (int j = 0; j < P; j++) {
      if ((~i >> j & 1) && !bad[i | 1 << j]) {
        dp[i | 1 << j] = dp[i];
      }
    }
  }
  int ans = 0;
  for (int i = 0; i < 1 << P; i++)
    if (dp[i]) {
      int s = 0;
      for (int j = 0; j < P; j++)
        if (i >> j & 1) s += F[j];
      if (ans < s) ans = s;
    }
  printf("%d\n", N - ans);
  return 0;
}
