#define _CRT_SECURE_NO_DEPRECATE
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
#include <iostream>
#pragma comment (linker,"/STACK:64000000")
using namespace std;
const int dx[5]={0,1,-1,0,0};
const int dy[5]={0,0,0,1,-1};
int a[55][55]={{0}};
bool b[55][55]={{false}};
int och[2505][3]={{0}};
int n,m,c,k,first,last;
int x,y,xt=0,yt=0,xnn=0,ynn=0;
void delColumn()
{
	int i,j;
	int d=0;
	for (i=1;i<=n;i++)
	{
		for (j=0;j<=m;j++)
		{
			a[i-d][j]=a[i][j];
			b[i-d][j]=b[i][j];
		}
		if (a[i][0]==0) d++;
	}
	n-=d;
}
void delCells()
{
	int i,j;
	int d=0;
	for (i=1;i<=n;i++)
	{
		d=0;
		for (j=1;j<=a[i][0];j++)
		{
			a[i][j-d]=a[i][j];
			b[i][j-d]=b[i][j];
			if (b[i][j])
			{
				d++;
			}
		}
		a[i][0]-=d;
	}
}
int main()
{
//	freopen("bricks.in","r",stdin);
//	freopen("bricks.out","w",stdout);
	cin >> n >> m >> c;
	int i,j;
	for (i=1;i<=n;i++)
	{
		a[i][0]=m;
		for (j=1;j<=m;j++) 
			cin >> a[i][j];
	}
	
	cin >> k;
	for (i=1;i<=k;i++)
	{
		cin >> x >> y;
		first=1;
		last=2;
		och[1][1]=x;
		och[1][2]=y;
		while (last>first)
		{
			xt=och[first][1];
			yt=och[first][2];
			first++;
			b[xt][yt]=true;
			for (j=1;j<=4;j++)
			{
				xnn=xt+dx[j];
				ynn=yt+dy[j];
				if ((xnn>0)&&(xnn<=n)&&(ynn>0)&&(ynn<=a[xnn][0])&&(b[xnn][ynn]==false)&&(abs(a[xnn][ynn]-a[x][y])<=c))
				{
					och[last][1]=xnn;
					och[last][2]=ynn;
					last++;
				}
			}
		}
		delCells();
		delColumn();
	}
	int cnt=0;
	for (i=1;i<=n;i++)
		cnt+=a[i][0];
	cout << cnt << endl;
	return 0;
}
