#include <bits/stdc++.h>
using namespace std;
int n, p, mp[20][20], pre[20], cnt[100005][20];
int ban[1 << 17], usd[1 << 17][20][20], can[1 << 17], sum[1 << 17];
char s[100005];
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", &mp[i][j]);
  memset(pre, -1, sizeof pre);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < p; j++)
      cnt[i][j] = (s[i] - 'a' == j) + (i ? cnt[i - 1][j] : 0);
    for (int j = 0; j < p; j++)
      if (!mp[s[i] - 'a'][j] && pre[j] >= 0 && pre[s[i] - 'a'] <= pre[j]) {
        int sta = 0;
        for (int k = 0; k < p; k++)
          if (cnt[i - 1][k] - cnt[pre[j]][k]) sta |= (1 << k);
        usd[sta][s[i] - 'a'][j] |= 1;
      }
    pre[s[i] - 'a'] = i;
  }
  for (int sta = 0; sta < (1 << p); sta++)
    for (int i = 0; i < p; i++)
      for (int j = 0; j < p; j++)
        if (usd[sta][i][j]) {
          int rsta = (1 << p) - 1 - sta - (1 << i) - (i != j ? (1 << j) : 0);
          for (int S = rsta;; S = (S - 1) & (rsta)) {
            ban[sta | S] = 1;
            if (!S) break;
          }
        }
  int ans = n;
  can[0] = 1;
  for (int sta = 0; sta < (1 << p); sta++) {
    if (can[sta] && !ban[sta]) {
      ans = min(ans, n - sum[sta]);
      for (int i = 0; i < p; i++)
        if (((1 << i) & sta) == 0)
          can[sta | (1 << i)] = 1,
                          sum[sta | (1 << i)] = sum[sta] + cnt[n - 1][i];
    }
  }
  printf("%d\n", ans);
}
