#include <bits/stdc++.h>
#pragma comment(linker, "/stack:247474112")
#pragma GCC optimize("Ofast")
using namespace std;
const int INF = 0x3f3f3f3f;
int n, p;
string s;
int G[18][18];
const int MAXN = 1 << 18;
int vis[MAXN];
int vab[MAXN];
int ans = INF;
map<char, int> cnt;
vector<int> pos[17];
void input() {
  cin >> n >> p;
  cin >> s;
  for (long long i = 0; i < p; ++i) {
    for (long long j = 0; j < p; ++j) {
      cin >> G[i][j];
    }
  }
  for (long long i = 0; i < n; ++i) {
    ++cnt[s[i]];
    pos[s[i] - 'a'].push_back(i);
  }
}
void mark(int m, int a, int b) {
  if (vis[m] == 1 && vab[(1 << a) | (1 << b)] == 1) return;
  vis[m] = 1;
  vab[(1 << a) | (1 << b)] = 1;
  for (long long i = 0; i < p; ++i) {
    if (i == a || i == b) continue;
    if ((m >> i) & 1) {
      mark(m ^ (1 << i), a, b);
    }
  }
}
void findm(int a, int b) {
  int flag = 0;
  int subm = 0;
  if (a != b) {
    for (long long i = 0; i < n; ++i) {
      if (s[i] == 'a' + a) {
        flag = 1;
        subm = 0;
      } else if (s[i] == 'a' + b) {
        if (flag == 1) {
          flag = 0;
          int m = (1 << p) - 1;
          m ^= subm;
          mark(m, a, b);
          vab[(1 << a) | (1 << b)] = 0;
        }
      } else {
        if (flag == 1) {
          subm |= (1 << (s[i] - 'a'));
        }
      }
    }
  } else {
    for (long long i = 0; i < n; ++i) {
      if (s[i] == 'a' + a) {
        if (flag == 0) {
          flag = 1;
        } else {
          int m = (1 << p) - 1;
          m ^= subm;
          mark(m, a, a);
          vab[1 << a] = 0;
          subm = 0;
        }
      } else {
        if (flag == 1) {
          subm |= (1 << (s[i] - 'a'));
        }
      }
    }
  }
}
bool DFS(int m) {
  if (vis[m] == 0) {
    vis[m] = 2;
    bool flag = false;
    for (long long i = 0; i < p; ++i) {
      if ((m >> i) & 1) {
        flag |= DFS(m ^ (1 << i));
      }
    }
    if (!flag) {
      int len = 0;
      for (long long i = 0; i < p; ++i) {
        if ((m >> i) & 1) {
          len += cnt['a' + i];
        }
      }
      ans = min(ans, len);
    }
    return true;
  }
  return false;
}
void solve() {
  for (long long i = 0; i < p; ++i) {
    for (long long j = 0; j < p; ++j) {
      if (G[i][j] == 0) {
        findm(i, j);
      }
    }
  }
  DFS((1 << p) - 1);
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  input();
  solve();
  cout << ans << endl;
}
