#include <bits/stdc++.h>
using namespace std;
const int maxS = (1 << 17) + 10;
const int maxn = 1e5 + 10;
char s[maxn];
int ans = 1 << 30;
int n, m, vis[maxS], gg[maxS], tempg[maxS], da[maxn], cnt[maxS], c[20][20];
void dfs(int s) {
  if (vis[s]) return;
  vis[s] = 1;
  for (int i = 0; i < m; ++i)
    if (s & (1 << i)) {
      dfs(s ^ (1 << i));
      tempg[s] |= tempg[s ^ (1 << i)];
    }
}
void Solve(int a, int b) {
  memset(tempg, 0, sizeof(tempg));
  for (int i = 1, j, s; i <= n;)
    if (da[i] == a) {
      for (j = i + 1, s = 0; j <= n; ++j) {
        if (da[j] == a || da[j] == b) break;
        s |= 1 << da[j];
      }
      if (j > n) break;
      if (da[j] == b) {
        i = j + 1;
        tempg[s] = 1;
      }
      if (da[j] == a) i = j;
    } else
      ++i;
  memset(vis, 0, sizeof(vis));
  if (a == b)
    dfs(((1 << m) - 1) ^ (1 << a));
  else
    dfs(((1 << m) - 1) ^ (1 << a) ^ (1 << b));
  for (int i = 0; i < (1 << m); ++i) gg[i] |= tempg[i];
}
void dfs2(int s) {
  if (vis[s]) return;
  vis[s] = 1;
  ans = min(ans, cnt[((1 << m) - 1) ^ s]);
  for (int i = 0; i < m; ++i)
    if ((s & (1 << i)) == 0 && !gg[s | (1 << i)]) dfs2(s | (1 << i));
}
int main() {
  scanf("%d%d", &n, &m);
  scanf("%s", s);
  for (int i = 0; i < m; ++i)
    for (int j = 0; j < m; ++j) scanf("%d", &c[i][j]);
  for (int i = 0; i < n; ++i) {
    da[i + 1] = s[i] - 'a';
    ++cnt[1 << da[i + 1]];
  }
  da[0] = da[n + 1] = -1;
  for (int i = 0; i < (1 << m); ++i)
    for (int j = 0; j < m; ++j)
      if (i & (1 << j)) {
        cnt[i] = cnt[i ^ (1 << j)] + cnt[1 << j];
        break;
      }
  for (int i = 0; i < m; ++i)
    for (int j = 0; j < m; ++j)
      if (!c[i][j]) Solve(i, j);
  memset(vis, 0, sizeof(vis));
  dfs2(0);
  printf("%d\n", ans);
  return 0;
}
