#include <bits/stdc++.h>
using namespace std;
bool __hack = std::ios::sync_with_stdio(false);
auto __hack1 = cin.tie(nullptr);
template <class T>
inline T set_bit(T mask, int bit) {
  assert(0 <= bit && bit < numeric_limits<T>::digits);
  return mask | ((T)(1) << bit);
}
template <class T>
inline bool get_bit(T mask, int bit) {
  assert(0 <= bit && bit < numeric_limits<T>::digits);
  return mask & ((T)(1) << bit);
}
namespace template_util {
constexpr int bytecount(uint64_t x) { return x ? 1 + bytecount(x >> 8) : 0; }
template <int N>
struct bytetype {};
template <uint64_t N>
struct minimal_uint : bytetype<bytecount(N)> {};
}  // namespace template_util
template <class T>
T next(istream& in) {
  T ret;
  in >> ret;
  return ret;
}
void solve(istream& in, ostream& out) {
  auto n = next<int>(in);
  auto p = next<int>(in);
  auto s = next<string>(in);
  vector<vector<int>> pos_in_s(p);
  vector<vector<int>> nxt(n, vector<int>(p, -1));
  vector<vector<int>> nxt1(n, vector<int>(p, -1));
  vector<int> cnt(p, 0);
  for (int i = 0; i < (n); ++i) {
    int let = (int)(s[i] - 'a');
    pos_in_s[let].push_back(i);
    ++cnt[let];
  }
  vector<int> pos(p, -1);
  for (int i = n - 1; i >= 0; --i) {
    nxt[i] = pos;
    pos[(int)(s[i] - 'a')] = i;
  }
  pos = vector<int>(p, -1);
  for (int i = 0; i < (n); ++i) {
    nxt1[i] = pos;
    pos[(int)(s[i] - 'a')] = i;
  }
  vector<vector<int>> a(p, vector<int>(p, 0));
  for (int i = 0; i < (p); ++i) {
    for (int j = 0; j < (p); ++j) {
      auto x = next<int>(in);
      a[i][j] = x;
    }
  }
  vector<int> f(1 << n, -1);
  f[0] = n;
  int res = n;
  for (int mask = 0; mask < ((1 << n)); ++mask) {
    if (f[mask] != -1) {
      for (int c = 0; c < (p); ++c) {
        if (!get_bit(mask, c)) {
          bool can_remove = true;
          for (int ps : pos_in_s[c]) {
            int le = -1;
            int ri = n + 1;
            for (int j = 0; j < (p); ++j) {
              if (!get_bit(mask, j) && j != c) {
                if (nxt[ps][j] != -1) {
                  ri = min(ri, nxt[ps][j]);
                }
                if (nxt1[ps][j] != -1) {
                  le = max(le, nxt1[ps][j]);
                }
              }
            }
            if (le >= 0 && ri < n) {
              int t1 = s[le] - 'a';
              int t2 = s[ri] - 'a';
              if (!a[t1][t2]) {
                can_remove = false;
                break;
              }
            }
          }
          if (can_remove) {
            f[set_bit(mask, c)] = f[mask] - cnt[c];
            res = min(res, f[mask] - cnt[c]);
          }
        }
      }
    }
  }
  out << res << "\n";
}
int main() {
  solve(cin, cout);
  return 0;
}
