import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.TreeSet;

@SuppressWarnings("unchecked")
public class F {
	
	int n, p;
	char[] s;
	boolean[][] adj;
	ArrayList<Integer>[][] badMasks;
	int[] cnt;
	int[] memo;
	int[] memoLen;
	
	int len(int mask) {
		if(memoLen[mask] != -1) return memoLen[mask];
		int ans = n;
		for(int i = 0; i < p; ++i) if((mask & (1 << i)) == 0) ans -= cnt[i];
		return memoLen[mask] = ans;
	}
	
	int go(int mask) {
		if(memo[mask] != -1) return memo[mask];
		int inv = ~mask;
		int ans = n;
		boolean good = true;
		for(int i = 0; good && i < p; ++i) {
			if((mask & (1 << i)) == 0) continue;
			for(int j = i; good && j < p; ++j) {
				if((mask & (1 << j)) == 0) continue;
				for(int bad : badMasks[i][j]) {
					if((bad & inv) == bad) {
						good = false;
						break;
					}
				}
			}
		}
		if(good) {
			ans = Math.min(ans, len(mask));
			for(int i = 0; i < p; ++i) {
				if((mask & (1 << i)) == 0) continue;
				ans = Math.min(ans, go(mask - (1 << i)));
			}
		}
		return memo[mask] = ans;
	}
	
//	int go(int mask) {
//		if(memo[mask] != -1) return memo[mask];
//		int inv = ~mask;
//		int ans = n;
//		if(isGood[mask]) {
//			ans = Math.min(ans, len(mask));
//			for(int i = 0; i < p; ++i) {
//				if((mask & (1 << i)) == 0) continue;
//				ans = Math.min(ans, go(mask - (1 << i)));
//			}
//		}
//		return memo[mask] = ans;
//	}
	
	public void solve(JS in, PrintWriter out) {
		n = in.nextInt();
		p = in.nextInt();
		s = in.next().toCharArray();
		adj = new boolean[p][p];
		cnt = new int[p];
		memoLen = new int[1 << p]; Arrays.fill(memoLen, -1);
		memo = new int[1 << p]; Arrays.fill(memo, -1);
		for(int i = 0; i < p; ++i) {
			for(int j = 0; j < p; ++j) {
				adj[i][j] = in.nextInt() == 1;
			}
		}
		for(int i = 0; i < n; ++i) cnt[s[i] - 'a']++;
		//precomp
		int[] masks = new int[n];
		for(int i = 0; i < n; ++i) masks[i] = 1 << (s[i] - 'a');
		RMQ rmq = new RMQ(masks);
		TreeSet<Integer>[] indexes = new TreeSet[p];
		badMasks = new ArrayList[p][p];
		for(int i = 0; i < p; ++i) indexes[i] = new TreeSet<>();
		for(int i = 0; i < n; ++i) indexes[s[i] - 'a'].add(i);
		for(int i = 0; i < p; ++i) for(int j = 0; j < p; ++j) badMasks[i][j] = new ArrayList<>();
		for(int i = 0; i < n; ++i) {
			int c1 = s[i] - 'a';
			for(int c2 = 0; c2 < p; ++c2) { //next thing after me
				if(adj[c1][c2]) continue;
				Integer j = indexes[c2].higher(i);
				if(j == null) continue;
				int middleMask = rmq.query(i + 1, j - 1);
				badMasks[c1][c2].add(middleMask);
				badMasks[c2][c1].add(middleMask);
			}
		}
		for(int i = 0; i < p; ++i) {
			for(int j = 0; j < p; ++j) {
				Collections.sort(badMasks[i][j], (a, b) -> Integer.bitCount(a) - Integer.bitCount(b));
			}
		}
		out.println(go((1 << p) - 1));
	}
	
	public static void main(String[] args) {
		JS in = new JS();
		PrintWriter out = new PrintWriter(System.out);
		new F().solve(in, out);
		out.close();
	}
	
	static class RMQ {
		int[] vs;
		int[][] lift;

		public RMQ(int[] vs) {
			this.vs = vs;
			int n = vs.length;
			int maxlog = Integer.numberOfTrailingZeros(Integer.highestOneBit(n)) + 2;
			lift = new int[maxlog][n];
			for (int i = 0; i < n; i++)
				lift[0][i] = vs[i];
			int lastRange = 1;
			for (int lg = 1; lg < maxlog; lg++) {
				for (int i = 0; i < n; i++) {
					lift[lg][i] = lift[lg - 1][i] | lift[lg - 1][Math.min(i + lastRange, n - 1)];
				}
				lastRange *= 2;
			}
		}

		public int query(int low, int hi) {
			if(low > hi) return 0;
			int range = hi - low + 1;
			int exp = Integer.highestOneBit(range);
			int lg = Integer.numberOfTrailingZeros(exp);
			return lift[lg][low] | lift[lg][hi - exp + 1];
		}
	}
	
	static class JS {
		public int BS = 1<<16;
		public char NC = (char)0;
		byte[] buf = new byte[BS];
		int bId = 0, size = 0;
		char c = NC;
		double num = 1;
		BufferedInputStream in;

		public JS() {
			in = new BufferedInputStream(System.in, BS);
		}

		public JS(String s) throws FileNotFoundException {
			in = new BufferedInputStream(new FileInputStream(new File(s)), BS);
		}

		public char nextChar(){
			while(bId==size) {
				try {
					size = in.read(buf);
				}catch(Exception e) {
					return NC;
				}
				if(size==-1)return NC;
				bId=0;
			}
			return (char)buf[bId++];
		}

		public int nextInt() {
			return (int)nextLong();
		}

		public long nextLong() {
			num=1;
			boolean neg = false;
			if(c==NC)c=nextChar();
			for(;(c<'0' || c>'9'); c = nextChar()) {
				if(c=='-')neg=true;
			}
			long res = 0;
			for(; c>='0' && c <='9'; c=nextChar()) {
				res = (res<<3)+(res<<1)+c-'0';
				num*=10;
			}
			return neg?-res:res;
		}

		public double nextDouble() {
			double cur = nextLong();
			return c!='.' ? cur:cur+nextLong()/num;
		}

		public String next() {
			StringBuilder res = new StringBuilder();
			while(c<=32)c=nextChar();
			while(c>32) {
				res.append(c);
				c=nextChar();
			}
			return res.toString();
		}

		public String nextLine() {
			StringBuilder res = new StringBuilder();
			while(c<=32)c=nextChar();
			while(c!='\n') {
				res.append(c);
				c=nextChar();
			}
			return res.toString();
		}

		public boolean hasNext() {
			if(c>32)return true;
			while(true) {
				c=nextChar();
				if(c==NC)return false;
				else if(c>32)return true;
			}
		}
	}
}
