#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <stack>
using namespace std;

int n,m,k,cnt,x,y,res;
int a[100][100];
bool f[100][100];

void Rec(int x, int y, int t)
{
	if (x<0 || y<0 || x>=n || y>=m)
		return;

	if (labs(a[x][y]-t) > k)
		return;

	if (f[x][y])
	{
		f[x][y] = false;
		--res;
		Rec(x-1,y,a[x][y]);
		Rec(x+1,y,a[x][y]);
		Rec(x,y-1,a[x][y]);
		Rec(x,y+1,a[x][y]);
	}
}

int main()
{
	freopen("bricks.in", "r", stdin);
	freopen("bricks.out", "w", stdout);

	scanf("%d%d%d", &n, &m, &k);
	for (int i=0; i<n; ++i)
		for (int j=0; j<m; ++j)
			scanf("%d", &a[i][j]),
			f[i][j] = true;
	res = n*m;

	scanf("%d", &cnt);
	for (int tt=0; tt<cnt; ++tt)
	{
		scanf("%d%d", &x, &y);
		Rec(x-1,y-1,a[x-1][y-1]);
		for (int i=0; i<n; ++i)
		{
			bool fExit;
			do
			{
				fExit = true;
				for (int j=1; j<m; ++j)
				{
					if (!f[i][j-1] && f[i][j])
						swap(f[i][j-1], f[i][j]),
						swap(a[i][j-1], a[i][j]),
						fExit = false;
				}
			} while (!fExit);
		}

		bool fExit;
		do
		{
			fExit = true;
			for (int i=1; i<n; ++i)
				if (!f[i-1][0] && f[i][0])
				{
					for (int j=0; j<m; ++j)
						swap(f[i-1][j], f[i][j]),
						swap(a[i-1][j], a[i][j]);
					fExit = false;
				}
		} while (!fExit);
	}

	printf("%d", res);
}