import Debug.Trace
import Data.Array
import Control.Monad
type Prof_Diff = Array Int (Array Int Int)
type Business_Day = Array Int (Array Int Bool)


get_BD::Int -> IO Business_Day
get_BD n = do
  ss<-forM [1..n] (\_ ->  getLine)
  let lss = map (\st ->map (\x -> if x=="1" then True else False) (words st) ) ss
  let f   = listArray (1,n) (map (listArray (1,10)) lss)
  return f

get_Prof_Diff::Int -> IO Prof_Diff
get_Prof_Diff n = do
  ss<-forM [1..n] (\_ -> getLine)
  let lss = map (\st ->map read (words st)) ss
  let p   = listArray (1,n) (map (listArray (0,10)) lss)
  return p


all_pat::Int ->[[Bool]]
all_pat n = (iterate (\ls-> concat [map (b:) ls|b<-[False,True]]) [[]])!!n

value::Business_Day->Prof_Diff->Array Int Bool->Int
value bd pd pat = let (shop_i,shop_f) = bounds bd  in
                  let (day_i ,day_f ) = bounds pat in
                  let doubling        = listArray (shop_i,shop_f) [ (length.(filter (\d ->  (pat!d) && ((bd!j)!d) ) ) ) [day_i..day_f] |j<-[shop_i..shop_f]] in
                  sum [(pd!j)!(doubling!j) |j<-[shop_i..shop_f]]

solver::Business_Day->Prof_Diff->Int->Int
solver bd pd n = let pat = map (listArray (1,n)) (tail (all_pat n)) in
                 maximum (map (value bd pd) pat)
main::IO()
main=do
  nc<-getLine
  let n = read nc::Int
  bd<-get_BD n
  pd<-get_Prof_Diff n
  --print bd
  --print pd
  --print (value bd pd (take 10 (repeat True)))
  print (solver bd pd 10)
