#include <bits/stdc++.h>
using namespace std;
const int N = 17;
const int inf = 1 << 30;
long long read() {
  long long a = 0;
  char b = 1, c;
  do
    if ((c = getchar()) == 45) b = -1;
  while (c < 48 || c > 57);
  do a = (a << 3) + (a << 1) + (c & 15);
  while ((c = getchar()) > 47 && c < 58);
  return a * b;
}
void write(long long x, char c) {
  if (x < 0) putchar(45), x = -x;
  char a[20], s = 0;
  do a[++s] = x % 10 | 48;
  while (x /= 10);
  do putchar(a[s]);
  while (--s);
  putchar(c);
}
int n, m, ans = inf;
bitset<N> b[N];
void solve(string a, bitset<N> c) {
  ans = min(ans, (int)a.size());
  for (int i = 0; i < m; ++i) {
    if (!c[i]) continue;
    string t;
    bool op = 0;
    for (char x : a) {
      if (x == i) continue;
      if (!t.empty() && !b[t.back()][x]) {
        op = 1;
        break;
      }
      t += x;
    }
    if (op) continue;
    c[i] = 0;
    solve(t, c);
    return;
  }
}
int main() {
  n = read(), m = read();
  string a;
  cin >> a;
  for (char &x : a) x -= 97;
  for (int i = 0; i < m; ++i) {
    for (int j = 0; j < m; ++j) {
      b[i][j] = read();
    }
  }
  solve(a, (1 << m) - 1);
  write(ans, '\n');
}
