tmp = list(map(int, input().split(' ')))
N = tmp[0]; M = tmp[1]
tmp = list(map(int, input().split(' ')))
r = tmp[0]; c = tmp[1]; d = tmp[2]
board = []
for _ in range(N):
board.append(list(map(int, input().split(' '))))
visited = [[0] * M for _ in range(N)]
visited[r][c] = 1
dx = [-1, 0, 1, 0]
dy = [0, 1, 0, -1]
while True:
flag = 0
for i in range(4):
x = r; y = c
d = (d+3)%4
x += dx[d]; y += dy[d]
if x >= 0 and x < N and y >= 0 and y < M:
if board[x][y] == 0 and visited[x][y] == 0:
visited[x][y] = 1
r = x; c = y
flag = 1; break
if flag == 0:
r -= dx[d]; c -= dy[d]
if board[r][c] == 1:
break
ans = 0
for i in range(N):
for j in range(M):
ans += visited[i][j]
print(ans)
- 반시계방향으로 90도 회전 -> d = (d+3)%3
- 청소한 좌표를 N*M 크기의 list를 생성하여 1로 표시함 (visited)