#include <bits/stdc++.h>
using namespace std;
const int maxP = 17;
const int maxN = 100010;
const int maxS = 1 << 17;
bool f[maxP][maxP], dp0[maxS], dp1[maxS], dp2[maxS];
int cnt[maxN], n, m;
char s[maxN];
int main() {
  cin >> n >> m >> s;
  for (int i = 0; i < n; i++) cnt[int(s[i] -= 'a')]++;
  for (int i = 0; i < m; i++)
    for (int j = 0; j < m; j++) cin >> f[i][j];
  int k = 1 << m;
  fill(dp1, dp1 + k, true);
  for (int u = 0; u < m; u++)
    for (int v = 0; v < m; v++) {
      if (f[u][v] == true) continue;
      fill(dp0, dp0 + k, true);
      for (int i = 0; i < n;) {
        while (i < n && s[i] != u) i++;
        int mask = 0, j = i + 1;
        while (j < n && s[j] != u && s[j] != v) mask |= 1 << s[j++];
        if (n <= j) break;
        if (s[j] == v) dp0[mask] = false;
        i = j;
      }
      for (int i = 0; i < k; i++) {
        if (dp0[i] == true) continue;
        for (int j = 0; j < m; j++) {
          if (j == u || j == v) continue;
          dp0[1 << j | i] = false;
        }
        dp1[i] = false;
      }
    }
  int ans = n;
  dp2[0] = true;
  for (int i = 0; i < k; i++) {
    if (dp2[i] == false) continue;
    if (dp1[i] == false) continue;
    int cur = 0;
    for (int j = 0; j < m; j++) {
      if (i >> j & 1) continue;
      dp2[1 << j | i] = true;
      cur += cnt[j];
    }
    ans = min(ans, cur);
  }
  cout << ans << endl;
  return 0;
}
