#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
long long mult(long long a, long long b, long long p = MOD) {
  return ((a % p) * (b % p)) % p;
}
long long add(long long a, long long b, long long p = MOD) {
  return (a % p + b % p) % p;
}
int N, P;
string txt;
set<int> Vpos[1234];
int camino[1234][1234];
int Vmi[300000];
int dp[300000];
int f(int mask) {
  if (dp[mask] != -1) return dp[mask];
  if (mask == (1 << P) - 1) return 0;
  int minn = 0;
  for (int i = 0; i < P; i++)
    if ((mask & (1 << i)) == 0) minn += Vpos[i + 'a'].size();
  for (int i = 0; i < P; i++) {
    if ((mask & (1 << i)) == 0 && Vmi[(mask | (1 << i))] == 0)
      minn = min(f(mask | (1 << i)), minn);
  }
  return dp[mask] = minn;
}
int main() {
  memset(Vmi, 0, sizeof Vmi);
  memset(dp, -1, sizeof dp);
  cin >> N >> P;
  cin >> txt;
  for (int i = 0; i < P; i++) {
    for (int j = 0; j < P; j++) {
      cin >> camino[i][j];
    }
  }
  for (int i = 0; i < txt.size(); i++) {
    Vpos[txt[i]].insert(i);
  }
  char michar, otrochar;
  for (int i = 0; i < txt.size(); i++) {
    priority_queue<pair<int, int>, vector<pair<int, int> >,
                   greater<pair<int, int> > >
        cola;
    set<int>::iterator ite;
    for (int j = 'a'; j < 'z' + 1; j++) {
      ite = Vpos[j].upper_bound(i);
      if (ite == Vpos[j].end()) continue;
      cola.push(pair<int, int>(*ite, j));
    }
    michar = txt[i];
    int maskact = 0;
    while (!cola.empty()) {
      pair<int, int> num = cola.top();
      cola.pop();
      otrochar = num.second;
      if (camino[michar - 'a'][otrochar - 'a'] == 0) {
        Vmi[maskact]++;
        Vmi[maskact | (1 << (michar - 'a'))]--;
        Vmi[maskact | (1 << (otrochar - 'a'))]--;
        Vmi[(maskact | (1 << (michar - 'a'))) | (1 << (otrochar - 'a'))]++;
      }
      maskact |= (1 << (otrochar - 'a'));
      if (otrochar == michar) break;
    }
  }
  for (int i = 0; i < P; i++) {
    for (int mask = (1 << P) - 1; mask >= 0; mask--)
      if (mask & (1 << i)) Vmi[mask] += Vmi[mask ^ (1 << i)];
  }
  cout << f(0);
}
