#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma loop - opt(on)
using namespace std;
vector<long long int> cnt;
vector<long long int> dp, mask;
vector<vector<long long int>> pp;
bitset<20> vis;
long long int n, p, sz;
string s;
void print2(long long int a) {
  for (long long int i = 0; i <= p - 1; i++) cout << ((a >> i) & 1);
  puts("");
}
long long int count_(long long int m) {
  long long int ans = 0;
  for (long long int i = 0; i <= p - 1; i++)
    if ((m >> i) & 1) ans += cnt[i];
  return ans;
}
void mark(long long int m, long long int a, long long int b) {
  for (long long int i = m; i > 0; i = (i - 1) & m) {
    if (!((m >> a) & 1) || !((m >> b) & 1)) continue;
    if (mask[i]) break;
    mask[i] = 1;
  }
  return;
}
void build() {
  for (long long int i = 0; i <= n - 1; i++) cnt[s[i] - 'a']++;
  for (long long int i = 0; i <= p - 1; i++)
    for (long long int j = 0; j <= p - 1; j++) {
      if (pp[i][j]) continue;
      long long int a = 0, b = 1, m;
      while (a < n && b < n) {
        m = 0, vis.reset();
        while (a < n && s[a] - 'a' != i) a++;
        b = a + 1;
        while (b < n && s[b] - 'a' != j) b++;
        if (a == n || b == n) break;
        for (long long int k = a + 1; k <= b - 1; k++) vis[s[k] - 'a'] = 1;
        for (long long int k = 0; k <= p - 1; k++) m |= ((1 << k) * (!vis[k]));
        mark(m, s[a] - 'a', s[b] - 'a');
        a = b;
      }
    }
  return;
}
long long int solve() {
  dp[(1 << p) - 1] = 1;
  long long int ans = count_((1 << p) - 1);
  for (long long int i = (1 << p) - 1; i >= 0; i--) {
    if (mask[i]) continue;
    for (long long int j = 0; j <= p - 1; j++) {
      if (!((i >> j) & 1)) dp[i] |= (dp[(1 << j) | i]);
    }
  }
  for (long long int i = 0; i <= (1 << p) - 1; i++) {
    if (dp[i]) ans = min(ans, count_(i));
  }
  return ans;
}
signed main() {
  ios::sync_with_stdio(false), cin.tie(0);
  cin >> n >> p >> s, sz = (1 << p);
  dp.resize(sz), mask.resize(sz), cnt.resize(p);
  pp.resize(p, vector<long long int>(p, 0));
  for (long long int i = 0; i <= p - 1; i++)
    for (long long int j = 0; j <= p - 1; j++) cin >> pp[i][j];
  build();
  cout << solve() << "\n";
  return 0;
}
