#include <bits/stdc++.h>
using namespace std;
int g[30], n, p, f[300005], len, c[300005], num[30], ans;
bool Vis[300005], vis[30], a[30][30];
char s[300005];
vector<int> V;
queue<int> Q;
void work(int u, int v) {
  int pos, cnt = 0, tmp = 0;
  V.clear();
  while (!Q.empty()) Q.pop();
  for (int i = 0; i < len; i++)
    if (s[i] == u + 'a') {
      pos = i + 1;
      while (pos < len && s[pos] != v + 'a') {
        if (s[pos] != v + 'a' && s[pos] == s[i]) break;
        if (!vis[s[pos] - 'a'])
          tmp += g[s[pos] - 'a'], c[++cnt] = s[pos] - 'a',
                                  vis[s[pos] - 'a'] = 1;
        pos++;
      }
      if (s[pos] != v + 'a' && s[pos] == s[i]) {
        for (int j = 1; j <= cnt; j++) vis[c[j]] = 0;
        i = pos - 1;
        continue;
      }
      if (pos >= len) {
        for (int j = 1; j <= cnt; j++) vis[c[j]] = 0;
        break;
      }
      if (!Vis[tmp]) {
        Vis[tmp] = 1;
        V.push_back(tmp);
      }
      for (int j = 1; j <= cnt; j++) vis[c[j]] = 0;
      cnt = tmp = 0;
    }
  for (auto o : V) Q.push(o);
  V.clear();
  while (!Q.empty()) {
    int x = Q.front();
    Q.pop();
    V.push_back(x);
    f[x] = 1;
    for (int i = 0; i < p; i++)
      if (i != u && i != v && !(x & g[i]) && !Vis[x | g[i]]) {
        Vis[x | g[i]] = 1;
        Q.push(x | g[i]);
        V.push_back(x | g[i]);
      }
  }
  for (auto o : V) Vis[o] = 0;
}
int main() {
  g[0] = 1;
  for (int i = 1; i <= 18; i++) g[i] = g[i - 1] * 2;
  scanf("%d%d", &n, &p);
  scanf("%s", s);
  len = strlen(s);
  for (int i = 0; i < len; i++) num[s[i] - 'a']++;
  for (int i = 0; i < p; i++)
    for (int j = 0; j < p; j++) {
      scanf("%d", &a[i][j]);
    }
  for (int i = 0; i < p; i++) {
    for (int j = 0; j < p; j++)
      if (a[i][j] == 0) {
        work(i, j);
      }
  }
  ans = 0;
  for (int i = 1; i < (1 << p); i++)
    if (!f[i]) {
      f[i] = 1;
      for (int j = 0; j < p; j++)
        if (i & g[j]) f[i] &= f[i - g[j]];
    }
  ans = len;
  for (int i = 0; i < (1 << p); i++)
    if (!f[i]) {
      int tmp = len;
      for (int j = 0; j < p; j++)
        if (g[j] & i) tmp -= num[j];
      ans = min(ans, tmp);
    }
  printf("%d\n", ans);
}
