#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<int> cant(1 << p, 0);
  for (char ch = 'a'; ch < 'a' + p; ++ch) {
    int i = 0;
    while (i < n && str[i] != ch) ++i;
    while (i < n) {
      int mask = 0;
      int j = i + 1;
      while (j < n && str[j] != ch) ++j;
      for (int k = i + 1; k <= min(n - 1, j); ++k) {
        if (!A[ch - 'a'][str[k] - 'a']) {
          cant[mask] |= 1 << (str[k] - 'a');
        }
        mask |= 1 << (str[k] - 'a');
      }
      i = j;
    }
  }
  for (int mask = 1; mask < 1 << p; ++mask) {
    for (int i = 0; i < p; ++i)
      if ((mask >> i) & 1) {
        cant[mask] |= cant[mask ^ (1 << i)] & (~mask);
      }
  }
  vector<bool> can(1 << p, false);
  can[0] = true;
  for (int mask = 1; mask < 1 << p; ++mask) {
    for (int i = 0; i < p; ++i) {
      if (((mask >> i) & 1) && cant[mask] == 0 && can[mask ^ (1 << i)])
        can[mask] = true;
    }
  }
  int res = str.size() + 1;
  for (int mask = 0; mask < 1 << p; ++mask) {
    if (can[mask]) {
      int cur = 0;
      for (int i = 0; i < p; ++i)
        if (!((mask >> i) & 1)) cur += cnt[i];
      res = min(res, cur);
    }
  }
  cout << res << endl;
  return 0;
}
