#include <bits/stdc++.h>
#define il inline
#define ri register int
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define mid ((l+r)>>1)
#define MAXN 100050
#define MAXM
#define mod
#define inf (1<<30)
#define eps (1e-6)
#define alpha 0.75
#define rep(i, x, y) for(ri i = x; i <= y; ++i)
#define repd(i, x, y) for(ri i = x; i >= y; --i)
#define file(s) freopen(s".in", "r", stdin), freopen(s".out", "w", stdout)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair <int, int> pii;
typedef pair <ll, int> pli;
typedef pair <int, ll> pil;
typedef pair <ll, ll> pll;
template <typename T> il bool chkmin(T &x, T y) {return x > y ? x = y, 1 : 0;}
template <typename T> il bool chkmax(T &x, T y) {return x < y ? x = y, 1 : 0;}
template <typename T> il void read(T &x) {
	char ch = getchar(); int f = 1; x = 0;
	while(ch < '0' || ch > '9') {if(ch == '-') f = -1; ch = getchar();}
	while(ch >= '0' && ch <= '9') x = x*10+ch-'0', ch = getchar();
	x *= f;
}
template <typename T, typename... Args> il void read(T &x, Args &...args) {
	char ch = getchar(); int f = 1; x = 0;
	while(ch < '0' || ch > '9') {if(ch == '-') f = -1; ch = getchar();}
	while(ch >= '0' && ch <= '9') x = x*10+ch-'0', ch = getchar();
	x *= f;
	read(args...);
}
const int Siz = 1 << 20;
int a[20][20], n, p, bad[Siz], num[20], ans[Siz], Ans = 1e9, S, tot;
char s[MAXN];
vector <int> occ[20];
void del(int x, int a, int b) {
	if(bad[x] == tot) return ;
	bad[x] = tot;
	rep(i, 0, p-1) if(i != a && i != b) del(x | (1 << i), a, b); 
}
void fuck(int x, int y) {
	int j = 0;
	rep(i, x+1, y-1) j |= 1 << (s[i]-'a');
	del(j, s[x]-'a', s[y]-'a');
}
void kick(int x, int y) {
	++tot;
	for(auto v: occ[x]) {
		int t = v+1;
		while(t <= n) {
			if(s[t]-'a' == y) fuck(v, t);
			if(s[t]-'a' == x) break;
			t++;
		}
	}
}
void bfs() {
	static int q[Siz << 2], vis[Siz << 2], front = 0, rear = 0;
	vis[q[rear++] = 0] = 1;
	while(front < rear) {
		int u = q[front++]; chkmin(Ans, ans[u]);
//		cout<<u<<' '<<ans[u]<<endl;
		rep(i, 0, p-1) 
			if(!bad[u | (1 << i)] && !vis[u | (1 << i)])
				vis[q[rear++] = (u | (1 << i))] = 1;
	}
}
int main() {
	read(n, p);
	S = 1 << p;
	scanf("%s", s+1);
	rep(i, 1, n) occ[s[i]-'a'].pb(i), ++num[s[i]-'a'];
	rep(i, 0, S-1) {
		ans[i] = n;
		rep(j, 0, p-1)
			if(i & (1 << j)) ans[i] -= num[j];
	}
	rep(i, 0, p-1)
		rep(j, 0, p-1) {
			read(a[i][j]);
			if(!a[i][j]) kick(i, j);
		}
	bfs();
	printf("%d", Ans);
	return 0;
}
