from collections import deque
from itertools import permutations
N = int(input())
scores = []
for i in range(N):
    scores.append(list(map(int,input().split())))

answer = 0
batters = permutations([1,2,3,4,5,6,7,8],8)
for entry in batters:
    orders = list(entry)
    orders.insert(3,0)
    temp = 0
    index = 0
    for inning in scores:
        outcount = 0
        b1, b2, b3 = 0, 0, 0
        while outcount!=3:
            if inning[orders[index%9]]==0:
                outcount+=1
            elif inning[orders[index%9]]==1:
                temp += b3
                b3, b2, b1 = b2, b1, 1
                
            elif inning[orders[index%9]]==2:
                temp += b3+b2
                b3, b2 ,b1 = b1, 1, 0
                
            elif inning[orders[index%9]]==3:
                temp += b3+b2+b1
                b3, b2, b1 = 1, 0, 0

            else:
                temp += b3+b2+b1+1
                b3, b2, b1 = 0, 0, 0

            index+=1
        
    if temp>answer:
        answer = temp
        
print(answer)

#베이스 처리를 덱으로 했으나 시간초과로 인해 그냥 개별변수로 처리하였다.






'''
from collections import deque
from itertools import permutations,combinations
N = int(input())
scores = []
for i in range(N):
    scores.append(list(map(int,input().split())))

def getScore(inning, orders):
    global order
    outcount = 0
    score = 0
    base = deque([])
    while outcount!=3:
        if inning[orders[order%9]]==0:
            outcount+=1
        elif inning[orders[order%9]]==1:
            base.appendleft(1)
        elif inning[orders[order%9]]==2:
            base.appendleft(1)
            base.appendleft(0)
        elif inning[orders[order%9]]==3:
            base.appendleft(1)
            base.appendleft(0)
            base.appendleft(0)
        elif inning[orders[order%9]]==4:
            base.appendleft(1)
            base.appendleft(0)
            base.appendleft(0)
            base.appendleft(0)
        
        while len(base)>3:
            home = base.pop()
            if home==1:
                score += 1
        order+=1
    return score

answer = 0
batters = []
for result in scores:
    result = list(enumerate(result))
    result.sort(key= lambda x: x[1])
    while result[0][1]==0:
        temp = result.pop(0)
        result.append(temp)
    for i in range(9):
        if result[i][0]==0:
            temp = result.pop(i)
            result.insert(3, temp)
            break
    batters.append([x[0] for x in result])
print(batters)


for entry in batters:
    order = 0
    temp = 0
    for inning in scores:
        temp += getScore(inning, entry)
    if temp>answer:
        answer = temp

print(answer)
'''