main = do
  inputs <- getContents
  let n = read $ head . lines $ inputs :: Int
  let ept = reverse $ map (\xs -> map read (words xs)) $ take n $ tail $ lines inputs :: [[Int]]
  let prf = map (\xs -> map read (words xs)) $ take n $ reverse $ lines inputs :: [[Int]]
  let mpt = map (toBin 10) [1..1023] :: [[Int]]
  let epr = zip ept prf
  print $ maximum $ map sum $ [ map (dj xs) epr | xs <- mpt ]

toBin :: Int -> Int -> [Int]
toBin digit n = map read $ reverse $ toBin' digit n
  where
    toBin' 0 _ = []
    toBin' d n = (show (n `mod` 2)) : toBin' (d - 1) (n `div` 2)

dj mpt (ept, prf) = prf !! (sk mpt ept)
  where
    sk _ [] = 0
    sk (x:xs) (y:ys)
      | x == 1 && y == 1 = 1 + (sk xs ys)
      | otherwise        = sk xs ys