#include <bits/stdc++.h>
int main() {
  int n, p;
  bool m[17][17];
  std::string s;
  std::cin >> n >> p >> s;
  for (int i = 0; i < p; i++) {
    for (int j = 0; j < p; j++) {
      int a;
      std::cin >> a;
      m[i][j] = (bool)a;
    }
  }
  bool can_del;
  while (true) {
    int del_ch = -1;
    for (int ch = 0; ch < p; ch++) {
      can_del = true;
      bool any = (s[0] == ch + 'a') || (s[s.size() - 1] == ch + 'a');
      for (int i = 1; i < s.length() - 1; i++) {
        if (s[i] == ch + 'a') {
          any = true;
          if (!m[s[i - 1] - 'a'][s[i + 1] - 'a']) {
            can_del = false;
            break;
          }
        }
      }
      if (can_del && any) {
        del_ch = ch;
        break;
      }
    }
    if (del_ch == -1) break;
    int z = 0;
    for (int i = 0; i < s.size(); i++) {
      s[i - z] = s[i];
      z += s[i] == del_ch + 'a';
    }
    s.resize(s.size() - z);
    if (s.empty()) break;
  }
  std::cout << s.size();
  return 0;
}
