#include <bits/stdc++.h>
using namespace std;
const int MAX_P = 17;
int n, p, a[MAX_P][MAX_P], f[1 << MAX_P], cnt[MAX_P];
bool visited[1 << MAX_P], ok[1 << MAX_P];
string s;
void readInput() {
  cin >> n >> p >> s;
  for (int i = 0; i < p; ++i) {
    for (int j = 0; j < p; ++j) cin >> a[i][j];
  }
}
void visit(int x, int t1, int t2) {
  visited[x] = true;
  for (int i = 0; i < p; ++i) {
    if (i != t1 && i != t2) {
      int tmp = (x & (~(1 << i)));
      if (!visited[tmp]) visit(tmp, t1, t2);
    }
  }
}
void init() {
  memset(ok, true, sizeof(ok));
  for (int i = 0; i < p; ++i) {
    for (int j = 0; j < p; ++j) {
      if (a[i][j]) continue;
      memset(visited, false, sizeof(visited));
      int mask = (1 << p) - 1, prv = -1;
      for (int t = 0; t < n; ++t) {
        if (s[t] - 'a' != i && s[t] - 'a' != j)
          mask = mask & (~(1 << (s[t] - 'a')));
        else {
          if ((s[t] - 'a' == i && prv == j) || (s[t] - 'a' == j && prv == i))
            visit(mask, i, j);
          prv = s[t] - 'a';
          mask = (1 << p) - 1;
        }
      }
      for (int x = 0; x < 1 << p; ++x) ok[x] = ok[x] && !visited[x];
    }
  }
  for (auto c : s) ++cnt[c - 'a'];
}
int dp(int mask) {
  if (f[mask] > -1) return f[mask];
  f[mask] = 0;
  for (int i = 0; i < p; ++i) {
    if (((mask >> i) & 1)) {
      int newMask = mask & (~(1 << i));
      if (ok[newMask]) f[mask] = max(f[mask], dp(newMask) + cnt[i]);
    }
  }
  return f[mask];
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  readInput();
  init();
  memset(f, -1, sizeof(f));
  cout << n - dp((1 << p) - 1);
}
