#include<cstdio>
#include<cstring>
int i,j,k,n,m,a[128][128],z,x,y,t,s,l,b,A,v[128][128],
f[256][128],g[256][128],X[]={0,1,0,-1},Y[]={1,0,-1,0};
char u[128][128];
char p[]="0123456789abcdefghijklmnopqrstuvwxyz";
void dfs(int x,int y,int d){
for(int i=0;i<4;++i){
t=x+X[i];s=y+Y[i];
if(t>=0&&s>=0&&t<n&&s<m)
if(d+a[t][s]<f[t*m+s][z]){
f[t*m+s][z]=d+a[t][s];
g[t*m+s][z]=0;
dfs(t,s,d+a[t][s]);
}
}
}
void ff(int x,int y,int z){
u[x][y]='X';
if(g[x*m+y][z]==0){
int r=f[x*m+y][z]-a[x][y];
if(r){
for(int i=0;i<4;++i){
t=x+X[i];s=y+Y[i];
if(t>=0&&s>=0&&t<n&&s<m)
if(f[t*m+s][z]==r){
ff(t,s,z);
break;
}
}
}
}
else
{
int w=g[x*m+y][z];
for(int i=0;i<4;++i){
t=x+X[i];s=y+Y[i];
if(t>=0&&s>=0&&t<n&&s<m)
if(f[t*m+s][w]+f[x*m+y][z^w]==f[x*m+y][z]){
ff(t,s,w);
ff(x,y,z^w);
break;
}
}
}
}
int main(){
scanf("%d%d%d",&n,&m,&k);
for(;i<n;++i){
memset(u[i],'.',m);
for(j=0;j<m;++j)scanf("%d",a[i]+j);
}
memset(f,32,sizeof(f));
memset(v,255,sizeof(v));
for(i=0;i<k;++i){
scanf("%d%d",&x,&y);--x;--y;z=1<<i;
v[x][y]=i;
dfs(x,y,f[x*m+y][z]=a[x][y]);
}
for(z=3;z<(1<<k);++z)if(__builtin_popcount(z)>1)
for(x=z-1;x;x=(x-1)&z){
y=x^z;
for(i=A=0;i<n;++i)for(j=0;j<m;++A,++j)
for(l=0;l<4;++l){
t=i+X[l];s=j+Y[l];
if(t>=0&&s>=0&&t<n&&s<m){
b=t*m+s;
if(f[A][x]+f[b][y]<f[A][z]){
f[A][z]=f[A][x]+f[b][y];
g[A][z]=y;
}
}
}
for(y=16;y--;)
for(i=A=0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