# 【題解】ZeroJudge b840: 104北二4.農作物採收問題

```#include <iostream>
using namespace std;

int n, arr[25][25], maxi, temp;

int main() {
cin >> n;
for (int i = 1; i <= n; i++){
for (int j = 1; j <= n; j++){
cin >> arr[i][j];
}
}
for (int i = 1; i <= n; i++){
for (int j = 1; j <= n; j++){
arr[i][j] += arr[i-1][j] + arr[i][j-1] - arr[i-1][j-1];
}
}

maxi = 0;
for (int a = 1; a <= n; a++){
for (int b = 1; b <= n; b++){
for (int c = 0; c < a; c++){
for (int d = 0; d < b; d++){
temp = arr[a][b]-arr[a][d]+arr[ c][d]-arr[ c][b];
if (temp > maxi) maxi = temp;
}
}
}
}
cout << maxi << "\n";
}
```

Python code (credit: Amy Chou, 9/12/2020)

```#測資有不預期的換行符號，須自行處理
tmp = []
while True:
try:
tmp.extend(list(map(int, input().split())))
except:
break

n = tmp[0]
tmp =  tmp[1:]
#把讀入的測資轉換成 n*n 的陣列
a = [[0 for _ in range(n+1)]]
for i in range(n):
a.append([0]+tmp[i*n:(i+1)*n])

for i in range(1, n+1):
for j in range(1, n+1):
a[i][j] += a[i-1][j] + a[i][j-1] - a[i-1][j-1]

mx = 0
for i in range(1, n+1):
for j in range(1, n+1):
for k in range(i):
for l in range(j):
tmp = a[i][j] - a[i][l] - a[k][j] + a[k][l]
mx = max(mx, tmp)
print(mx)
```