#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx,avx2,sse,sse2")
using namespace std;
time_t current_time;
const int N = 2e6 + 10, mod = 1e9 + 7, inf = 1e9;
int mat[20][20], n, p;
string s;
int charset[17][1 << 17];
int done[17][1 << 17][17];
void addall(char ch1, int mask, char ch2) {
  if (done[ch1 - 'a'][mask][ch2 - 'a']) return;
  done[ch1 - 'a'][mask][ch2 - 'a'] = 1;
  charset[ch1 - 'a'][mask] = 1;
  for (int i = 0; i < p; ++i) {
    if (i != (ch1 - 'a') and i != (ch2 - 'a') and ((mask >> i) & 1) == 0) {
      addall(ch1, mask + (1 << i), ch2);
    }
  }
}
int memo[1 << 18], counts[20];
int dp(int mask) {
  if (~memo[mask]) return memo[mask];
  int &ans = memo[mask];
  ans = inf;
  if (mask == ((1 << p) - 1)) {
    ans = 0;
    return ans;
  }
  for (int i = 0; i < p; ++i) {
    if (((mask >> i) & 1) == 0 and charset[i][mask]) {
      ans = inf;
      return ans;
    }
  }
  ans = n;
  for (int i = 0; i < p; ++i) {
    if ((mask >> i) & 1) ans -= counts[i];
  }
  for (int i = 0; i < p; ++i) {
    if ((mask >> i) & 1) continue;
    ans = min(ans, dp(mask + (1 << i)));
  }
  return ans;
}
void solve() {
  memset(memo, -1, sizeof(memo));
  memset(done, 0, sizeof(done));
  memset(charset, 0, sizeof(charset));
  cin >> n >> p >> s;
  for (int i = 0; i < p; ++i) {
    for (int j = 0; j < p; ++j) cin >> mat[i][j];
  }
  set<pair<int, char> > indices;
  unordered_map<char, int> mapto;
  for (char ch = 'a'; ch < 'r'; ch++) {
    indices.insert({n, ch});
    mapto[ch] = n;
  }
  memset(counts, 0, sizeof(counts));
  for (int i = n - 1; i >= 0; --i) {
    int currmask = 0;
    counts[s[i] - 'a']++;
    for (auto &[ind, ch] : indices) {
      if (ind == n) break;
      if (mat[s[i] - 'a'][ch - 'a'] == 0) {
        addall(s[i], currmask, ch);
      }
      currmask += (1 << (ch - 'a'));
      if (ch == s[i]) break;
    }
    indices.erase({mapto[s[i]], s[i]});
    indices.insert({i, s[i]});
    mapto[s[i]] = i;
  }
  int ans = dp(0);
  if (ans == inf)
    cout << 0 << "\n";
  else
    cout << ans << "\n";
}
signed main() {
  ios::sync_with_stdio(false);
  cout.tie(0);
  cin.tie(0);
  current_time = time(NULL);
  int t = 1;
  while (t--) solve();
}
