#include <bits/stdc++.h>
using namespace std;
int n, m, ans, tt[17], ls[17], p[17][17], a[1 << 17];
set<pair<int, int> > se;
char s[100010];
bool r[17][17][1 << 17], v[1 << 17];
int main() {
  scanf("%d%d%s", &n, &m, s + 1);
  int nn = (1 << m) - 1;
  for (int i = (0); i <= (m - 1); ++i)
    for (int j = (0); j <= (m - 1); ++j) scanf("%d", &p[i][j]);
  for (int i = (n); i >= (1); --i) {
    int x = 0, c = s[i] - 'a';
    ++tt[c];
    for (pair<int, int> j : se) {
      if (!p[c][j.second]) r[max(c, j.second)][min(c, j.second)][x] = 1;
      x |= 1 << j.second;
    }
    if (ls[c]) se.erase({ls[c], c});
    ls[c] = i;
    se.insert({i, c});
  }
  for (int i = (0); i <= (m - 1); ++i)
    for (int j = (0); j <= (i); ++j) {
      int m1 = 1 << i, m2 = 1 << j;
      for (int k = (0); k <= (nn); ++k)
        if (r[i][j][k]) {
          for (int d = (~k) & nn, t = d & (-d); t; d ^= t, t = d & (-d))
            if (t != m1 && t != m2) r[i][j][k | t] = 1;
          v[k] = 1;
        }
    }
  for (int i = (1); i <= (nn); ++i) a[i] = -n;
  for (int i = (0); i <= (nn); ++i)
    if (!v[i]) {
      ans = max(ans, a[i]);
      for (int d = (~i) & nn, t = d & (-d); t; d ^= t, t = d & (-d))
        if (!v[i | t]) a[i | t] = max(a[i | t], a[i] + tt[__builtin_ctz(t)]);
    }
  printf("%d\n", n - ans);
}
