N = int(input())
F = []
P = []
for i in range(N):
    F.append(input().split().count("1"))
for i in range(N):
    P.append(list(map(int, input().split()[0:F[i]+1])))

result = 0;

for i in P:
    print(i)
    result += max(i)

print(result)
