#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
const int maxp = 18;
int n, p, ans;
int lst[maxp], cnt[maxp], aft[maxp];
int a[maxp][maxp];
int dp[1 << maxp], del[1 << maxp], sum[1 << maxp];
char s[maxn], buf[maxp][maxp];
int main() {
  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[i][j]);
  memset(lst, -1, sizeof(lst));
  for (int i = (0); i < (n); ++i) {
    char c = s[i] - 'a';
    for (int j = (0); j < (p); ++j)
      if (!a[c][j] && ~lst[j]) {
        if (aft[j] >> c & 1) continue;
        --del[aft[j]];
        --del[aft[j] ^ (1 << c) ^ (1 << j)];
        ++del[aft[j] ^ (1 << c)];
        ++del[aft[j] ^ (1 << j)];
      }
    for (int j = (0); j < (p); ++j) aft[j] |= 1 << c;
    lst[c] = i;
    aft[c] = 0;
  }
  for (int msk = (0); msk < (1 << p); ++msk)
    for (int sub = msk; sub; (sub -= 1) &= msk) sum[msk] += del[sub];
  for (int i = (0); i < (n); ++i) ++cnt[s[i] - 'a'];
  dp[0] = true;
  for (int msk = (1); msk < (1 << p); ++msk)
    if (sum[msk] >= 0)
      for (int j = (0); j < (p); ++j)
        if ((msk >> j & 1) && dp[msk ^ (1 << j)]) dp[msk] = true;
  for (int msk = (0); msk < (1 << p); ++msk)
    if (dp[msk]) {
      int res = 0;
      for (int j = (0); j < (p); ++j)
        if (msk >> j & 1) res += cnt[j];
      ans = max(ans, res);
    }
  printf("%d\n", n - ans);
  return 0;
}
