锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
#include <iostream>
#include <cstring>
using namespace std;
int map[102][102];
int ans[102][102];
int n,m;
int dir[4][2]=
{0,1,1,0,0,-1,-1,0};
int dfs(int x,int y)

{
if(ans[x][y]) return ans[x][y];
int i,j,nx,ny;
int temp,maxv=0;
for(i=0;i<4;++i)
{
for(j=1;j<=m;++j)
{
nx = x+dir[i][0]*j;
ny = y+dir[i][1]*j;
if(nx>=1 && nx<=n && ny>=1 && ny<=n && map[nx][ny] > map[x][y])
{
temp = dfs(nx,ny);
if (temp > maxv)
{
maxv = temp;
}
}
}
}
ans[x][y]=maxv+map[x][y];
return ans[x][y];
}
int main()

{
int i,j;
while(~scanf("%d%d",&n,&m))
{
if(n==-1 && m==-1)break;
memset(ans,0,sizeof(ans));
memset(map,10,sizeof(map));
for(i=1;i<=n;++i)
{
for(j=1;j<=n;++j)
{
scanf("%d",&map[i][j]);
}
}
dfs(1,1);
printf("%d\n",ans[1][1]);
}
return 0;
}
#include<iostream>
#include<queue>
#include <cstdio>
#include <cstring>
using namespace std;
struct Node

{
int vi;
int vj;
int day;
int type;
friend bool operator < (Node a,Node b)
{
if(a.day != b.day)
return a.day > b.day;
else
return a.type > b.type;
}
};
priority_queue<Node>Q;
int dir[4][2] =
{-1,0,1,0,0,-1,0,1};
int m,n;
int gra[501][501];
int sum[250002];
void init()

{
int i,j;
Node p;
memset(sum,0,sizeof(sum));
for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
{
scanf("%d",&gra[i][j]);
if(gra[i][j] > 0)
{
p.vi = i;
p.vj = j;
p.day = 1;
p.type = gra[i][j];
Q.push(p);
sum[gra[i][j]] ++;
}
}
}
}
void bfs()

{
int k,dmax;
Node p,q;
while(!Q.empty())
{
q = Q.top();
Q.pop();
dmax = -111111111;
for(k=0;k<4;k++)
{
p.vi = q.vi + dir[k][0];
p.vj = q.vj + dir[k][1];
if(p.vi>=1 && p.vi<=m && q.vi>=1 && q.vj<=n )
{
if(gra[p.vi][p.vj] < 0 ) 
{
if( gra[p.vi][p.vj] + q.day >= 0)
{
p.type = q.type;
p.day = q.day;
gra[p.vi][p.vj] = p.type;
Q.push(p);
sum[p.type] ++;
}
else if(gra[p.vi][p.vj] > dmax)//瀵繪壘鍏跺懆鍥存渶蹇紶鏌撶殑鏈哄瓙~
{
dmax = gra[p.vi][p.vj] ;
}
}
}//for(k=0;k<4;k++)
}
if(dmax != -111111111)
{
q.day = dmax * (-1);
Q.push(q);
}
}
}
int main()

{
int a,T;
while(cin>>m>>n)
{
init();
bfs();
cin>>T;
while(T--)
{
scanf("%d",&a);
printf("%d\n",sum[a]);
}
}
return 0;
}
