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