#include <bits/stdc++.h>
using namespace std;
const int N = 100050;
int pos[20][N];
int mat[20][20];
char str[N];
int cnt[20];
int V[(1 << 17) + 5];
int dp[(1 << 17) + 5];
int n, p;
bool ck[(1 << 17) + 5][18][18], ck2[(1 << 17) + 5][18];
vector<int> X[(1 << 17) + 10][18], Y[(1 << 17) + 10];
vector<int> hh[20][20];
int dpp[(1 << 17) + 10];
int rec1(int ms, int i, int j) {
  if (ms == (1 << p) - 1) return 1;
  int &ret = dpp[ms];
  if (ret != -1) return ret;
  ret = 1;
  for (int x = 0; x < p; x++) {
    if (x == i || x == j || (ms & (1 << x))) continue;
    ret = rec1(ms ^ (1 << x), i, j);
  }
  return ret;
}
int rec(int ms) {
  if (ms == (1 << p) - 1) return 0;
  int &ret = dp[ms];
  if (ret != -1) return ret;
  ret = 0;
  for (int i = 0; i < p; i++) {
    if (ms & (1 << i)) continue;
    int tms = ms | (1 << i);
    if (V[tms] == 0) ret = max(ret, cnt[i] + rec(tms));
  }
  return ret;
}
int main() {
  scanf("%d %d", &n, &p);
  scanf("%s", str);
  for (int i = 0; i < p; i++) {
    for (int j = 0; j < p; j++) scanf("%d", &mat[i][j]);
  }
  memset(pos, -1, sizeof pos);
  for (int i = n - 1; i >= 0; i--) {
    cnt[str[i] - 'a']++;
    for (int j = 0; j < p; j++) pos[j][i] = pos[j][i + 1];
    pos[str[i] - 'a'][i] = i;
  }
  for (int i = 0; i < n; i++) {
    int ms = 0;
    int ch = str[i] - 'a';
    int nw = i + 1, f = 0;
    while (nw < n) {
      int mn = N;
      for (int j = 0; j < p; j++) {
        if (pos[j][nw] == -1 || (ms & (1 << j))) continue;
        mn = min(mn, pos[j][nw]);
      }
      if (mn == N) break;
      int ch1 = str[mn] - 'a';
      nw = mn + 1;
      if (mat[ch][ch1] == 0) {
        if (ck[ms][ch][ch1] == 1) continue;
        hh[ch][ch1].push_back(ms);
        X[ms][ch].push_back(ch1);
        ck[ms][ch][ch1] = 1;
        if (ck2[ms][ch] == 1) continue;
        Y[ms].push_back(ch);
        ck2[ms][ch] = 1;
      }
      ms |= (1 << ch1);
    }
  }
  for (int i = 0; i < p; i++) {
    for (int j = 0; j < p; j++) {
      memset(dpp, -1, sizeof dpp);
      for (int k = 0; k < hh[i][j].size(); k++) {
        int ms = hh[i][j][k];
        rec1(ms, i, j);
      }
      for (int k = 0; k < (1 << p); k++) {
        if (dpp[k] != -1) V[k] = 1;
      }
    }
  }
  memset(dp, -1, sizeof dp);
  int ret = n - rec(0);
  printf("%d\n", ret);
}
