#include <bits/stdc++.h>
using namespace std;
char s[100010];
int n, p, a[17][17], cnt[1 << 17];
bool ban[1 << 17], dp[1 << 17], vis[1 << 17];
void dfs(int S) {
  if (vis[S]) return;
  vis[S] = 1;
  for (int i = 0; i < p; i++) {
    if ((S >> i) & 1) {
      dfs(S ^ (1 << i));
      dp[S] |= dp[S ^ (1 << i)];
    }
  }
}
void init(int x, int y) {
  memset(dp, 0, sizeof(dp));
  int l = -1;
  for (int i = 0; i < n; i++) {
    if (s[i] - 'a' == x || s[i] - 'a' == y) {
      l = i;
      break;
    }
  }
  if (l == -1) return;
  int S = 0, r = l + 1;
  while (l < n) {
    while (r < n) {
      if (s[r] - 'a' == x + y - s[l] + 'a')
        break;
      else if (s[r] == s[l]) {
        l = r, r++, S = 0;
        continue;
      }
      S |= (1 << (s[r++] - 'a'));
    }
    if (r >= n) break;
    dp[S] = 1;
    l = r, r++;
    S = 0;
  }
  int sta = (1 << p) - 1;
  sta ^= 1 << x;
  if (x != y) sta ^= 1 << y;
  memset(vis, 0, sizeof(vis));
  dfs(sta);
  for (int i = 1; i < (1 << p); i++) ban[i] |= dp[i];
}
int main() {
  scanf("%d%d%s", &n, &p, s);
  for (int i = 0; i < p; i++)
    for (int j = 0; j < p; j++) scanf("%d", &a[i][j]);
  for (int i = 0; i < n; i++) cnt[1 << (s[i] - 'a')]++;
  for (int i = 1; i < (1 << p); i++) {
    for (int j = 0; j < p; j++) {
      if (!((i >> j) & 1)) {
        cnt[i | (1 << j)] = cnt[i] + cnt[1 << j];
      }
    }
  }
  for (int i = 0; i < p; i++) {
    for (int j = 0; j < p; j++) {
      if (!a[i][j]) init(i, j);
    }
  }
  int ans = 0;
  for (int i = 1; i < (1 << p); i++) {
    if (ban[i]) continue;
    ban[i] = 1;
    for (int j = 0; j < p; j++) {
      if (((i >> j) & 1) && !ban[i ^ (1 << j)]) {
        ban[i] = 0;
        ans = max(ans, cnt[i]);
        break;
      }
    }
  }
  printf("%d\n", n - ans);
  return 0;
}
