#include <bits/stdc++.h>
using namespace std;
int n, p;
char s[100000 + 2];
int adj[20][20];
int cnt[20];
char perm[20];
int deleted;
int ans;
bool OK(char ch) {
  char oldc;
  int j = 0;
  while (s[j] < 0 || s[j] == ch) j++;
  if (j == n) return true;
  oldc = s[j];
  while (j < n) {
    while (s[j] != ch) {
      if (s[j] > 0) oldc = s[j];
      j++;
    }
    if (j == n) return true;
    while (s[j] < 0 || s[j] == ch) j++;
    if (j == n) break;
    if (!adj[oldc][s[j]]) return false;
    j++;
  }
  return true;
}
void toggle(char ch) {
  for (int j = 0; j < n; j++)
    if (s[j] == ch) s[j] *= -1;
}
void swap(char &a, char &b) {
  char t = a;
  a = b;
  b = t;
}
void dfs(int t) {
  if (t > p) return;
  for (int i = t; i <= p; i++) {
    char ch = perm[i];
    if (cnt[ch] && OK(ch)) {
      swap(perm[t], perm[ch]);
      toggle(ch);
      deleted += cnt[ch];
      if (n - deleted < ans) ans = n - deleted;
      dfs(t + 1);
      deleted -= cnt[ch];
      toggle(-ch);
      swap(perm[t], perm[ch]);
    }
  }
}
int main() {
  scanf("%d%d", &n, &p);
  scanf("%s", s);
  for (int i = 1; i <= p; i++)
    for (int j = 1; j <= p; j++) scanf("%d", &adj[i][j]);
  for (int i = 0; i < n; i++) {
    s[i] -= ('a' - 1);
    cnt[s[i]]++;
  }
  ans = n;
  deleted = 0;
  for (int i = 1; i <= p; i++) perm[i] = i;
  dfs(1);
  printf("%d\n", ans);
  return 0;
}
