#include <bits/stdc++.h>
using namespace std;
int n, p;
char s[100000 + 2];
int adj[17];
int cnt[20];
int del[1 << 17];
int delcnt[1 << 17];
bool OK(int m) {
  char oldc;
  int j = 0;
  while (j < n && (m & (1 << s[j]))) j++;
  if (j == n) return true;
  while (j < n) {
    while (j < n && !(m & (1 << s[j]))) {
      oldc = s[j];
      j++;
    }
    if (j == n) break;
    while (j < n && (m & (1 << s[j]))) j++;
    if (j == n) break;
    if (!(adj[oldc] & (1 << s[j]))) return false;
  }
  return true;
}
void dfs(int m, int x, int y) {
  del[m] = 0;
  for (int b = 0; b < p; b++) {
    if (b == x || b == y || (m & (1 << b))) continue;
    dfs(m | (1 << b), x, y);
  }
}
void Init0() {
  for (int m = 0; m < (1 << p); m++) del[m] = -1;
  for (int x = 0; x < p; x++) {
    for (int y = 0; y < p; y++) {
      if (adj[x] & (1 << y)) continue;
      int l, r;
      int m;
      l = 0;
      while (l < n) {
        m = 0;
        while (l < n && s[l] != x) l++;
        if (l >= n) break;
        r = l + 1;
        while (r < n && s[r] != y) {
          if (s[r] == x)
            m = 0;
          else
            m |= (1 << s[r]);
          r++;
        }
        if (r >= n) break;
        del[m] = 0;
        dfs(m, x, y);
        l = r;
      }
    }
  }
}
void Init1() {
  del[0] = 1;
  delcnt[0] = 0;
  for (int m = 0; m < (1 << p); m++) {
    if (del[m] == 0) continue;
    if (del[m] == 1)
      for (int b = 0; b < p; b++) {
        int mplus = m | (1 << b);
        if (mplus != m && del[mplus] == -1) {
          del[mplus] = 1;
          delcnt[mplus] = delcnt[m] + cnt[b];
        }
      }
  }
  for (int m = 0; m < (1 << p); m++) printf("%d ", del[m]);
  printf("\n");
}
int GetAns() {
  int ans = 0;
  for (int m = 0; m < (1 << p); m++)
    if (del[m] == 1 && delcnt[m] > ans) ans = delcnt[m];
  return n - ans;
}
int main() {
  int a;
  scanf("%d%d", &n, &p);
  scanf("%s", s);
  for (int i = 0; i < p; i++)
    for (int j = 0; j < p; j++) {
      scanf("%d", &a);
      if (a == 1) adj[i] |= (1 << j);
    }
  for (int i = 0; i < n; i++) {
    s[i] -= 'a';
    cnt[s[i]]++;
  }
  Init0();
  Init1();
  printf("%d\n", GetAns());
  return 0;
}
