# 2018 牛客多校第二场 J farm

White Rabbit has a rectangular farmland of n*m. In each of the grid there is a kind of plant. The plant in the j-th column of the i-th row belongs the a[i][j]-th type.
White Cloud wants to help White Rabbit fertilize plants, but the i-th plant can only adapt to the i-th fertilizer. If the j-th fertilizer is applied to the i-th plant (i!=j), the plant will immediately die.
Now White Cloud plans to apply fertilizers T times. In the i-th plan, White Cloud will use k[i]-th fertilizer to fertilize all the plants in a rectangle [x1[i]...x2[i]][y1[i]...y2[i]].
White rabbits wants to know how many plants would eventually die if they were to be fertilized according to the expected schedule of White Cloud.

#include <bits/stdc++.h>
const int MAXN=1e6+7;
using namespace std;
int tree[MAXN*20];
int son[MAXN*20][4];
int n,m,t;
int id;
vector<int>a[MAXN];
int stx,enx,sty,eny,v;

void push_down(int rt){
if(tree[rt]==0){
return;
}else if(tree[rt]==-1){
tree[son[rt][0]]=tree[son[rt][1]]=tree[son[rt][2]]=tree[son[rt][3]]=-1;
}else{
for(int i=0;i<4;i++){
if(tree[son[rt][i]]==0) tree[son[rt][i]]=tree[rt];
else if(tree[son[rt][i]]!=tree[rt]) tree[son[rt][i]]=-1;
else continue;
}
}
}
void build(int lx,int rx,int ly,int ry,int rt){
tree[rt]=0;
if(lx==rx&&ly==ry){

return ;
}
int midx=(lx+rx)/2;
int midy=(ly+ry)/2;
build(lx,midx,ly,midy,son[rt][0]=++id);
if(ly!=ry) build(lx,midx,midy+1,ry,son[rt][1]=++id);
if(lx!=rx) build(midx+1,rx,ly,midy,son[rt][2]=++id);
if(ly!=ry&&lx!=rx) build(midx+1,rx,midy+1,ry,son[rt][3]=++id);
}
int query(int lx,int rx,int ly,int ry,int rt){
if(tree[rt]==-1){
return 0;
}

if(lx==rx&&ly==ry){
if(tree[rt]==0||tree[rt]==a[lx][ly]){
return 1;
}
return 0;
}
int midx=(lx+rx)/2;
int midy=(ly+ry)/2;
push_down(rt);
int ans=0;
ans+=query(lx,midx,ly,midy,son[rt][0]);
if(ly!=ry) ans+=query(lx,midx,midy+1,ry,son[rt][1]);
if(lx!=rx) ans+=query(midx+1,rx,ly,midy,son[rt][2]);
if(ly!=ry&&lx!=rx) ans+=query(midx+1,rx,midy+1,ry,son[rt][3]);
return ans;
}
void update(int lx,int rx,int ly,int ry,int rt){
if(tree[rt]==-1||stx>rx||sty>ry||enx<lx||eny<ly) return ;
if(stx<=lx&&rx<=enx&&sty<=ly&&ry<=eny){
if(tree[rt]==0||tree[rt]==v){
tree[rt]=v;
return ;
}else{
tree[rt]=-1;
return ;
}
}
push_down(rt);
int midx=(lx+rx)/2;
int midy=(ly+ry)/2;
update(lx,midx,ly,midy,son[rt][0]);
if(ly!=ry) update(lx,midx,midy+1,ry,son[rt][1]);
if(lx!=rx) update(midx+1,rx,ly,midy,son[rt][2]);
if(ly!=ry&&lx!=rx) update(midx+1,rx,midy+1,ry,son[rt][3]);
//push_up(rt);
}
int main()
{

ios::sync_with_stdio(false);
//freopen("in.txt","r",stdin);

cin>>n>>m>>t;id=1;
if(n==0||m==0){cout<<0<<endl;return 0;}
for(int i=1;i<=n;i++){
a[i].push_back(0);
for(int j=1;j<=m;j++){int x;cin>>x;
a[i].push_back(x);
}
}

build(1,n,1,m,1);
while(t--){cin>>stx>>sty>>enx>>eny>>v;
update(1,n,1,m,1);
}
cout<<n*m-query(1,n,1,m,1)<<endl;
}