import java.util.*;
import java.io.*;
class D{

	static int []p,res[];
	static int S,T,f,nodes;
	static int INF=(int)1e9;
	static PrintWriter out;
	static StringBuilder sb;
	static void augment(int u,int minEdge)
	{
		
		if(u==S) 
		{
			f= minEdge;
			return;
		}
 
		int v=p[u];
 
		if(v!=-1)
		{
			augment(v,Math.min(minEdge, res[v][u]));
 
			res[u][v]+=f;
			res[v][u] -= f;
 
		}
 
	}
	static int print(int u)
	{
		
		
		if(u==T)
			return 0;
		int ans=0;
		for(int v=0;v<nodes;v++)
			if(p[u]==v)
			{
				
				if(v!=T && u!=S) {
					sb.append(u+" "+v+"\n");
					ans++;
				}
				
				ans+=print(v);
				
				res[v][u]--;
				break;
			}
		return ans;
	}
	
	public static void main(String[] args) throws IOException 
	{
		
		Scanner sc=new Scanner(System.in);
		out = new PrintWriter (System.out);
		int n=sc.nextInt();
		int k=sc.nextInt();
		nodes=n+2;
		res=new int [nodes][nodes];
		char [][]s=new char [n][n];
		int []b=new int [k];
		S=0;
		T=nodes-1;
		for(int i=0;i<k;i++)
			res[S][sc.nextInt()]=1;
		for(int i=0;i<k;i++)
		{
			int x=sc.nextInt();
			res[x][T]=1;
			b[i]=x;
		}
		for(int i=1;i<=n;i++)
		{
			s[i-1]=sc.next().toCharArray();
			for(int j=1;j<=n;j++)
				if(s[i-1][j-1]=='1')
					res[i][j]=1;
		}
		
		p=new int [nodes];
		int mf=0;
		while(true)
		{
			int []dist=new int [nodes];
			f=0;
			Arrays.fill(dist, -1);
			Arrays.fill(p, -1);
			dist[S]=0;
			Queue<Integer> q= new LinkedList();
			q.add(S);
			while(!q.isEmpty())
			{
				int u=q.poll();
				if(u==T)
					break;
				for(int v=0;v<nodes;v++)
					if(dist[v]==-1 && res[u][v]>0)
					{
						dist[v]=dist[u]+1;
						p[v]=u;
						q.add(v);
					}
			}
			
			augment(T, INF);


			if(f==0)
				break;
			else 

				mf+=f;


		}
	
		if(mf<k)
			out.println("NO");
		else
		{
			sb=new StringBuilder();
			out.println("YES");
			int ans=0;
			while(k-->0)
			{
				
				
				Queue<Integer> q=new LinkedList();
				q.add(T);
				int []dist=new int [nodes];
				p=new int[nodes];
				Arrays.fill(dist, -1);
				dist[T]=0;
				while(!q.isEmpty())
				{
					int u=q.poll();
					for(int v=0;v<nodes;v++)
						if(res[u][v]>0 && dist[v]==-1)
						{
							q.add(v);
							p[v]=u;
							dist[v]=dist[u]+1;
						}
				}
				ans+=print(S);
			}
			out.println(ans);
			out.println(sb);
		}
		
		
		out.close();
	
	}
	
	
	
	static class Scanner {
        StringTokenizer st;
        BufferedReader br;
 
        public Scanner(InputStream s) {
            br = new BufferedReader(new InputStreamReader(s));
        }
 
        public Scanner(FileReader s) {
            br = new BufferedReader(s);
        }
 
        public String next() throws IOException {
            while (st == null || !st.hasMoreTokens())
                st = new StringTokenizer(br.readLine());
            return st.nextToken();
        }
 
        public int nextInt() throws IOException {
            return Integer.parseInt(next());
        }
 
        public long nextLong() throws IOException {
            return Long.parseLong(next());
        }
 
        public String nextLine() throws IOException {
            return br.readLine();
        }
        public boolean ready() throws IOException {return br.ready();}
        public double nextDouble() throws IOException {return Double.parseDouble(next());}
       
    }

}