---
# System prepended metadata

title: ZeroJudge d206 max subarray 2D
tags: [zerojudge, python]

---

ZeroJudge d206 max subarray 2D
```python=
def maxsub(mat):
    n = len(mat)
    ans = -1
    for i in range(n): # summing up mat[i:j+1] to one row
        row = [0]*n
        for j in range(i,n):
            for k in range(n):
                row[k] += mat[j][k]
            # max subarray
            imax = 0
            for q in row:
                imax = max(0,imax)+q
                if imax>ans: ans=imax
    return ans
    
a = []
while True:
    try:
        a += [int(x) for x in input().split()]
    except:
        break
idx = 0
while idx<len(a): # for one test case
    n = a[idx]
    n2 =n*n
    mat = []
    idx += 1
    for i in range(0,n2,n):
        mat.append(a[idx+i:idx+i+n])
    idx += n2
    print(maxsub(mat))

```