#include <bits/stdc++.h>
using namespace std;
const int MAX_P = 17, MAX_N = 1e5;
int N, P;
char rS[MAX_N + 1];
vector<int> S;
int A[MAX_P][MAX_P];
bool vst[1 << MAX_P], memo[1 << MAX_P], dp[1 << MAX_P];
int cnt[MAX_P];
int main() {
  scanf("%d %d", &N, &P);
  {
    scanf("%s", rS);
    for (int i = (0); i < (N); i++) {
      S.push_back(rS[i] - 'a');
      cnt[S[i]]++;
    }
  }
  for (int i = (0); i < (P); i++) {
    for (int j = (0); j < (P); j++) {
      cin >> A[i][j];
    }
  }
  memset(vst, true, sizeof(vst));
  for (int a = (0); a < (P); a++) {
    for (int b = (0); b < (P); b++) {
      if (A[a][b] == 0) {
        memset(memo, false, sizeof(memo));
        for (int i = 0; i < N;) {
          int m = 0;
          while (i < N && S[i] != a) i++;
          if (i == N) break;
          i++;
          while (i < N && S[i] != a && S[i] != b) {
            m |= 1 << S[i];
            i++;
          }
          if (i == N) break;
          if (S[i] == b) {
            int m2 = 0;
            for (int j = (0); j < (P); j++) {
              if (((m >> j) & 1) == 0) {
                m2 += 1 << j;
              }
            }
            m2 |= 1 << a;
            m2 |= 1 << b;
            memo[m2] = true;
          }
        }
        for (int bit = (1 << P) - 1; bit >= 0; bit--) {
          if (memo[bit]) {
            vst[bit] = false;
            for (int p = (0); p < (P); p++) {
              if (p != a && p != b && (bit >> p) & 1) {
                memo[bit & (~(1 << p))] = true;
              }
            }
          }
        }
      }
    }
  }
  memset(dp, false, sizeof(dp));
  dp[(1 << P) - 1] = true;
  int ans = N;
  for (int bit = (1 << P) - 1; bit >= 0; bit--) {
    if (dp[bit]) {
      int sm = 0;
      for (int p = (0); p < (P); p++) {
        if ((bit >> p) & 1) {
          sm += cnt[p];
        }
      }
      ans = min(ans, sm);
      for (int p = (0); p < (P); p++) {
        if ((bit >> p) & 1 && vst[bit & (~(1 << p))]) {
          dp[bit & (~(1 << p))] = true;
        }
      }
    }
  }
  cout << ans << endl;
}
