### subroutines

def countbits(bits)
  c = 0
  while bits != 0
    c+= 1 if (bits & 1) != 0
    bits >>= 1
  end
  c
end

def max_score(k, prev_l)
  return 0 if k >= $n
  return $mscache[k][prev_l] if ! $mscache[k][prev_l].nil?

  cur_l = prev_l | $as[k]
  max_sc = 0

  for b in $bs
    bl = (cur_l & b)
    l = cur_l ^ bl

    sc = countbits(bl) + max_score(k + 1, l)

    max_sc = sc if max_sc < sc
  end

  $mscache[k][prev_l] = max_sc
end

### main

while true
  $n, $c = gets.strip.split(' ').map{|s| s.to_i}
  break if $n == 0 && $c == 0

  $as = $n.times.map{gets.strip.gsub(' ', '').to_i(2)}
  $bs = $c.times.map{gets.strip.gsub(' ', '').to_i(2)}
  #p $as
  #p $bs

  $mscache = $n.times.map{[]}

  ms = max_score(0, 0)
  puts ms
end