#include <bits/stdc++.h>
using namespace std;
template <typename T1, typename T2>
using pr = pair<T1, T2>;
template <typename T>
using vec = vector<T>;
int n, p;
vec<char> s;
vec<char> red_s;
bool g[22][22];
int triger_pair[(1 << 18)][18];
bool good_mask[(1 << 18)];
bool can_reach[(1 << 18)];
int q[32];
int main() {
  cin >> n >> p;
  s = vec<char>(n, 'a');
  for (int(i) = (0); (i) < (n); ++(i)) {
    scanf(" %c", &s[i]);
    ++q[s[i] - 'a'];
    if (i == 0 || s[i] != s[i - 1]) red_s.push_back(s[i]);
  }
  int red_n = ((int)red_s.size());
  for (int(i) = (0); (i) < (p); ++(i)) {
    for (int(j) = (0); (j) < (p); ++(j)) {
      cin >> g[i][j];
      if (i <= j && !g[i][j]) {
        int cur_mask = 0;
        int prev_ind = -1;
        for (int(k) = (0); (k) < (red_n); ++(k)) {
          if (red_s[k] == i + 'a') {
            if (prev_ind == j) {
              triger_pair[cur_mask][i] |= (1 << j);
            }
            cur_mask = 0;
            prev_ind = i;
          } else if (red_s[k] == j + 'a') {
            if (prev_ind == i) {
              triger_pair[cur_mask][i] |= (1 << j);
            }
            cur_mask = 0;
            prev_ind = j;
          } else
            cur_mask |= (1 << (red_s[k] - 'a'));
        }
      }
    }
  }
  for (int(mask) = (0); (mask) < ((1 << p)); ++(mask)) {
    for (int(i) = (0); (i) < (p); ++(i)) {
      if ((mask >> i) & 1) {
        int red_mask = mask ^ (1 << i);
        for (int(j) = (0); (j) < (p); ++(j)) {
          triger_pair[mask][j] |= triger_pair[red_mask][j];
        }
      }
    }
    bool z = true;
    for (int(fr) = (0); (fr) < (p); ++(fr)) {
      for (int(sc) = (fr); (sc) <= (p - 1); ++(sc)) {
        if ((triger_pair[mask][fr] >> sc) & 1) {
          int bit1 = (mask >> fr) & 1;
          int bit2 = (mask >> sc) & 1;
          if (!bit1 && !bit2) {
            z = false;
            break;
          }
        }
      }
      if (!z) break;
    }
    good_mask[mask] = z;
  }
  int mx = 0;
  can_reach[0] = true;
  for (int(mask) = (1); (mask) <= ((1 << p) - 1); ++(mask)) {
    if (!good_mask[mask]) continue;
    int sum = 0;
    for (int(i) = (0); (i) < (p); ++(i)) {
      if ((mask >> i) & 1) {
        int red_mask = mask ^ (1 << i);
        if (can_reach[red_mask]) can_reach[mask] = true;
        sum += q[i];
      }
    }
    if (can_reach[mask]) mx = max(mx, sum);
  }
  int res = n - mx;
  cout << res << endl;
  return 0;
}
