#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define mp make_pair
#define si(x) int(x.size())
const int mod=1000000007,MAX=55,INF=1<<30;
int H,W,K,sh,sw,gh,gw;
int dis[MAX][MAX][2][1024];
char S[MAX][MAX],T[MAX][MAX][10];

vector<int> dh={0,1,0,-1},dw={1,0,-1,0};

struct dat{
    int h;
    int w;
    int f;
    int state;
};

void BFS(){
    dis[sh][sw][0][0]=0;
    queue<dat> Q;
    Q.push({sh,sw,0,0});
    
    while(!Q.empty()){
        auto u=Q.front();Q.pop();
        int h=u.h,w=u.w,f=u.f,state=u.state,d=dis[h][w][f][state];
        
        int s=0;
        for(int i=0;i<K;i++){
            if(state&(1<<i)){
                if(T[h][w][i]=='*') s^=1;
            }
        }
        
        if(S[h][w]=='|'){
            if(chmin(dis[h][w][f^1][state],d+1)){
                Q.push({h,w,f^1,state});
            }
        }
        
        if(s==f){
            if('a'<=S[h][w]&&S[h][w]<='z'){
                int x=S[h][w]-'a';
                if(chmin(dis[h][w][f^(T[h][w][x]=='*')][state^(1<<x)],d+1)){
                    Q.push({h,w,f^(T[h][w][x]=='*'),state^(1<<x)});
                }
            }
        }else{
            if('A'<=S[h][w]&&S[h][w]<='Z'){
                int x=S[h][w]-'A';
                if(chmin(dis[h][w][f^(T[h][w][x]=='*')][state^(1<<x)],d+1)){
                    Q.push({h,w,f^(T[h][w][x]=='*'),state^(1<<x)});
                }
            }
        }
        
        for(int k=0;k<4;k++){
            int toh=h+dh[k],tow=w+dw[k];
            if(toh<0||toh>=H||tow<0||tow>=W) continue;
            
            int s=0;
            for(int i=0;i<K;i++){
                if(state&(1<<i)){
                    if(T[toh][tow][i]=='*') s^=1;
                }
            }
            if(s==f){
                if(S[toh][tow]=='#'||S[toh][tow]=='^') continue;
                if('A'<=S[toh][tow]&&S[toh][tow]<='Z') continue;
            }else{
                if(S[toh][tow]=='#'||S[toh][tow]=='_'||S[toh][tow]=='%'||S[toh][tow]=='&') continue;
                if('a'<=S[toh][tow]&&S[toh][tow]<='z') continue;
            }
            
            if(chmin(dis[toh][tow][f][state],d+1)){
                Q.push({toh,tow,f,state});
            }
        }
    }
}

int main(){
    
    std::ifstream in("text.txt");
    std::cin.rdbuf(in.rdbuf());
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    cin>>W>>H;
    for(int i=0;i<H;i++){
        for(int j=0;j<W;j++){
            cin>>S[i][j];
            if(S[i][j]=='%'){
                sh=i;sw=j;
            }
            if(S[i][j]=='&'){
                gh=i;gw=j;
            }
            
            for(int k=0;k<2;k++) for(int l=0;l<1024;l++) dis[i][j][k][l]=INF;
        }
    }
    cin>>K;
    for(int t=0;t<K;t++){
        for(int i=0;i<H;i++){
            for(int j=0;j<W;j++){
                cin>>T[i][j][t];
            }
        }
    }
    
    BFS();
    
    int ans=INF;
    
    for(int k=0;k<2;k++){
        for(int l=0;l<(1<<K);l++){
            int s=0;
            for(int i=0;i<K;i++){
                if(l&(1<<i)){
                    if(T[gh][gw][i]=='*') s^=1;
                }
            }
            if(s==k) chmin(ans,dis[gh][gw][k][l]);
        }
    }
    
    
    if(ans==INF) ans=-1;
    
    cout<<ans<<endl;
    
}



Measured memory_bytes: 23628