#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, p;
  cin >> n >> p;
  string str;
  cin >> str;
  vector<vector<int>> A(p, vector<int>(p, 0));
  for (int i = 0; i < p; ++i)
    for (int j = 0; j < p; ++j) cin >> A[i][j];
  vector<int> cnt(p, 0);
  for (int i = 0; i < n; ++i) ++cnt[str[i] - 'a'];
  string s = "";
  for (int i = 0; i < n; ++i)
    if (i == 0 || str[i] != str[i - 1]) s += str[i];
  n = s.size();
  vector<vector<int>> cant(p, vector<int>(1 << p, 0));
  for (int t = 0; t < 2; ++t) {
    for (char ch = 'a'; ch < 'a' + p; ++ch) {
      int i = 0;
      while (i < n && s[i] != ch) ++i;
      while (i < n) {
        int mask = 0;
        int j = i + 1;
        while (j < n && s[j] != ch) ++j;
        for (int k = i + 1; k <= min(n - 1, j); ++k) {
          if (!A[ch - 'a'][s[k] - 'a'] && !((mask >> (s[k] - 'a')) & 1)) {
            cant[ch - 'a'][mask] |= 1 << (s[k] - 'a');
          }
          mask |= 1 << (s[k] - 'a');
        }
        i = j;
      }
      for (int mask = 1; mask < 1 << p; ++mask) {
        if ((mask >> (ch - 'a')) & 1) continue;
        for (int i = 0; i < p; ++i)
          if ((mask >> i) & 1) {
            cant[ch - 'a'][mask] |= cant[ch - 'a'][mask ^ (1 << i)] & (~mask);
          }
      }
    }
    reverse(s.begin(), s.end());
  }
  vector<bool> can(1 << p, true);
  for (int mask = 1; mask < 1 << p; ++mask) {
    for (int i = 0; i < p; ++i) {
      if (!((mask >> i) & 1)) {
        if (cant[i][mask]) can[mask] = false;
      }
    }
    bool ok = false;
    for (int i = 0; i < p; ++i)
      if ((mask >> i) & 1) ok |= can[mask ^ (1 << i)];
    if (!ok) can[mask] = false;
  }
  int res = str.size() + 1;
  for (int mask = 0; mask < (1 << p) - 1; ++mask) {
    int cur = 0;
    int c = 0;
    for (int i = 0; i < p; ++i)
      if (!((mask >> i) & 1)) {
        cur += cnt[i];
        ++c;
      }
    if (can[mask]) {
      res = min(res, cur);
      if (c == 1) res = 0;
    }
  }
  cout << res << endl;
  return 0;
}
