https://www.acmicpc.net/problem/17144
def spread():
global board
dy, dx = [-1,0,1,0], [0,1,0,-1]
new_board = [[0 if board[y][x] != -1 else -1 for x in range(c)] for y in range(r)]
for y in range(r):
for x in range(c):
center, side = board[y][x], board[y][x] // 5
if center <= 0:
continue
for d in range(4):
ny, nx = y + dy[d], x + dx[d]
if 0 <= ny < r and 0 <= nx < c and board[ny][nx] >= 0:
new_board[ny][nx] += side
center -= side
new_board[y][x] += center
board = new_board
def clean():
for y in range(r):
if board[y][0] == -1:
go(0, y+1, y, -1)
go(y+1, r, y+1, 1)
break
# 범위1, 범위2, 시작, 방향
def go(r1, r2, y, d):
dy, dx = [d,0,-d,0], [0,1,0,-1]
board[y][0] = 0
board[y+d][0] = -1
cy, cx = y, 0
for d in range(4):
while True:
ny, nx = cy + dy[d], cx + dx[d]
# 마지막 제외
if ny == y and nx == 0:
break
# 벗어날 경우
if not (r1 <= ny < r2 and 0 <= nx < c):
break
board[cy][cx], board[ny][nx] = board[ny][nx], board[cy][cx]
cy, cx = ny, nx
# init
import sys
read = sys.stdin.readline
r, c, t = map(int, read().split())
board = [list(map(int, read().split())) for _ in range(r)]
# start
for _ in range(t):
spread()
clean()
print(sum([sum(board[i]) for i in range(r)]) + 2)