import Control.Monad

f xs (ys:yss) zss = maximum [
 sum rs - sum ss + f ss yss zss | zs <- zss, let ss = zipWith h rs zs]
 where 
  rs = zipWith g xs ys
  g 0 0 = 0
  g _ _ = 1
  h 1 0 = 1
  h _ _ = 0
f _ _ _ = 0

getNums = fmap (map read . words) getLine

main = do
 [n,c] <- getNums
 unless (n == 0 && c == 0) $ do
  as <- replicateM n getNums
  bs <- replicateM c getNums
  print $ f xs as bs
  main
 where xs = replicate 16 0