import Control.Monad
import Data.Bits
import Data.Array

limit = bit 16 - 1
bs2n = foldl (\x y -> x * 2 + y) 0

solve n as bs =  f 1 0
 where
  as' = listArray (1,n) $ map bs2n as
  bs' = map bs2n bs
  ar = listArray ((1,0),(n+1,limit)) [f i x| i <- [1..n+1], x <- [0..limit]]
  f i x
   | i > n = 0
   | otherwise = maximum [
    (k - popCount z) + (ar ! (i+1,z))| b <- bs', let z = y .&. complement b]
   where 
    y = x .|. as' ! i
    k = popCount y

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 $ solve n as bs
  main