#include <iostream>
#include <math.h>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <queue>

using namespace std;
vector<vector<int>> v;
int c, n, m, cnt, ch;

int nov(){
	for (int i = 1; i <= n; ++i){
		cnt = 0;
		while(!v[i][1] && cnt <= m + 1){
			v[i].erase(v[i].begin() + 1);
			v[i].push_back(0);
			cnt++;
		}
		if(!v[i][1]){
			v.erase(v.begin() + i);
			n--;
			i--;
		}
	}
	return 0;
}

int ud(int a, int b, int ch){
	v[a][b]=0;
	if (abs(v[a][b - 1] - ch) <= c && v[a][b - 1])
		ud(a, b - 1, ch);
	if (abs(v[a][b + 1] - ch) <= c && v[a][b + 1])
		ud(a, b + 1, ch);
	if (abs(v[a - 1][b] - ch) <= c && v[a - 1][b])
		ud(a - 1, b, ch);
	if (abs(v[a + 1][b] - ch) <= c && v[a + 1][b])
		ud(a + 1, b, ch);
	return 0;
}

int main(){
	freopen("bricks.in", "rt", stdin);
	freopen("bricks.out", "wt", stdout);
	int tmp, k, ans = 0, n1, m1;
	vector<int> tmpv;
	scanf("%d %d %d", &n, &m, &c);
	for (int j = 0; j < m + 2; ++j){
		tmpv.push_back(0);
	}
	v.push_back(tmpv);
	tmpv.clear();

	for (int i = 0; i < n; ++i){
		tmpv.push_back(0);
		for (int j = 0; j < m; ++j){
			scanf("%d", &tmp);
			tmpv.push_back(tmp);
		}
		tmpv.push_back(0);
		v.push_back(tmpv);
		tmpv.clear();
	}
	for (int j = 0; j < m + 2; ++j){
		tmpv.push_back(0);
	}
	v.push_back(tmpv);
	tmpv.clear();

	scanf("%d", &k);
	for (int i = 0; i < k; ++i){
		scanf("%d %d", &n1, &m1);
		ud(n1, m1, v[n1][m1]);
		nov();
	}
	
	for (int i = 0; i <= n + 1; ++i)
		for (int j = 0; j <= m + 1; ++j)
			if (v[i][j] != 0)
				ans++;
	printf("%d", ans);

	return 0;
}