#include <bits/stdc++.h>
using namespace std;
template <class T>
inline void read(T &k) {
  k = 0;
  int flag = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') flag = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    k = k * 10 + c - '0';
    c = getchar();
  }
  k *= flag;
}
const int N = 100005;
struct node {
  int v, o;
  node(){};
  node(int x) {
    v = x;
    o = 0;
    while (x) {
      if (x & 1) ++o;
      x >>= 1;
    }
  }
  bool operator<(const node &k) const { return o < k.o; }
} f[1 << 17];
int n, m, k, s, t, p, h, b[17][17];
bitset<(1 << 17)> a[17][17];
int d[N][17], e[17], sum[17], g[1 << 17];
char c[N];
int aaa(int x, int y, int v) {
  if (x > y) swap(x, y);
  a[x][y][v] = b[x][y] ^ 1;
  return x == y;
}
int main() {
  read(n);
  read(p);
  cin >> c + 1;
  for (int i = 0; i < p; ++i)
    for (int j = 0; j < p; ++j) read(b[i][j]);
  for (int i = 0; i < p; ++i) e[i] = n + 1;
  for (int i = n; i >= 1; --i) {
    for (int j = 0; j < p; ++j) d[i][j] = e[j];
    e[c[i] - 'a'] = i;
  }
  for (int i = 1; i <= n; ++i) {
    memset(e, 0, sizeof(e));
    int x = i;
    s = 0;
    while (x <= n) {
      t = n + 1;
      for (int j = 0; j < p; ++j)
        if (e[j] == 0 && d[x][j] < t) {
          t = d[x][j];
        }
      if (t > n || aaa(c[i] - 'a', c[t] - 'a', s)) break;
      s |= 1 << (c[t] - 'a');
      e[c[t] - 'a'] = 1;
      x = t;
    }
  }
  for (int i = 1; i < (1 << p); ++i) {
    f[i] = node(i);
  }
  sort(f + 1, f + (1 << p));
  for (int i = 0; i < p; ++i)
    for (int j = i; j < p; ++j)
      for (int k = 1; k < (1 << p); ++k) {
        s = h = f[k].v;
        t = 0;
        while (s && a[i][j][h] == 0) {
          if (s & 1) a[i][j][h] = a[i][j][h ^ (1 << t)];
          s >>= 1;
          ++t;
        }
      }
  for (int i = 1; i <= n; ++i) sum[c[i] - 'a']++;
  int tot = 0, bo, boo, ans = 0;
  memset(g, -1, sizeof(g));
  g[0] = 0;
  for (int i = 1; i < (1 << p); ++i) {
    tot = 0;
    for (int j = 0; j < p; ++j)
      if ((i & (1 << j)) == 0) e[++tot] = j;
    bo = 1;
    for (int l = 1; l <= tot; ++l)
      for (int r = l; r <= tot; ++r)
        if (a[e[l]][e[r]][i] == 1) {
          bo = 0;
          break;
        }
    if (bo) {
      boo = 1;
      for (int j = 0; j < p; ++j)
        if ((i & (1 << j)) && g[i ^ (1 << j)] >= 0) {
          boo = 0;
          g[i] = g[i ^ (1 << j)] + sum[j];
          ans = max(ans, g[i]);
          break;
        }
    }
  }
  cout << n - ans << endl;
}
