#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, p;
  cin >> n >> p;
  string s;
  cin >> s;
  vector<vector<int>> mat(p, vector<int>(p, 0));
  for (int i = 0; i < p; i++) {
    for (int j = 0; j < p; j++) {
      cin >> mat[i][j];
    }
  }
  vector<int> valid(1 << p, 1);
  for (int fi = 0; fi < p; fi++) {
    for (int se = 0; se < p; se++) {
      if (mat[fi][se]) continue;
      vector<int> Va(1 << p, 1);
      for (int i = 0; i < n;) {
        for (; i < n && s[i] - 'a' != fi; i++)
          ;
        i++;
        int found = 0;
        int cur = 0;
        for (; i < n; i++) {
          if (s[i] - 'a' == fi && fi != se) cur = 0;
          if (s[i] - 'a' == se) {
            found = 1;
            break;
          }
          cur |= (1 << (s[i] - 'a'));
        }
        if (found) {
          Va[cur] = 0;
        }
      }
      for (int i = 0; i < (1 << p); i++) {
        if (Va[i]) continue;
        valid[i] = 0;
        for (int j = 0; j < p; j++) {
          if (j != fi && j != se && !(i & (1 << j))) {
            Va[i | (1 << j)] = 0;
          }
        }
      }
    }
  }
  vector<int> dp(1 << p, 0);
  dp[0] = 1;
  queue<int> q;
  q.push(0);
  while (!q.empty()) {
    int nd = q.front();
    q.pop();
    for (int i = 0; i < p; i++) {
      if (!(nd & (1 << i))) {
        int nxt = nd | (1 << i);
        if (valid[nxt] && !dp[nxt]) {
          dp[nxt] = 1;
          q.push(nxt);
        }
      }
    }
  }
  vector<int> fr(p, 0);
  for (int i = 0; i < n; i++) {
    fr[s[i] - 'a']++;
  }
  int ans = n;
  for (int i = 0; i < (1 << p); i++) {
    if (!dp[i]) continue;
    int cur = n;
    for (int j = 0; j < p; j++) {
      if (i & (1 << j)) {
        cur -= fr[j];
      }
    }
    ans = min(ans, cur);
  }
  cout << ans << '\n';
  return 0;
}
