#include <bits/stdc++.h>
using namespace std;
mt19937 Rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
template <typename T>
inline void chkmax(T &x, T y) {
  if (x < y) x = y;
}
template <typename T>
inline void chkmin(T &x, T y) {
  if (x > y) x = y;
}
inline int read() {
  int x = 0;
  char c = getchar();
  while (c < 48) c = getchar();
  while (c > 47) x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
  return x;
}
const int maxn = 1e5 + 10;
char str[maxn];
int n, m, lst[17], G[17][17], sum[17][maxn];
bitset<(1 << 17)> dp, vis, mark[17][17];
int main() {
  scanf("%d %d %s", &n, &m, str + 1);
  for (int i = (0), iend = (m - 1); i <= iend; ++i)
    for (int j = (0), jend = (m - 1); j <= jend; ++j) {
      G[i][j] = read();
    }
  int all = (1 << m) - 1;
  for (int i = (1), iend = (n); i <= iend; ++i) {
    int x = str[i] - 'a';
    for (int j = (0), jend = (m - 1); j <= jend; ++j) {
      sum[j][i] = sum[j][i - 1] + (x == j);
      if (G[x][j] || !lst[j]) continue;
      int S = all;
      for (int k = (0), kend = (m - 1); k <= kend; ++k) {
        if (k != x && k != j && sum[k][i - 1] - sum[k][lst[j]]) {
          S ^= 1 << k;
        }
      }
      mark[min(x, j)][max(x, j)][S] = 1;
    }
    lst[x] = i;
  }
  for (int p = (0), pend = (m - 1); p <= pend; ++p)
    for (int q = (p), qend = (m - 1); q <= qend; ++q)
      if (!G[p][q]) {
        for (int S = (all), Send = (0); S >= Send; --S)
          if (mark[p][q][S]) {
            vis[S] = 1;
            for (int i = (0), iend = (m - 1); i <= iend; ++i)
              if (i != p && i != q && S >> i & 1) {
                mark[p][q][S ^ 1 << i] = 1;
              }
          }
      }
  int res = n;
  dp[all] = 1;
  for (int S = (all), Send = (0); S >= Send; --S) {
    if (dp[S]) {
      int val = 0;
      for (int i = (0), iend = (m - 1); i <= iend; ++i)
        if (S >> i & 1) {
          val += sum[i][n], dp[S ^ 1 << i] = !vis[S ^ 1 << i];
        }
      chkmin(res, val);
    }
  }
  cout << res;
  return 0;
}
