#include <bits/stdc++.h>
const int N = 100005, M = 17;
bool occur[M][M][1 << M];
int n, p;
std::string s;
std::vector<int> v[M];
bool a[M][M], ok[1 << M];
int dp[1 << M], cnt[M];
int main() {
  std::ios::sync_with_stdio(0), std::cin.tie(0);
  std::cin >> n >> p >> s;
  for (int i = 0; i < n; ++i) v[s[i] - 'a'].emplace_back(i), ++cnt[s[i] - 'a'];
  for (int i = 0; i < p; ++i)
    for (int j = 0; j < p; ++j) std::cin >> a[i][j];
  for (int i = 0; i < n - 1; ++i) {
    static int first[M], rk[M];
    for (int j = 0; j < p; ++j) {
      int k = std::upper_bound(v[j].begin(), v[j].end(), i) - v[j].begin();
      first[j] = k == v[j].size() ? -1 : v[j][k], rk[j] = j;
    }
    std::sort(rk, rk + p, [](int x, int y) { return first[x] < first[y]; });
    int q, pre = 0;
    for (q = 0; q < p; ++q)
      if (first[rk[q]] != -1) break;
    for (; q < p; ++q) {
      occur[s[i] - 'a'][rk[q]][pre] = 1;
      occur[rk[q]][s[i] - 'a'][pre] = 1;
      pre |= 1 << rk[q];
      if (rk[q] == s[i] - 'a') break;
    }
  }
  for (int i = 0; i < p; ++i)
    for (int j = i; j < p; ++j) {
      for (int k = 1; k < 1 << p; k <<= 1)
        for (int l = 0; l < 1 << p; ++l)
          if (l & k) occur[i][j][l] |= occur[i][j][l ^ k];
      if (i != j) std::memcpy(occur[j][i], occur[i][j], 1 << p);
    }
  for (int i = 0; i < 1 << p; ++i) {
    ok[i] = 1;
    for (int j = 0; j < p; ++j)
      if (!(i >> j & 1))
        for (int k = 0; k < p; ++k)
          if (!(i >> k & 1)) ok[i] &= !occur[j][k][i] || a[j][k];
  }
  int mask = ~(-1 << p);
  dp[0] = 0;
  for (int i = 1; i < 1 << p; ++i)
    if (ok[mask ^ i]) {
      for (int j = 0; j < p; ++j)
        if (i >> j & 1) dp[i] += cnt[j];
      for (int j = 0; j < p; ++j)
        if (i >> j & 1) dp[i] = std::min(dp[i], dp[i ^ 1 << j]);
    } else
      dp[i] = 0x3f3f3f3f;
  std::cout << dp[mask] << '\n';
  return 0;
}
