#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;
int S;
bool BJ[1 << 18];
int A[maxn];
int n, p;
char ss[maxn];
void gaogao(int x, int y) {
  int lst = 0;
  for (int i = 1; i <= n; i++) {
    if (A[lst] == x && A[i] == y) {
      int orz = (1 << (x - 1)) | (1 << (y - 1));
      int YO = S ^ orz;
      BJ[orz] = 1;
      for (int j = lst + 1; j < i; j++) YO &= (S ^ (1 << (A[j] - 1)));
      for (int o = YO; o; o = (o - 1) & YO) BJ[YO | o] = 1;
    }
    if (A[i] == x || A[i] == y) lst = i;
  }
}
int ANS;
int cnt[maxn];
void dfs(int s, int cc) {
  if (BJ[s]) return;
  BJ[s] = 1;
  ANS = min(ANS, cc);
  for (int i = 1; i <= p; i++) {
    if ((1 << (i - 1)) & s) dfs(s ^ (1 << (i - 1)), cc - cnt[i]);
  }
}
int main() {
  scanf("%d%d", &n, &p);
  S = (1 << p) - 1;
  scanf("%s", &ss[1]);
  for (int i = 1; i <= n; i++) {
    A[i] = ss[i] - 'a' + 1;
    cnt[A[i]]++;
  }
  for (int i = 1; i <= p; i++) {
    for (int j = 1; j <= p; j++) {
      int x;
      scanf("%d", &x);
      if (!x) {
        gaogao(i, j);
      }
    }
  }
  ANS = n;
  dfs(S, n);
  printf("%d", ANS);
}
