#include<iostream>
#include
<queue>
using namespace std;

int mat[502][502];//存输入矩阵
typedef struct node 
{
    
int x,y;
    
int type;
    
int day;
    friend 
bool operator < (node a,node b)
    
{
        
if(a.day != b.day)
            
return a.day > b.day; //先按天数排序
        else
            
return a.type > b.type;
    }

}
Node;//优先队列的节点
int mark[502][502];

int dir[4][2= {1,0,-1,0,0,1,0,-1};
int m,n;  
int sum[250002];//存储几号病毒感染的电脑数
priority_queue<Node> Q;

void init()
{
    Node p;
    
int i,j;
    memset(sum,
0,sizeof(sum));//一定要全部清空,切记切记
    for(i = 1;i <=m ;i++)
        
for(j = 1;j <= n;j++)
        
{
            scanf(
"%d",&mat[i][j]);
            
if(mat[i][j] > 0)//将已经感染的电脑入队
            {
                p.x 
= i;
                p.y 
= j;
                p.type 
= mat[i][j];
                p.day 
= 1;
                Q.push(p);
                sum[mat[i][j]]
++;
            }
                
        }

}


void Bfs()
{
    
int k,dmax;
    Node p,q;
    
while(!Q.empty())
    
{
        q 
= Q.top();
        Q.pop();
        dmax 
= -100000000;
        
for(k = 0;k < 4;k++)
        
{
            p.x 
= q.x + dir[k][0];
            p.y 
= q.y + dir[k][1];
            
if(p.x >= 1 && p.x <= m && p.y >= 1 && p.y <= n)
            
{
                
if(mat[p.x][p.y] < 0)
                
{
                    
if(mat[p.x][p.y] + q.day >= 0)
                    
{
                        p.type 
= q.type;
                        q.day 
= q.day;
                        mat[p.x][p.y] 
= p.type;
                        Q.push(p);
                        sum[mat[p.x][p.y]] 
++;
                    }

                    
else if(mat[p.x][p.y] > dmax )//寻找其周围最快传染的机子~
                    {
                        dmax 
= mat[p.x][p.y];
                    }

                }

            }

        }
//for()
        if(dmax != -100000000 )//优化,找到周围最快被感染的电脑的天数入队
        {
            q.day 
= dmax * (-1);
            Q.push(q);
        }

    }
//while()
}


int main()
{
    
while(cin>>m>>n)
    
{
        
int t;
        
int h;
        init();
        Bfs();
        
int i;
        cin
>>t;
        
for(i = 0;i < t;i++)
        
{
            scanf(
"%d",&h);
            printf(
"%d\n",sum[h]);
        }

    }

    
return 0;
}