#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
const int INF = 2000000000;
const long long BIG = 1446803456761533460LL;
const int maxn = 100010;
const int maxp = 17;
int N, P, A[maxn], dp[1 << maxp], cnt[1 << maxp];
bool forb[1 << maxp], M[maxp][maxp], temp[1 << maxp];
void calc(int a, int b) {
  memset(temp, 0, sizeof temp);
  int lst = -1;
  int mask = (1 << P) - 1;
  for (int i = 0; i < N; ++i) {
    mask &= -1 ^ (1 << A[i]);
    if (A[i] == a || A[i] == b) {
      if (lst != -1 && (A[lst] == a && A[i] == b || A[lst] == b && A[i] == a)) {
        temp[mask] = 1;
      }
      mask = (1 << P) - 1;
      mask &= -1 ^ (1 << A[i]);
      lst = i;
    }
  }
  int m = ((1 << P) - 1) ^ ((1 << a) | (1 << b));
  for (int sb = (1 << P) - 1; sb >= 0; --sb)
    if (temp[sb]) {
      forb[sb | (1 << a) | (1 << b)] = 1;
      for (int i = 0; i < P; ++i)
        if (sb >> i & 1) {
          temp[sb ^ (1 << i)] = 1;
        }
    }
}
int main() {
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin >> N >> P;
  string str;
  cin >> str;
  for (int i = 0; i < N; ++i) A[i] = str[i] - 'a', ++cnt[1 << A[i]];
  for (int i = 0; i < P; ++i)
    for (int j = 0; j < P; ++j) cin >> M[i][j];
  for (int i = 0; i < P; ++i)
    for (int j = 0; j < i + 1; ++j)
      if (!M[i][j]) calc(i, j);
  for (int mask = 1; mask < 1 << P; ++mask) {
    int lb = mask & -mask;
    dp[mask] = dp[mask ^ lb] + cnt[lb];
  }
  for (int mask = 0; mask < 1 << P; ++mask)
    if (!forb[mask]) {
      for (int i = 0; i < P; ++i)
        if ((mask >> i & 1) && (!forb[mask ^ (1 << i)])) {
          dp[mask] = min(dp[mask], (dp[mask ^ (1 << i)]));
        }
    }
  cout << dp[(1 << P) - 1] << '\n';
}
