#include <bits/stdc++.h>
using namespace std;
const int mod = 1000000007;
int n, p, i, j, cnt[18], nxt[100005][18], a[20][20];
int dp[1 << 17], mask, mask1, flag, d[1 << 17][17], ans;
string s;
int main() {
  ios_base ::sync_with_stdio();
  cin.tie(0);
  cin >> n >> p >> s;
  for (i = 0; i < p; i++) {
    for (j = 0; j < p; j++) cin >> a[i][j];
  }
  for (i = n - 1; i >= 0; i--) {
    for (j = 0; j < p; j++) {
      if (i + 1 == n)
        nxt[i][j] = n;
      else
        nxt[i][j] = (s[i + 1] - 'a' == j) ? i + 1 : nxt[i + 1][j];
    }
  }
  for (i = 0; i < n; i++) {
    int x = s[i] - 'a', y;
    vector<pair<int, int> > v;
    mask = 0;
    cnt[x]++;
    for (j = 0; j < p; j++) {
      if (nxt[i][j] != n && nxt[i][j] <= nxt[i][x]) v.push_back({nxt[i][j], j});
    }
    sort(v.begin(), v.end());
    for (j = 0; j < v.size(); j++) {
      y = v[j].second;
      if (a[x][y] == 0) d[mask][max(y, x)] |= 1 << min(x, y);
      mask |= 1 << y;
    }
  }
  fill(dp, dp + (1 << n), 1000000000LL);
  dp[0] = n;
  ans = 1000000000LL;
  for (mask = 0; mask < (1 << p); mask++) {
    for (i = 0; i < p; i++) {
      int mask1 = mask ^ (1 << i);
      if (mask1 > mask) continue;
      for (j = 0; j < p; j++) d[mask][j] |= d[mask1][j];
    }
    flag = 0;
    for (i = 0; i < p; i++) {
      for (j = 0; j <= i; j++) {
        if (d[mask][i] & (1 << j)) {
          if ((mask & (1 << i)) | (mask & (1 << j)))
            ;
          else {
            flag = 1;
            break;
          }
        }
      }
      if (flag) break;
    }
    if (dp[mask] != 1000000000LL && !flag) {
      for (i = 0; i < p; i++) {
        mask1 = mask ^ (1 << i);
        if (mask1 < mask) continue;
        dp[mask1] = dp[mask] - cnt[i];
      }
      ans = min(ans, dp[mask]);
    }
  }
  cout << ans;
  return 0;
}
