#include <bits/stdc++.h>
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize("Ofast")
using namespace std;
namespace ywy {
int v[20][20];
char str[200001];
int bit[1048576], val[1048576];
vector<int> vec[30];
unsigned char dp[1048576], g[1048576], f[1048576];
void ywymain() {
  int n, p;
  cin >> n >> p;
  cin >> str + 1;
  for (register int i = 1; i <= n; i++) vec[str[i] - 'a'].push_back(i);
  for (register int i = 0; i < p; i++) {
    for (register int j = 0; j < p; j++) cin >> v[i][j];
  }
  for (register int i = 0; i < p; i++) {
    for (register int j = 0; j <= i; j++) {
      if (v[i][j]) continue;
      unsigned char bswitch = 0;
      for (register int k = 0; k < vec[i].size(); k++) {
        int s = 0;
        for (register int a = vec[i][k] + 1; a <= n; a++) {
          if (str[a] - 'a' == j) {
            g[s] = 1;
            bswitch = 1;
            break;
          }
          if (str[a] - 'a' == i) break;
          s |= (1 << (str[a] - 'a'));
        }
      }
      if (!bswitch) break;
      for (register int bit = 2; bit <= (1 << p); bit <<= 1) {
        for (register int a = 0; a < (1 << p); a += bit) {
          for (register int b = a; b < a + (bit >> 1); b++)
            g[b + (bit >> 1)] |= g[b];
        }
      }
      for (register int k = 0; k < (1 << p); k++) {
        if (!g[k]) continue;
        if ((k & (1 << i)) && (k & (1 << j))) continue;
        f[k] = 1;
        g[k] = 0;
      }
    }
  }
  for (register int i = 0; i < p; i++) bit[1 << i] = vec[i].size();
  for (register int i = 1; i < (1 << p); i++)
    val[i] = val[i - (i & -i)] + bit[i & -i];
  if (f[0]) {
    cout << n << endl;
    return;
  }
  dp[0] = 1;
  for (register int i = 0; i < (1 << p); i++) {
    if (!dp[i]) continue;
    for (register int j = 0; j < p; j++) {
      if (i & (1 << j)) continue;
      if (!f[i | (1 << j)]) dp[i | (1 << j)] = 1;
    }
  }
  int maxn = 0;
  for (register int i = 0; i < (1 << p); i++)
    if (dp[i]) maxn = max(maxn, val[i]);
  cout << n - maxn << endl;
}
}  // namespace ywy
int main() {
  ywy::ywymain();
  return (0);
}
