import Control.Monad
import Data.List
import qualified Data.IntMap as IM
import Debug.Trace

type Map = IM.IntMap (IM.IntMap Int)

main :: IO ()
main = do
  n <- readLn :: IO Int
  fs <- replicateM n $ map (== 1) <$> getInts
  ps <- replicateM n $ zip [0..] <$> getInts
  let as = IM.fromList $ zip [1..] $ map IM.fromList ps
  print $ solve fs as

getInts :: IO [Int]
getInts = map read . words <$> getLine

solve :: [[Bool]] -> Map -> Int
solve fs ps = maximum $ go xs
  where
    xs = init $ replicateM 10 [True, False]
    go [] = []
    go (y:ys) =
      let ns = f y fs in
--        trace (show ns) $
        foldr (\(i, j) b ->
                  (ps IM.! i) IM.! j + b) 0 ns : go ys
    f ns ms = zip [1..] $
              map (length
                   . filter id . zipWith (&&) ns) ms
