Buggy python submission (verdict: Runtime Error):
while True:
    n, m = map(int, input().split())
    if n == 0 and m == 0:
        break
    h = [0]
    for i in range(n):
        h.append(int(input()) + h[i])
    w = [0]
    for i in range(m):
        w.append(int(input()) + w[i])

    hList = [0] * 1000
    for i in range(n + 1):
        for j in range(i, n + 1):
            if i == j:
                continue
            else:
                hList[abs(h[i] - h[j])] += 1
    wList = [0] * 1000
    for i in range(m + 1):
        for j in range(i, m + 1):
            if i == j:
                continue
            else:
                wList[abs(w[i] - w[j])] += 1

    squareList = []
    for i in range(1000):
        squareList.append(hList[i] * wList[i])
    print(sum(squareList))


Fixed submission:
while True:
    n, m = map(int, input().split())
    if n == 0 and m == 0:
        break
    h = [0]
    for i in range(n):
        h.append(int(input()) + h[i])
    w = [0]
    for i in range(m):
        w.append(int(input()) + w[i])

    hDict = {}
    for i in range(n + 1):
        for j in range(i, n + 1):
            if i == j:
                continue
            else:
                diff = abs(h[i] - h[j])
                if diff in hDict:
                    hDict[diff] += 1
                else:
                    hDict[diff] = 1
    wDict = {}
    for i in range(m + 1):
        for j in range(i, m + 1):
            if i == j:
                continue
            else:
                diff = abs(w[i] - w[j])
                if diff in wDict:
                    wDict[diff] += 1
                else:
                    wDict[diff] = 1

    ans = 0
    for diff in hDict.keys():
        if diff in wDict:
            ans += hDict[diff] * wDict[diff]
    print(ans)

Buggy python submission (verdict: Runtime Error):
while True:
    n, m = map(int, input().split())
    if n == 0 and m == 0:
        break
    h = [0]
    for i in range(n):
        h.append(int(input()) + h[i])
    w = [0]
    for i in range(m):
        w.append(int(input()) + w[i])

    hList = [0] * 1000
    for i in range(n + 1):
        for j in range(i, n + 1):
            if i == j:
                continue
            else:
                hList[abs(h[i] - h[j])] += 1
    wList = [0] * 1000
    for i in range(m + 1):
        for j in range(i, m + 1):
            if i == j:
                continue
            else:
                wList[abs(w[i] - w[j])] += 1

    squareList = []
    for i in range(1000):
        squareList.append(hList[i] * wList[i])
    print(sum(squareList))


Fixed submission:
while True:
    n, m = map(int, input().split())
    if n == 0 and m == 0:
        break
    h = [0]
    for i in range(n):
        h.append(int(input()) + h[i])
    w = [0]
    for i in range(m):
        w.append(int(input()) + w[i])

    hDict = {}
    for i in range(n + 1):
        for j in range(i, n + 1):
            if i == j:
                continue
            else:
                diff = abs(h[i] - h[j])
                if diff in hDict:
                    hDict[diff] += 1
                else:
                    hDict[diff] = 1
    wDict = {}
    for i in range(m + 1):
        for j in range(i, m + 1):
            if i == j:
                continue
            else:
                diff = abs(w[i] - w[j])
                if diff in wDict:
                    wDict[diff] += 1
                else:
                    wDict[diff] = 1

    ans = 0
    for diff in hDict.keys():
        if diff in wDict:
            ans += hDict[diff] * wDict[diff]
    print(ans)

Buggy python submission (verdict: Runtime Error):
while True:
    n, m = map(int, input().split())
    if n == 0 and m == 0:
        break
    h = [0]
    for i in range(n):
        h.append(int(input()) + h[i])
    w = [0]
    for i in range(m):
        w.append(int(input()) + w[i])

    hList = [0] * 1000
    for i in range(n + 1):
        for j in range(i, n + 1):
            if i == j:
                continue
            else:
                hList[abs(h[i] - h[j])] += 1
    wList = [0] * 1000
    for i in range(m + 1):
        for j in range(i, m + 1):
            if i == j:
                continue
            else:
                wList[abs(w[i] - w[j])] += 1

    squareList = []
    for i in range(1000):
        squareList.append(hList[i] * wList[i])
    print(sum(squareList))


Fixed submission:
while True:
    n, m = map(int, input().split())
    if n == 0 and m == 0:
        break
    h = [0]
    for i in range(n):
        h.append(int(input()) + h[i])
    w = [0]
    for i in range(m):
        w.append(int(input()) + w[i])

    hDict = {}
    for i in range(n + 1):
        for j in range(i, n + 1):
            if i == j:
                continue
            else:
                diff = abs(h[i] - h[j])
                if diff in hDict:
                    hDict[diff] += 1
                else:
                    hDict[diff] = 1
    wDict = {}
    for i in range(m + 1):
        for j in range(i, m + 1):
            if i == j:
                continue
            else:
                diff = abs(w[i] - w[j])
                if diff in wDict:
                    wDict[diff] += 1
                else:
                    wDict[diff] = 1

    ans = 0
    for diff in hDict.keys():
        if diff in wDict:
            ans += hDict[diff] * wDict[diff]
    print(ans)