#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, p;
  string s;
  cin >> n >> p >> s;
  int U = (1 << p) - 1;
  vector<vector<int> > adj(p, vector<int>(p));
  for (int i = 0; i < p; ++i) {
    for (int j = 0; j < p; ++j) {
      cin >> adj[i][j];
    }
  }
  vector<vector<int> > pos(p);
  for (int i = 0; i < s.size(); ++i) {
    pos[s[i] - 'a'].push_back(i);
  }
  vector<bool> able(U + 1, 1);
  for (int i = 0; i < p; ++i) {
    for (int j = i; j < p; ++j) {
      if (adj[i][j]) continue;
      vector<bool> mark(U + 1), iq(U + 1);
      if (i != j) {
        vector<int> &a = pos[i], &b = pos[j];
        for (int l = 0, r = 0; l < a.size() && r < b.size();) {
          if (l + 1 < a.size() && a[l + 1] < b[r]) {
            ++l;
            continue;
          }
          if (r + 1 < b.size() && b[r + 1] < a[l]) {
            ++r;
            continue;
          }
          int msk = 0, dfn = min(a[l], b[r]), low = max(a[l], b[r]);
          for (int k = dfn + 1; k < low; ++k) {
            msk |= 1 << (s[k] - 'a');
          }
          mark[U ^ msk] = 1;
          if (a[l] < b[r])
            ++l;
          else
            ++r;
        }
      } else {
        vector<int> &a = pos[i];
        for (int l = 1; l < a.size(); ++l) {
          int msk = 0;
          for (int k = a[l - 1] + 1; k < a[l]; ++k) {
            msk |= 1 << (s[k] - 'a');
          }
          mark[U ^ msk] = 1;
        }
      }
      for (int xs = U; xs; --xs) {
        bool check = 1;
        for (int k = 0; k < p; ++k) {
          if (~xs >> k & 1) {
            check &= iq[xs | (1 << k)];
          }
        }
        check &= !mark[xs];
        if (check) {
          iq[xs] = 1;
        } else {
          able[xs] = 0;
        }
      }
    }
  }
  int ans = n;
  for (int xs = U - 1; ~xs; --xs) {
    bool ap = 0;
    for (int k = 0; k < p; ++k) {
      if (~xs >> k & 1) ap |= able[xs | (1 << k)];
    }
    if (!ap) {
      able[xs] = 0;
    }
    if (able[xs]) {
      int go = 0;
      for (int i = 0; i < p; ++i) {
        if (xs >> i & 1) go += pos[i].size();
      }
      ans = min(ans, go);
    }
  }
  cout << ans << endl;
  return 0;
}
