n = int(input()) inf = 1 << 60 dp1 = [-inf] * n dp2 = [-inf] * n dp3 = [-inf] * n dp1[0] = 0 dp2[0] = 0 dp3[0] = 0 for t in range(n): ndp1 = [-inf] * n ndp2 = [-inf] * n ndp3 = [-inf] * n A = list(map(int, input().split())) for a in A: for j in range(n): nj = j + a v = max(dp2[j], dp3[j]) if nj > n: v += (n - t) * (nj - n) nj = n v += nj nj = max(nj - 1, 0) ndp1[nj] = max(ndp1[nj], v) dp1, dp2, dp3 = dp2, dp3, dp1 ndp1, ndp2, ndp3 = ndp2, ndp3, ndp1 dp1 = ndp1 dp2 = ndp2 dp3 = ndp3 print(max(max(dp1), max(dp2), max(dp3)))