




import sys
input = sys.stdin.readline
dx = (-1, 0, 1, 0)
dy = (0, 1, 0, -1)
N, M, K = map(int, input().split())
board = [[[] for m in range(N)] for n in range(N)]
for n in range(N):
tmp = list(map(int, input().split()))
for m in range(N):
if tmp[m] != 0: board[n][m].append(tmp[m])
player = dict(); player_pos = dict()
for m in range(M):
x, y, d, s = map(int, input().split())
player[m+1] = [x-1, y-1, d, s, 0]
player_pos[(x-1, y-1)] = m+1
def changeGun(m, x, y):
global player
max_, idx_ = player[m][4], -1
for i in range(len(board[x][y])):
if board[x][y][i] > max_:
max_ = board[x][y][i]
idx_ = i
if idx_ != -1:
if player[m][4] != 0:
board[x][y][idx_] = player[m][4]
else:
board[x][y].pop(idx_)
player[m][4] = max_
def processBattle(win, lose, x, y):
global player, player_pos
score_stat[win] += abs((player[win][3]+player[win][4]) - (player[lose][3]+player[lose][4]))
if player[lose][4] != 0:
board[x][y].append(player[lose][4])
player[lose][4] = 0
del player_pos[(player[win][0], player[win][1])]
del player_pos[(player[lose][0], player[lose][1])]
d = player[lose][2]
while True:
nx, ny = x+dx[d], y+dy[d]
if (nx, ny) not in player_pos and 0<=nx<N and 0<=ny<N:
if board[nx][ny]:
changeGun(lose, nx, ny)
player[lose][0] = nx; player[lose][1] = ny
player_pos[(nx, ny)] = lose
player[lose][2] = d
break
d = (d+1)%4
player[win][0] = x; player[win][1] = y
player_pos[(x, y)] = win
changeGun(win, x, y)
score_stat = [0] * (M+1)
for k in range(K):
for m in range(1, M+1):
x, y, d, s, g = player[m]
nx, ny = x+dx[d], y+dy[d]
if nx<0 or nx>=N or ny<0 or ny>=N: d = (d+2)%4; nx, ny = x+dx[d], y+dy[d]; player[m][2] = d
if (nx, ny) in player_pos:
n = player_pos[(nx, ny)]
if (player[m][3]+player[m][4]) > (player[n][3]+player[n][4]):
processBattle(m, n, nx, ny)
elif (player[m][3]+player[m][4]) == (player[n][3]+player[n][4]):
if player[m][3] > player[n][3]:
processBattle(m, n, nx, ny)
else:
processBattle(n, m, nx, ny)
else:
processBattle(n, m, nx, ny)
else:
if board[nx][ny]:
changeGun(m, nx, ny)
player[m][0] = nx; player[m][1] = ny
del player_pos[(x, y)]; player_pos[(nx, ny)] = m
print(" ".join(map(str, score_stat[1:])))