#include <bits/stdc++.h>
using namespace std;
int bit(int m, int i) { return (m >> i) & 1; }
const int N = 1e6;
const int P = 20;
int n, p;
bool good[P][P];
int pref[N][P];
char s[N];
int a[N];
bool bad[N];
bool dp[N];
vector<int> g[P];
int sum(int c, int x, int y) {
  int l = min(x, y) + 1;
  int r = max(x, y) - 1;
  if (l > r) {
    return 0;
  }
  return pref[r + 1][c] - pref[l][c];
}
void mark(int m, int x, int y) {
  if (bit(m, x) || bit(m, y)) {
    return;
  }
  bad[m] = true;
  for (int t = 0; t < p; ++t) {
    if (!bit(m, t)) {
      mark(m | (1 << t), x, y);
    }
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> n >> p;
  cin >> s;
  for (int i = 0; i < n; ++i) {
    a[i] = s[i] - 'a';
    g[a[i]].push_back(i);
    copy_n(pref[i], p, pref[i + 1]);
    ++pref[i + 1][a[i]];
  }
  for (int i = 0; i < p; ++i) {
    for (int j = 0; j < p; ++j) {
      cin >> good[i][j];
    }
  }
  for (int x = 0; x < p; ++x) {
    if (!good[x][x]) {
      for (int i = 0; i + 1 < (int)g[x].size(); ++i) {
        int mask = 0;
        for (int t = 0; t < p; ++t) {
          if (sum(t, g[x][i], g[x][i + 1])) {
            mask |= 1 << t;
          }
        }
        mark(mask, x, x);
      }
    }
    for (int y = x + 1; y < p; ++y) {
      if (good[x][y]) {
        continue;
      }
      auto i = g[x].begin(), j = g[y].begin();
      while (i < g[x].end() && j < g[y].end()) {
        if (sum(x, *i, *j) || sum(y, *i, *j)) {
          assert(*i != *j);
          if (*i < *j) {
            ++i;
          } else {
            ++j;
          }
        } else {
          int mask = 0;
          for (int t = 0; t < p; ++t) {
            if (sum(t, *i, *j)) {
              mask |= 1 << t;
            }
          }
          mark(mask, x, y);
          ++i;
          ++j;
        }
      }
    }
  }
  dp[0] = true;
  for (int i = 1; i < (1 << p); ++i) {
    if (bad[i]) {
      continue;
    }
    for (int t = 0; t < p; ++t) {
      if (bit(i, t)) {
        int tmp = i ^ (1 << t);
        dp[i] |= dp[tmp];
      }
    }
  }
  int answer = n;
  for (int i = 0; i < (1 << p); ++i) {
    if (!dp[i]) {
      continue;
    }
    int cur = n;
    for (int t = 0; t < p; ++t) {
      if (bit(i, t)) {
        cur -= (int)g[t].size();
      }
    }
    answer = min(answer, cur);
  }
  cout << answer << '\n';
  return 0;
}
