#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int n, p;
string s;
int a[17][17];
int u[1 << 17];
int num[1 << 17];
int dp[1 << 17];
int cnt[17];
int main() {
  ios::sync_with_stdio(false), cin.tie(0);
  cin >> n >> p >> s;
  for (int i = 0; i < p; i++)
    for (int j = 0; j < p; j++) cin >> a[i][j];
  for (int i = 0; i < n; i++) cnt[s[i] - 'a']++;
  int mask = (1 << p) - 1;
  for (int i = 0; i < p; i++)
    for (int j = i; j < p; j++)
      if (!a[i][j]) {
        vector<int> d(1 << p);
        int last1 = -1, last2 = -1;
        int now = 0;
        for (int z = 0; z < n; z++) {
          if (s[z] == 'a' + i)
            last2 = last1, last1 = z;
          else if (s[z] == 'a' + j)
            last2 = last1, last1 = z;
          if (last1 != -1 && last2 != -1 && last1 == z &&
              ((s[last1] == 'a' + i && s[last2] == 'a' + j) ||
               (s[last1] == 'a' + j && s[last2] == 'a' + i))) {
            d[now ^ mask] = 1;
          }
          now |= 1 << (s[z] - 'a');
          if (last1 == z) now = 0;
        }
        for (int k = mask; k >= 0; k--)
          if (d[k]) {
            u[k ^ mask] = 1;
            for (int l = 0; l < p; l++)
              if (l != i && l != j && (k & (1 << l))) {
                d[k ^ (1 << l)] = 1;
              }
          }
      }
  num[0] = n;
  dp[0] = 1;
  int ans = n;
  for (int i = 0; i < 1 << p; i++)
    if (!u[i] && dp[i]) {
      ans = min(ans, num[i]);
      for (int j = 0; j < p; j++)
        if (!(i & (1 << j))) {
          int k = i ^ (1 << j);
          num[k] = num[i] - cnt[j];
          dp[k] = 1;
        }
    }
  cout << ans << endl;
  return 0;
}
