#include <bits/stdc++.h>
using namespace std;
int bit(int m, int i) { return (m >> i) & 1; }
const int N = 5e5;
const int L = 20;
const int P = 18;
int n, p;
int bin_log[N];
int s[N];
int sparse[L][N];
int good[L][L];
char forb[1 << P][L][L];
bool fff[1 << P];
int dp[1 << P];
vector<int> g[L];
int cnt[L];
void build() {
  bin_log[0] = -1;
  for (int i = 1; i < N; ++i) {
    bin_log[i] = bin_log[i / 2] + 1;
  }
  for (int i = 1; i < L; ++i) {
    for (int j = 0; j + (1 << i) <= n; ++j) {
      sparse[i][j] = sparse[i - 1][j] | sparse[i - 1][j + (1 << (i - 1))];
    }
  }
}
int query(int l, int r) {
  if (l > r) {
    return 0;
  }
  ++r;
  int i = bin_log[r - l];
  return sparse[i][l] | sparse[i][r - (1 << i)];
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> n >> p;
  for (int i = 0; i < n; ++i) {
    char c;
    cin >> c;
    s[i] = c - 'a';
    ++cnt[s[i]];
    g[s[i]].push_back(i);
    sparse[0][i] = 1 << s[i];
  }
  build();
  for (int i = 0; i < p; ++i) {
    for (int j = 0; j < p; ++j) {
      cin >> good[i][j];
    }
  }
  for (int i = 0; i < n; ++i) {
    for (int pos, c = 0; c < p; ++c) {
      if (good[s[i]][c]) {
        continue;
      }
      auto &v = g[c];
      auto tt = upper_bound(begin(v), end(v), i);
      if (tt != v.end()) {
        pos = *tt;
        assert(i < pos);
        int q = query(i + 1, pos - 1);
        if (!bit(q, s[i]) && !bit(q, c)) {
          forb[q][s[i]][c] = true;
          forb[q][c][s[i]] = true;
        }
      }
      auto it = lower_bound(begin(v), end(v), i);
      if (it != v.begin()) {
        pos = *prev(it);
        assert(pos < i);
        int q = query(pos + 1, i - 1);
        if (!bit(q, s[i]) && !bit(q, c)) {
          forb[q][s[i]][c] = true;
          forb[q][c][s[i]] = true;
        }
      }
    }
  }
  for (int i = 0; i < (1 << p); ++i) {
    for (int b = 0; b < p; ++b) {
      if (!bit(i, b)) {
        continue;
      }
      int t = i;
      t &= ~(1 << b);
      for (int a = 0; a < p; ++a) {
        for (int bb = 0; bb < p; ++bb) {
          forb[i][a][bb] |= forb[t][a][bb];
        }
      }
    }
    for (int a = 0; a < p; ++a) {
      for (int b = 0; b < p; ++b) {
        if (bit(i, a) || bit(i, b)) {
          forb[i][a][b] = false;
        }
        if (forb[i][a][b]) {
          fff[i] = true;
        }
      }
    }
  }
  dp[0] = true;
  for (int i = 1; i < (1 << p); ++i) {
    if (fff[i]) {
      continue;
    }
    for (int b = 0; b < p; ++b) {
      if (!bit(i, b)) {
        continue;
      }
      int t = i;
      t &= ~(1 << b);
      dp[i] |= dp[t];
    }
  }
  int answer = 1e9;
  for (int m = 0; m < (1 << p); ++m) {
    if (dp[m]) {
      int cur = n;
      for (int i = 0; i < p; ++i) {
        if (bit(m, i)) {
          cur -= cnt[i];
        }
      }
      answer = min(answer, cur);
    }
  }
  cout << answer << '\n';
  return 0;
}
