#include <bits/stdc++.h>
using namespace std;
const int P = 17;
int chk[1 << P], CNT[P], vis[1 << P];
queue<int> q;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, p;
  cin >> n >> p;
  string s;
  cin >> s;
  vector<vector<int> > A(p, vector<int>(p));
  for (int i = 0; i < p; i++)
    for (int j = 0; j < p; j++) cin >> A[i][j];
  int cnt = 1;
  for (int i = 0; i < n; i++) CNT[s[i] - 'a']++;
  for (int i = 0; i < p; i++) {
    for (int j = 0; j < p; j++) {
      if (A[i][j] == 1) continue;
      for (int k = 0; k < n; k++) {
        if (s[k] == i + 'a') {
          int idx = k + 1;
          int z = (1 << p) - 1;
          while (idx < n) {
            if (s[idx] == i + 'a' && i != j) {
              k = idx - 1;
              break;
            } else if (s[idx] == j + 'a') {
              if (vis[z] != cnt) q.push(z);
              vis[z] = cnt;
              k = idx;
              if (i == j) k = idx - 1;
              break;
            } else {
              z &= ((1 << p) - 1 - (1 << (s[idx] - 'a')));
            }
            idx++;
            if (idx == n) k = idx;
          }
        }
      }
      while (!q.empty()) {
        int tmp = q.front();
        chk[tmp] = 1;
        q.pop();
        for (int it = 0; it < p; it++) {
          if (it != i && it != j && (tmp >> it) & 1 &&
              vis[tmp ^ (1 << it)] != cnt) {
            vis[tmp ^ (1 << it)] = cnt;
            q.push(tmp ^ (1 << it));
          }
        }
      }
      cnt++;
    }
  }
  int cur = (1 << p) - 1;
  for (int i = 0; i < cur + 1; i++) vis[i] = 0;
  q.push(cur);
  vis[cur] = 1;
  int ans = (int)s.size();
  while (!q.empty()) {
    cur = q.front();
    q.pop();
    int tmp = 0;
    for (int i = 0; i < p; i++)
      if ((cur >> i) & 1) tmp += CNT[i];
    ans = min(ans, tmp);
    for (int i = 0; i < p; i++) {
      if ((cur >> i) & 1) {
        tmp = cur ^ (1 << i);
        if (vis[tmp] == 0 && chk[tmp] == 0) {
          vis[tmp] = 1;
          q.push(tmp);
        }
      }
    }
  }
  cout << ans;
  return 0;
}
