/*
    
    Problem : http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2015
    
    Coding time : 20min
    Debugging time : 30min
    idea:
        (1) load  h_1 - h_N and w_1 - w_M
        (2) calcurate the values of x-coordinate of north-south road
         and the values of y-coordinate of east-west road (h[n], w[m])
        (3) calcurate h[j] - h[i] (0 <= i < j <= n) and w[j] - w[i] (0 <= i < j <= m)
        (4) sort h[j] - h[i] (0 <= i < j <= n) and w[j] - w[i] (0 <= i < j <= m)
        (5) find pair of h[j] - h[i] (0 <= i < j <= n) and w[j] - w[i] (0 <= i < j <= m)
         which are equal and count the number
        (6) output the number
     Things that I devised
        (1) calcurated the values of x-coordinate of north-south road
         and the values of y-coordinate of east-west road (h[n], w[m])
         in order to make it easy to calcurate h[j] - h[i] (0 <= i < j <= n) and w[j] - w[i] (0 <= i < j <= m)
        (2) sorted h[j] - h[i] (0 <= i < j <= n) and w[j] - w[i] (0 <= i < j <= m) before finding pairs
         so that the computational complexity is O(n * log(n) + m * log(m) + n + m), not O(n * m)
     Things that was hard
        (1) At first, I missed some cases (the following case is one example), so it took long time to debug
            3 3
            1
            1
            2
            1
            1
            3
            
            (*) was like this before debugging
            if(sh[i] == sw[j]){
                cnt++;
                i++;
            }
*/

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
 
int main() {
    int n, m, i, j, cnt, hsum, wsum, tmp, cnti, cntj;
    while (1) {
        cin >> n >> m;
        if (n == 0 && m == 0)break;
        int h[1502], w[1502];
        vector<int> sh, sw;
        hsum = wsum = 0;
        h[0] = 0;
        for (i = 1; i <= n; i++) {
            cin >> tmp;
            hsum += tmp;
            h[i] = hsum;
        }
        w[0] = 0;
        for (i = 1; i <= m; i++) {
            cin >> tmp;
            wsum += tmp;
            w[i] = wsum;
        }
         
        for (i = 0; i <= n - 1; i++) {
            for (j = i + 1; j <= n; j++) {
                sh.push_back(h[j] - h[i]);
            }
        }
        for (i = 0; i <= m - 1; i++) {
            for (j = i + 1; j <= m; j++) {
                sw.push_back(w[j] - w[i]);
            }
        }
 
        sort(sh.begin(), sh.end());
        sort(sw.begin(), sw.end());
         
        i = j = 0;
        cnt = 0;
        int maxi = sh.size(), maxj = sw.size();
        while (i < maxi && j < maxj) {                        // <---- (*)
            if (sh[i] == sw[j]) {
                cnti = cntj = 1;
                while (i < maxi - 1 && sh[i] == sh[i + 1]) {
                    cnti++;
                    i++;
                }
                while (j < maxj - 1 && sw[j] == sw[j + 1]) {
                    cntj++;
                    j++;
                }
                cnt += cnti * cntj;
                i++;
                j++;
            } else if (sh[i] < sw[j]) {
                i++;
            } else {
                j++;
            }
        }
         
        cout << cnt << endl;
         
    }
 
 
 
 
    return 0;
}

Measured memory_bytes: 19340