#include <bits/stdc++.h>
using namespace std;
const int N = 112345;
const int P = 17 + 1;
int n, p, a[P][P];
string s;
int invalid[1 << P], len[1 << P];
int cnt[P];
bool vis[1 << P];
int id(char c) { return c - 'a'; }
void make_invalid(int mask, int a, int b) {
  invalid[mask] = true;
  vis[mask] = true;
  for (int i = 0; i < p; i++) {
    if (((1 << i) & mask) && i != a && i != b && !vis[mask ^ (1 << i)])
      make_invalid(mask ^ (1 << i), a, b);
  }
}
void dfs(int mask, int &minm) {
  minm = min(minm, len[mask]);
  vis[mask] = true;
  for (int i = 0; i < p; i++) {
    if (((1 << i) & mask) && !invalid[mask ^ (1 << i)] && !vis[mask ^ (1 << i)])
      dfs(mask ^ (1 << i), minm);
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> n >> p >> s;
  for (int i = 0; i < p; i++) {
    for (int j = 0; j < p; j++) cin >> a[i][j];
  }
  int all = 0;
  for (int i = 0; i < s.length(); i++) {
    cnt[id(s[i])]++;
    all |= (1 << id(s[i]));
  }
  for (int mask = all; mask; mask = (mask - 1) & all) {
    for (int j = 0; j < p; j++) {
      if ((1 << j) & mask) len[mask] += cnt[j];
    }
  }
  for (int i = 0; i < p; i++) {
    if (!(all & (1 << i))) continue;
    for (int j = i + 1; j < p; j++) {
      if (!(all & (1 << j))) continue;
      if (a[i][j]) continue;
      memset(vis, 0, sizeof(vis));
      int last = -1, tot = 0;
      for (int k = 0; k < s.length(); k++) {
        int cur = id(s[k]);
        if (cur == i || cur == j) {
          if (last != -1 && cur != last && !vis[all ^ tot])
            make_invalid(all ^ tot, i, j);
          last = cur;
          tot = 0;
        } else
          tot |= (1 << cur);
      }
    }
  }
  for (int i = 0; i < p; i++) {
    if (!(all & (1 << i))) continue;
    if (a[i][i]) continue;
    memset(vis, 0, sizeof(vis));
    bool first = true;
    int tot = 0;
    for (int j = 0; j < s.length(); j++) {
      int cur = id(s[j]);
      if (cur == i) {
        if (!first && !vis[all ^ tot]) make_invalid(all ^ tot, i, i);
        first = false;
        tot = 0;
      } else
        tot |= (1 << cur);
    }
  }
  int minm = s.length();
  memset(vis, 0, sizeof(vis));
  dfs(all, minm);
  cout << minm << '\n';
  return 0;
}
