#include <bits/stdc++.h>
using namespace std;
bool a[20][20];
string s;
bool dp[17][17][1 << 17];
bool dp2[1 << 17];
int cnt[17];
int occ[17];
int n, p;
void preprocess() {
  memset(occ, -1, sizeof occ);
  for (int i = (int)s.size() - 1; i >= 0; --i) {
    vector<pair<int, int> > v;
    for (int j = 0; j < p; ++j) {
      if (occ[j] != -1) {
        v.push_back(make_pair(occ[j], j));
      }
    }
    sort(v.begin(), v.end());
    int mask = 0;
    for (int j = 0; j < (int)v.size(); ++j) {
      int cur = s[i] - 'a';
      int nx = v[j].second;
      dp[cur][nx][mask] = 1;
      mask |= (1 << nx);
    }
    occ[s[i] - 'a'] = i;
  }
  for (int i = 0; i < p; ++i) {
    for (int j = 0; j < p; ++j) {
      for (int mask = 0; mask < (1 << p) - 1; ++mask) {
        if (!((mask >> i) & 1) and (!((mask >> j) & 1)) and dp[i][j][mask]) {
          for (int k = 0; k < p; ++k) {
            if (!((mask >> k) & 1)) {
              dp[i][j][mask | (1 << k)] = 1;
            }
          }
        }
      }
    }
  }
}
void solve() {
  cin >> n >> p;
  cin >> s;
  for (int i = 0; i < p; ++i) {
    for (int j = 0; j < p; ++j) {
      cin >> a[i][j];
    }
  }
  for (int i = 0; i < (int)s.size(); ++i) {
    cnt[s[i] - 'a']++;
  }
  preprocess();
  dp2[0] = 1;
  for (int i = 0; i < (1 << p) - 1; ++i) {
    if (dp2[i]) {
      for (int l = 0; l < p; ++l) {
        if (!((i >> l) & 1)) {
          int f = 1;
          for (int j = 0; j < p; ++j) {
            for (int k = 0; k < p; ++k) {
              if (l != j and l != k and (!((i >> k) & 1)) and
                  (!((i >> j) & 1))) {
                if (dp[j][k][i | (1 << l)] and !a[j][k]) {
                  f = 0;
                }
              }
            }
          }
          dp2[i | (1 << l)] = f;
        }
      }
    }
  }
  int ans = 1000000007;
  for (int i = 0; i < (1 << p); ++i) {
    if (dp2[i]) {
      int cur = (int)s.size();
      for (int j = 0; j < p; ++j) {
        if ((i >> j) & 1) {
          cur -= cnt[j];
        }
      }
      ans = min(ans, cur);
    }
  }
  cout << ans << '\n';
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  while (t--) {
    solve();
  }
  return 0;
}
