#include <bits/stdc++.h>
using namespace std;
const int N = 100000 + 10;
const int M = 1000000007;
const double eps = 1e-9;
const double PI = acos(-1);
const int oo = 1000000000;
int n, p, nxt[N][17], can[17][17], pre[N][17], fr[17];
bool bad[1 << 17], vis[1 << 17][17][17], dp[1 << 17];
char s[N];
vector<int> oc[17];
void go(int msk, int a, int b) {
  if (a > b) swap(a, b);
  if (vis[msk][a][b]) return;
  vis[msk][a][b] = bad[msk] = 1;
  bad[msk] = 1;
  for (int i = 0; i < p; ++i)
    if (msk & 1 << i && i - a && i - b) go(msk ^ 1 << i, a, b);
}
int main() {
  cin >> n >> p;
  scanf("%s", s);
  for (int i = 0; i < n; ++i) {
    oc[s[i] - 'a'].push_back(i);
    if (i)
      for (int j = 0; j < p; ++j) pre[i][j] = pre[i - 1][j];
    ++pre[i][s[i] - 'a'];
    ++fr[s[i] - 'a'];
  }
  for (int i = 0; i < p; ++i)
    for (int j = 0; j < p; ++j) scanf("%d", &can[i][j]);
  memset(nxt, -1, sizeof(nxt));
  for (int i = n - 2; i >= 0; --i) {
    for (int j = 0; j < p; ++j) nxt[i][j] = nxt[i + 1][j];
    nxt[i][s[i + 1] - 'a'] = i + 1;
  }
  for (int i = 0; i < p; ++i) {
    for (int j = 0; j < p; ++j) {
      if (can[i][j]) continue;
      for (int k = 0; k < oc[i].size(); ++k) {
        int d = oc[i][k];
        if (nxt[d][j] == -1) break;
        if (k < oc[i].size() - 1 && nxt[d][j] > oc[i][k + 1]) continue;
        int msk = (1 << p) - 1;
        if (nxt[d][j] > d + 1) {
          for (int t = 0; t < p; ++t) {
            int fr = pre[nxt[d][j] - 1][t];
            fr -= pre[d][t];
            if (fr) msk ^= 1 << t;
          }
        }
        go(msk, i, j);
      }
    }
  }
  dp[(1 << p) - 1] = 1;
  int ans = n;
  for (int i = (1 << p) - 2; i >= 0; --i) {
    if (bad[i]) continue;
    for (int j = 0; j < p && !dp[i]; ++j)
      if (!(i & 1 << j)) dp[i] |= dp[i ^ 1 << j];
    if (!dp[i]) continue;
    int cur = 0;
    for (int j = 0; j < p; ++j)
      if (i & 1 << j) cur += fr[j];
    ans = min(ans, cur);
  }
  cout << ans << endl;
  return 0;
}
