https://www.acmicpc.net/problem/1780
from sys import stdin
input = stdin.readline
# 입력
n = int(input())
paper = []
for _ in range(n):
data = list(map(int, input().split()))
paper.append(data)
# -1 개수, 0 개수, 1 개수
negative_one = 0
zero = 0
one = 0
# 재귀함수 solve(한 변의 길이, 좌측최상단 row idx, 좌측 최상단 col idx)
def solve(length, row, col):
# 전역 변수 선언
global negative_one
global zero
global one
#stop case 1: 한 변의 길이가 1이 되었을 때
if length == 1:
if paper[row][col] == -1:
negative_one += 1
elif paper[row][col] == 0:
zero += 1
else:
one += 1
return
# stop case 2: 그 영역 내 모든 수가 동일할 때
all_same = True
prev = None
for r in range(length):
for c in range(length):
# 맨 처음 케이스
if prev == None:
prev = paper[row+r][col+c]
else:
# 같지 않은 것이 하나라도 있다면 탐색 중단
if paper[row+r][col+c] != prev:
# all_same 플래그 값 변경
all_same = False
break
if all_same:
if paper[row][col] == -1:
negative_one += 1
elif paper[row][col] == 0:
zero += 1
else:
one += 1
return
# 계속 하는 경우
next_length = length//3
solve(next_length, row, col)
solve(next_length, row, col + next_length)
solve(next_length, row, col + next_length * 2)
solve(next_length, row + next_length, col)
solve(next_length, row + next_length, col + next_length)
solve(next_length, row + next_length, col + next_length * 2)
solve(next_length, row + next_length * 2, col)
solve(next_length, row + next_length * 2, col + next_length)
solve(next_length, row + next_length * 2, col + next_length * 2)
# 함수 호출
solve(n, 0, 0)
# 출력
print(negative_one)
print(zero)
print(one)