#include <bits/stdc++.h>
std::string s;
int real_size;
int get_next_isnt(int p, char isnt) {
  while (true) {
    p++;
    if (p >= real_size) return 17;
    if (s[p] != isnt) return s[p] - 'a';
  }
}
int get_prev_isnt(int p, char isnt) {
  while (true) {
    p--;
    if (p < 0) return 17;
    if (s[p] != isnt) return s[p] - 'a';
  }
}
int main() {
  int n, p;
  bool m[19][19];
  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;
    }
  }
  for (int i = 0; i < 19; i++) {
    m[i][17] = true;
    m[17][i] = true;
    m[i][19] = false;
    m[19][i] = true;
  }
  bool can_del;
  real_size = s.size();
  while (true) {
    int del_ch = -1;
    for (int ch = 0; ch < p; ch++) {
      can_del = true;
      bool any = (s[0] == ch + 'a') || (s[real_size - 1] == ch + 'a');
      for (int i = 1; i < real_size - 1; i++) {
        if (s[i] == ch + 'a') {
          any = true;
          if (!m[get_prev_isnt(i, ch + 'a')][get_next_isnt(i, ch + '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 < real_size; i++) {
      s[i - z] = s[i];
      z += s[i] == del_ch + 'a';
    }
    real_size -= z;
    if (real_size == 0) break;
  }
  std::cout << real_size;
  return 0;
}
