μ£Όμ΄μ§ μμ μ΄λ¦μ μ¬νμλ‘ NXNμ 격μλ‘ λνλΌ μ μλ€. rν cμ΄μ ꡬμμ (r,c)λ‘ λνλΌ μ μκ³ μ£Όμ΄μ§ μλ₯Ό 5κ°μ μ κ±°κ΅¬λ‘ λλ μ μμ΄μΌ νλ©° κ° κ΅¬μμ λ€μ― μ 거ꡬ μ€ νλμ ν¬ν¨λμ΄μΌ νλ€.
μ 거ꡬλ₯Ό λλλ λ°©λ²μ λ€μκ³Ό κ°λ€.
1. κΈ°μ€μ (X, Y)μ κ²½κ³μ κΈΈμ΄ (d1, d2)λ₯Ό μ νλ€. (d1, d2 >=1, 1<=x<x+d1+d2<=N, 1<=y-d1<y<y+d2<=N)
2. κ²½κ³μ μ΄ μ£Όμ΄μ§λ€. (κ²½κ³μ μ λ²μλ +- d1, d2λ‘ μ΄ 4κ°)
3. κ²½κ³μ κ³Ό κ²½κ³μ λ΄ ν¬ν¨λ κ³³μ 5λ² μ 거ꡬ
- μ λ ₯κ° : 첫μ€μλ ν¬κΈ° n, λμ§Έ μ€λΆν° nμ€κΉμ§ nκ°μ μ μκ° μ£Όμ΄μ§.
- μΆλ ₯κ° : 첫μ€μ μΈκ΅¬κ° κ°μ₯λ§μ μ 거ꡬμ κ°μ₯ μ μ μ 거ꡬμ μΈκ΅¬ μ°¨μ΄ μ΅μκ° μΆλ ₯
import sys
input = sys.stdin.readline
n = int(input())
graph = [[0]*(n+1)] + [[0] + list(map(int,input().split())) for _ in range(n)]
total_sum = 0
for i in range(1, n+1):
total_sum += sum(graph[i])
def boundary(x, y, d1, d2):
tmp = [[0]*(n+1) for _ in range(n+1)]
tmp[x][y] = 5
for i in range(1, d1+1):
tmp[x+i][y-i] = 5
for i in range(1, d1+1):
tmp[x+d2+i][y+d2-i] = 5
for i in range(1, d2+1):
tmp[x+i][y+i] = 5
for i in range(1, d2+1):
tmp[x+d1+i][y-d1+i] = 5
# μ 거ꡬ 5κ°
people = [0] * 5
# 1λ² μ 거ꡬ
for i in range(1, x+d1):
for j in range(1, y+1):
if tmp[i][j] == 5:
break
else:
people[0] += graph[i][j]
# 2λ²
for i in range(1, x+d2+1):
for j in range(n, y, -1):
if tmp[i][j] == 5:
break
else:
people[1] += graph[i][j]
# 3λ²
for i in range(x+d1, n+1):
for j in range(1, y-d1+d2):
if tmp[i][j] == 5:
break
else:
people[2] += graph[i][j]
# 4λ²
for i in range(x+d2+1, n+1):
for j in range(n, y-d1+d2-1, -1):
if tmp[i][j] == 5:
break
else:
people[3] += graph[i][j]
# 5λ²
people[4] += total_sum - sum(people)
return max(people) - min(people)
answer = 1e9
for x in range(1, n+1):
for y in range(1, n+1):
for d1 in range(1,n+1):
for d2 in range(1, n+1):
if x+d1+d2 > n:
continue
if y-d1 < 1:
continue
if y+d2 > n:
continue
answer = min(answer, boundary(x,y,d1,d2))
print(answer)