#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false), cin.tie(0);
  int n, p;
  string s;
  cin >> n >> p >> 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];
    }
  }
  vector<vector<int>> cnt(n + 1, vector<int>(p));
  for (int i = 0; i < n; i++) {
    cnt[i + 1] = cnt[i];
    ++cnt[i + 1][s[i] - 'a'];
  }
  vector<int> f(1 << p);
  vector<int> nex(p, n);
  for (int i = n - 1; i >= 0; i--) {
    for (int j = 0; j < p; j++)
      if (nex[j] < n && A[s[i] - 'a'][j] == 0) {
        int mask = (1 << p) - 1;
        for (int k = 0; k < p; k++)
          if (cnt[nex[j]][k] - cnt[i + 1][k] > 0) {
            mask -= 1 << k;
          }
        if ((mask & (1 << (s[i] - 'a'))) == 0) continue;
        f[mask] += 1;
        f[mask - (1 << j)] -= 1;
        f[mask - (1 << (s[i] - 'a'))] -= 1;
        f[mask - ((1 << (s[i] - 'a')) | (1 << j))] += 1;
      }
    nex[s[i] - 'a'] = i;
  }
  for (int i = 0; i < p; i++) {
    for (int j = 0; j < (1 << p); j++)
      if (j & (1 << i)) {
        f[j - (1 << i)] += f[j];
      }
  }
  int res = n;
  vector<int> dp(1 << p);
  dp.back() = 1;
  for (int S = (1 << p) - 1; S >= 0; S--)
    if (dp[S]) {
      int len = 0;
      for (int i = 0; i < p; i++)
        if (S & (1 << i)) {
          len += cnt[n][i] - cnt[0][i];
        }
      res = min(res, len);
      for (int i = 0; i < p; i++)
        if (S & (1 << i)) {
          if (f[S - (1 << i)] == 0) dp[S - (1 << i)] = 1;
        }
    }
  cout << res << endl;
  return 0;
}
