#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9 + 7;
const long long longinf = 1LL << 60;
const long long mod = 1e9 + 7;
bool adj[1 << 17][26][26], ok[26][26];
bool dp[1 << 17];
int main() {
  int n, m;
  cin >> n >> m;
  string s;
  cin >> s;
  for (int i = (int)(0); i < (int)(m); ++i)
    for (int j = (int)(0); j < (int)(m); ++j) {
      int x;
      cin >> x;
      ok[i][j] = x;
    }
  for (int i = (int)(0); i < (int)(m); ++i) {
    char cur = 'a' + i;
    for (int aaa = (int)(0); aaa < (int)(2); ++aaa) {
      int mask = 0;
      bool used = false;
      for (int j = (int)(0); j < (int)(n); ++j) {
        int ch = s[j] - 'a';
        if (ch != i) {
          if (!used) continue;
          if ((mask >> ch) & 1) continue;
          adj[mask][i][ch] = 1;
          mask += (1 << ch);
        } else {
          if (used) adj[mask][i][i] = 1;
          used = true;
          mask = 0;
        }
      }
      reverse(s.begin(), s.end());
    }
  }
  int all = 1 << m;
  for (int x = (int)(0); x < (int)(m); ++x)
    for (int y = (int)(0); y < (int)(m); ++y) {
      for (int j = (int)(0); j < (int)(m); ++j)
        for (int k = (int)(0); k < (int)(all); ++k) {
          if ((1 << j) & k) {
            adj[k][x][y] |= adj[k ^ (1 << j)][x][y];
          }
        }
    }
  for (int i = (int)(0); i < (int)(all); ++i)
    for (int j = (int)(0); j < (int)(m); ++j)
      for (int k = (int)(0); k < (int)(m); ++k) {
        if ((1 << j) & i) adj[i][k][j] = 0;
      }
  dp[0] = 1;
  for (int i = (int)(0); i < (int)(all); ++i) {
    bool can = false;
    for (int j = (int)(0); j < (int)(m); ++j) {
      if (((1 << j) & i) == 0) continue;
      if (!dp[i ^ (1 << j)]) continue;
      can = true;
    }
    if (!can) continue;
    for (int x = (int)(0); x < (int)(m); ++x)
      for (int y = (int)(0); y < (int)(m); ++y) {
        if (((1 << x) & i) || ((1 << y) && i)) continue;
        if (ok[x][y]) continue;
        if (adj[i][x][y]) can = false;
      }
    dp[i] = can;
  }
  int ans = n;
  vector<int> cnt(m);
  for (int i = (int)(0); i < (int)(n); ++i) {
    cnt[s[i] - 'a']++;
  }
  for (int i = (int)(0); i < (int)(all); ++i) {
    if (!dp[i]) continue;
    int ret = 0;
    for (int j = (int)(0); j < (int)(m); ++j)
      if (((i >> j) & 1) == 0) ret += cnt[j];
    ans = min(ans, ret);
  }
  cout << ans << endl;
  return 0;
}
