#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int x = 0, neg = 1;
  char c = getchar();
  while (!isdigit(c)) {
    if (c == '-') neg = -1;
    c = getchar();
  }
  while (isdigit(c)) x = x * 10 + c - '0', c = getchar();
  return x * neg;
}
inline void print(int x) {
  if (x < 0) {
    putchar('-');
    print(abs(x));
    return;
  }
  if (x <= 9)
    putchar(x + '0');
  else {
    print(x / 10);
    putchar(x % 10 + '0');
  }
}
inline int qpow(int x, int e, int _MOD) {
  int ans = 1;
  while (e) {
    if (e & 1) ans = ans * x % _MOD;
    x = x * x % _MOD;
    e >>= 1;
  }
  return ans;
}
int n = read(), k = read();
char s[100005];
bool vis[1 << 17], is[1 << 17], hav[1 << 17];
inline void deal(int x) {
  if (vis[x]) return;
  vis[x] = 1;
  for (int i = 0; i <= k - 1; i++) {
    if (x >> i & 1) {
      int t = (x ^ (1 << i));
      deal(t);
      is[x] |= is[t];
    }
  }
}
inline void calc(int x, int y) {
  memset(is, 0, sizeof(is));
  memset(vis, 0, sizeof(vis));
  x--;
  y--;
  int l = 0;
  for (int i = 1; i <= n; i++) {
    if (s[i] - 'a' == x || s[i] - 'a' == y) {
      l = i;
      break;
    }
  }
  if (!l) return;
  int r = l + 1;
  while (l <= n) {
    int sta = 0;
    while (r <= n) {
      if ((s[l] - 'a' + s[r] - 'a') == x + y)
        break;
      else if (s[l] == s[r]) {
        l = r;
        r++;
        sta = 0;
        continue;
      }
      sta |= (1 << (s[r] - 'a'));
      r++;
    }
    if (r > n) break;
    is[sta] = 1;
    l = r;
    r++;
  }
  int sta = (1 << k) - 1;
  sta ^= (1 << x);
  if (x != y) sta ^= (1 << y);
  deal(sta);
  for (int i = 0; i < 1 << k; i++) hav[i] |= is[i];
}
int cnt[20];
signed main() {
  cin >> s + 1;
  for (int i = 1; i <= n; i++) cnt[s[i] - 'a']++;
  for (int i = 1; i <= k; i++) {
    for (int j = 1; j <= k; j++) {
      int t = read();
      if (!t) calc(i, j);
    }
  }
  int ans = 0;
  for (int i = 1; i < (1 << k); i++) {
    if (!hav[i]) {
      hav[i] = 1;
      for (int j = 0; j <= k - 1; j++) {
        if (i >> j & 1 && !hav[i ^ (1 << j)]) hav[i] = 0;
      }
      if (!hav[i]) {
        int sum = 0;
        for (int j = 0; j <= k - 1; j++)
          if (i >> j & 1) sum += cnt[j];
        ans = max(ans, sum);
      }
    }
  }
  cout << n - ans << endl;
  return 0;
}
