import sys
from collections import deque
input=sys.stdin.readline
N,M,T=map(int,input().split())
arr=[deque(map(int,input().split())) for _ in range(N)]
for tc in range(T):
x,d,k=map(int,input().split())
x-=1
d=1 if d==0 else -1
for idx in range(x,N,x+1):
for turn in range(k):
arr[idx].rotate(d)
remove=[]
# 양 옆 확인하기
for r in range(N):
for c in range(-1,M-1):
if arr[r][c]=='x' or arr[r][c+1]=='x': continue
if arr[r][c]==arr[r][c+1]:
remove.extend([(r,c),(r,c+1)])
# 위 아래 확인하기
for c in range(M):
for r in range(N-1):
if arr[r][c] == 'x' or arr[r+1][c] == 'x': continue
if arr[r][c]==arr[r+1][c]:
remove.extend([(r,c),(r+1,c)])
for r,c in set(remove):
arr[r][c]='x' # 값 지우기
total, cnt = 0, 0
for r in range(N):
for c in range(M):
if arr[r][c] != 'x':
total += arr[r][c]
cnt += 1
if len(remove)==0:
if cnt!=0:
avg=total/cnt # 평균 비교해서 값 수정하기
for r in range(N):
for c in range(M):
if arr[r][c]!='x':
if arr[r][c]<avg: arr[r][c]+=1
elif arr[r][c]>avg: arr[r][c]-=1
total=0
for r in range(N):
for c in range(M):
if arr[r][c] != 'x':
total += arr[r][c]
print(total)