import Control.Applicative
import qualified Data.Map.Lazy as M

merge :: [Integer] -> [Integer] -> [Integer]
merge xs [] = xs
merge [] ys = ys
merge (x:xs) (y:ys)
    | x <= y = x : merge xs (y:ys)
    | x > y = y : merge (x:xs) ys

ruiseki :: [Integer] -> [Integer]
ruiseki = scanl (+) 0

combination :: [Integer] -> [Integer]
combination [] = []
combination (x:xs) = merge (comb xs) (combination xs)
    where
      comb [] = []
      comb (y:ys) = (y - x) : comb ys

combination_cnt :: [Integer] -> M.Map Integer Integer
combination_cnt xs = M.fromAscListWith (+) (comb_cnt_one (combination xs))
    where 
      comb_cnt_one = map (\x -> (x, 1))


solve :: [Integer] -> [Integer] -> Integer
solve hs ws = M.foldrWithKey (\key val res -> res + val * (M.findWithDefault 0 key ws_comb_cnt)) 0 hs_comb_cnt
    where
      hs_comb_cnt = combination_cnt $ ruiseki hs
      ws_comb_cnt = combination_cnt $ ruiseki ws

in_out :: [[Integer]] -> [Integer]
in_out [] = []
in_out (nm:ins)
    | nm == [0, 0] = []
    | otherwise = (solve hs ws) : (in_out rems)
  where
    n = fromIntegral $ nm !! 0
    m = fromIntegral $ nm !! 1
    hs = concat $ take n ins
    ws = concat $ (take m) $ drop n ins
    rems = drop (n+m) ins

main :: IO ()
main = do
  ins <- fmap (fmap read . words) . lines <$> getContents
  putStr $ foldr (\res out -> (show res) ++ "\n" ++ out) "" (in_out ins)