#include <iostream>

using namespace std;

const int NMAX=50, MMAX=50;

int n,m,c,k,answer;
int a[MMAX+1][NMAX+1];
int t[MMAX+1];

int diff(int a, int b){
  int res = a-b;
  if (res<0) res = -res;
  return res;
}

void clear (int x, int y, int b){

  a[y][x]=-100000;
  if (x-1>=1 && a[y][x-1]>0 && diff(a[y][x-1],b)<=c) clear(x-1,y,b);
  if (x+1<=n && a[y][x+1]>0 && diff(a[y][x+1],b)<=c) clear(x+1,y,b);
  if (y-1>=1 && a[y-1][x]>0 && diff(a[y-1][x],b)<=c) clear(x,y-1,b);
  if (y+1<=m && a[y+1][x]>0 && diff(a[y+1][x],b)<=c) clear(x,y+1,b);
}

void remove() {
  for (int i=1; i<=n; i++){
    int k=0;
    for (int j=1; j<=m; j++)
      if (a[j][i]>0) t[k++]=a[j][i];
    for (int j=1; j<=m; j++) a[j][i]=-100000;
    for (int j=0; j<k; j++) a[j+1][i]=t[j];
    a[0][i] = k;
  }

  //теперь - сдвиг колонн
  for (int i=1; i<n; i++) {
    if (a[0][i]>0) continue;
    for (int j=i+1; j<=n; j++) {
      if (a[0][j]==0) continue;
      for (int k=0; k<=m; k++){
        a[k][i]=a[k][j];
        a[k][j]=-100000;
      }
      a[0][j]=0;
      break;
    }
  }
}

int main(){
//  freopen ("bricks.in","r",stdin);
//  freopen ("bricks.out","w",stdout);
  cin >> n >> m >> c;
  for (int i=1; i<=n; i++)
    for (int j=1; j<=m; j++)
      cin >> a[j][i];
  for (int i=1; i<=n; i++)
    a[0][i] = m; //кирпичей в колонне


  //debug 
/*
  for (int j=1; j<=m; j++){
    for (int i=1; i<=n; i++)
      if (a[j][i]>0) printf("%3d",a[j][i]); else printf("%3d",0);
    cout << endl;
  }
  cout << endl;
*/

  cin >> k;
  for(; k>0; k--) {
    int y,x; 
    cin >> x >> y;
    if (a[y][x]<0) {
      cout << "Error" << endl;
      return 0;
    }
    clear(x,y,a[y][x]);
    remove();

  //debug 
/*
  for (int j=1; j<=m; j++){
    for (int i=1; i<=n; i++)
      if (a[j][i]>0) printf("%3d",a[j][i]); else printf("%3d",0);
    cout << endl;
  }
  cout << endl;
*/

  }

  answer=0;
  for (int i=1; i<=n; i++)
    for (int j=1; j<=m; j++)
      if (a[j][i]>0) answer++;

  cout << answer << endl;
  return 0;
}
