#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
using namespace std;

#define REP(i, s, e) for (int i = (s); i < (e); i++)
#define REPI(i, s, e) for (int i = (s); i <= (e); i++)
#define rep(i, n) REP(i, 0, n)
#define repi(i, n) REPI(i, 0, n)
#define ALL(v) (v).begin(), (v).end()

#define dump(x) (cout << #x << " = " << x << endl)
#define dump2(x, y)                                                            \\
  (cout << "(" << #x << ", " << #y << ") = (" << x << ", " << y << ")" << endl)

typedef long long ll;
typedef pair<int, int> pii;

int main(void) {
  int n, q;
  while (cin >> n >> q, n | q) {
    vector<int> vs;
    rep(i, n) {
      int m;
      cin >> m;
      rep(j, m) {
        int t;
        cin >> t;
        vs.push_back(t);
      }
    }

    vector<pii> ans;
    rep(i, vs.size()) {
      int n = vs[i];
      int cnt = 0;
      REP(j, i, vs.size())
      if (vs[j] == n)
        cnt++;
      ans.push_back(pii(cnt, n));
    }

    rep(i, ans.size())
        REP(j, i + 1, ans.size()) if (ans[i].first < ans[j].first)
            swap(ans[i], ans[j]);

    if (ans[0].first >= q)
      cout << ans[0].second << endl;
    else
      cout << 0 << endl;
  }
  return 0;
}

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
using namespace std;

#define REP(i, s, e) for (int i = (s); i < (e); i++)
#define REPI(i, s, e) for (int i = (s); i <= (e); i++)
#define rep(i, n) REP(i, 0, n)
#define repi(i, n) REPI(i, 0, n)
#define ALL(v) (v).begin(), (v).end()

#define dump(x) (cout << #x << " = " << x << endl)
#define dump2(x, y)                                                            \\
  (cout << "(" << #x << ", " << #y << ") = (" << x << ", " << y << ")" << endl)

typedef long long ll;
typedef pair<int, int> pii;

int main(void) {
  int n, q;
  while (cin >> n >> q, n | q) {
    vector<int> vs;
    rep(i, n) {
      int m;
      cin >> m;
      rep(j, m) {
        int t;
        cin >> t;
        vs.push_back(t);
      }
    }

    vector<pii> ans;
    rep(i, vs.size()) {
      int n = vs[i];
      int cnt = 0;
      REP(j, i, vs.size())
      if (vs[j] == n)
        cnt++;
      ans.push_back(pii(cnt, n));
    }

    rep(i, ans.size())
        REP(j, i + 1, ans.size()) if (ans[i].first < ans[j].first)
            swap(ans[i], ans[j]);
    rep(i, ans.size())
        REP(j, i + 1, ans.size()) if (ans[i].first == ans[j].first &&
                                      ans[i].second > ans[j].second)
            swap(ans[i], ans[j]);

    if (ans.size() == 0 || ans[0].first < q)
      cout << 0 << endl;
    else
      cout << ans[0].second << endl;
  }
  return 0;
}


#include <limits.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {
  int n, m;
  int i, j, k, l;
  scanf("%d %d", &n, &m);

  while (1) {
    if ((n == 0) && (m == 0)) {
      break;
    } else {
      int date[101] = {0};

      for (i = 0; i < n; i++) {
        int o;
        int buf;
        scanf("%d", &o);
        for (j = 0; j < o; j++) {
          scanf("%d", &buf);
          date[buf]++;
        }
      }

      for (j = 1; j < (n + 1); j++) {
        if (date[j] >= m) {
          printf("%d\\n", date[j]);
          break;
        }
      }
    }
  }
  return 0;
}

#include <limits.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {
  int n, m;
  int i, j, k, l;
  int ans, sum;

  while (1) {
    scanf("%d %d", &n, &m);
    if ((n == 0) && (m == 0)) {
      break;
    } else {
      int date[102] = {0};

      for (i = 0; i < n; i++) {
        int o;
        int buf;
        scanf("%d", &o);
        for (j = 0; j < o; j++) {
          scanf("%d", &buf);
          date[buf]++;
        }
      }

      ans = 0;
      sum = 0;

      for (j = 1; j < 102; j++) {
        if (date[j] > sum) {
          sum = date[j];
          ans = j;
        }
      }

      if (sum >= m) {
        printf("%d\\n", ans);
      } else {
        printf("0\\n");
      }
    }
  }
  return 0;
}


// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1124

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <numeric>
#include <vector>

#define ALL(v) (v).begin(), (v).end()
#define REP(i, p, n) for (int i = p; i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define dump(a) (cerr << #a << "=" << (a) << endl)
#define DUMP(list)                                                             \\
  cout << "{ ";                                                                \\
  for (auto nth : list) {                                                      \\
    cout << nth << " ";                                                        \\
  }                                                                            \\
  cout << "}" << endl;

using namespace std;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N, Q, M;

  while (cin >> N >> Q) {
    if (N == 0 && Q == 0) {
      break;
    }

    vector<int> date;
    rep(nth, N) {
      cin >> M;

      int tmp;
      rep(i, M) {
        cin >> tmp;
        date.push_back(tmp);
      }
    }

    int MAX = *max_element(ALL(date));
    int count = 0;
    bool flag = false;
    for (int m = N; m >= Q; --m) {
      REP(num, 1, MAX + 1) {
        count = 0;
        rep(i, date.size()) {
          if (date[i] == num) {
            count++;
          }
          if (count == m) {
            cout << num << endl;
            flag = true;
            goto end;
          }
        }
      }
    }
  end:
    if (!flag) {
      cout << 0 << endl;
    }
  }
  return 0;
}

// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1124

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <numeric>
#include <vector>

#define ALL(v) (v).begin(), (v).end()
#define REP(i, p, n) for (int i = p; i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define dump(a) (cerr << #a << "=" << (a) << endl)
#define DUMP(list)                                                             \\
  cout << "{ ";                                                                \\
  for (auto nth : list) {                                                      \\
    cout << nth << " ";                                                        \\
  }                                                                            \\
  cout << "}" << endl;

using namespace std;

int main() {
  int N, Q, M;

  while (cin >> N >> Q) {
    if (N == 0 && Q == 0) {
      break;
    }

    vector<int> date;
    rep(nth, N) {
      cin >> M;

      int tmp;
      rep(i, M) {
        cin >> tmp;
        date.push_back(tmp);
      }
    }

    if (date.size() == 0) {
      date.push_back(0);
    }

    int MAX = *max_element(ALL(date));
    int count = 0;
    bool flag = false;
    for (int m = N; m >= Q; --m) {
      REP(num, 1, MAX + 1) {
        count = 0;
        rep(i, date.size()) {
          if (date[i] == num) {
            count++;
          }
          if (count == m) {
            cout << num << endl;
            flag = true;
            goto end;
          }
        }
      }
    }
  end:
    if (!flag) {
      cout << 0 << endl;
    }
  }
  return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define REP(i, s, n) for (int i = s; i < n; ++i)
#define rep(i, n) REP(i, 0, n)
#define SORT(c) sort((c).begin(), (c).end())
#define IINF INT_MAX
#define LLINF LLONG_MAX

typedef long long ll;
typedef pair<int, int> ii;

int main() {

  int n, q;
  while (cin >> n >> q, n, q) {
    vector<int> cnt(51, 0);
    rep(i, n) {
      int m;
      cin >> m;
      rep(j, m) {
        int tmp;
        cin >> tmp;
        cnt[tmp]++;
      }
    }

    int ans = 0;
    int ans_c = 0;

    rep(i, 51) {
      if (ans_c < cnt[i]) {
        ans_c = cnt[i];
        ans = i;
      }
    }

    cout << (ans_c >= q ? ans : 0) << endl;
  }

  return 0;
}

#include <bits/stdc++.h>
using namespace std;

#define REP(i, s, n) for (int i = s; i < n; ++i)
#define rep(i, n) REP(i, 0, n)
#define SORT(c) sort((c).begin(), (c).end())
#define IINF INT_MAX
#define LLINF LLONG_MAX

typedef long long ll;
typedef pair<int, int> ii;

#define MAXI 150

int main() {

  int n, q;
  while (cin >> n >> q, n, q) {
    vector<int> cnt(MAXI, 0);
    rep(i, n) {
      int m;
      cin >> m;
      rep(j, m) {
        int tmp;
        cin >> tmp;
        cnt[tmp]++;
      }
    }

    int ans = 0;
    int ans_c = 0;

    rep(i, MAXI) {
      if (ans_c < cnt[i]) {
        ans_c = cnt[i];
        ans = i;
      }
    }

    cout << (ans_c >= q ? ans : 0) << endl;
  }

  return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main() {
  int N, Q, M, temp;

  while (1) {
    int temp2[60] = {}, ma = 0, ans = 0;
    cin >> N >> Q;

    if (N == 0 && Q == 0)
      break;

    for (int i = 1; i <= N; i++) {
      cin >> M;
      for (int j = 1; j <= M; j++) {
        cin >> temp;
        temp2[temp]++;
      }
    }

    for (int i = 1; i <= 55; i++) {
      if (temp2[ans] < temp2[i])
        ans = i;
    }

    if (temp2[ans] >= Q)
      cout << ans << endl;
    else
      cout << 0 << endl;
  }
  return 0;
}

#include <bits/stdc++.h>
using namespace std;

int main() {
  int N, Q, M, temp;

  while (1) {
    int temp2[300] = {}, ans = 0;
    cin >> N >> Q;

    if (N == 0 && Q == 0)
      break;

    for (int i = 1; i <= N; i++) {
      cin >> M;
      for (int j = 1; j <= M; j++) {
        cin >> temp;
        temp2[temp]++;
      }
    }

    for (int i = 1; i <= 250; i++) {
      if (temp2[ans] < temp2[i])
        ans = i;
    }

    if (temp2[ans] >= Q)
      cout << ans << endl;
    else
      cout << 0 << endl;
  }
  return 0;
}


#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;

int main() {
  while (1) {
    int n, m;
    cin >> n >> m;
    if (n == 0)
      break;

    vector<int> h(n + 1), w(m + 1);
    for (int i = 0; i < n; i++)
      cin >> h[i + 1];
    for (int i = 0; i < m; i++)
      cin >> w[i + 1];
    for (int i = 1; i <= n; i++)
      h[i] += h[i - 1];
    for (int i = 1; i <= m; i++)
      w[i] += w[i - 1];

    int cnt = 0;
    for (int i1 = n; i1 >= 0; i1--) {
      for (int j1 = i1 + 1; j1 < n + 1; j1++) {
        for (int i2 = m; i2 >= 0; i2--) {
          for (int j2 = i2 + 1; j2 < m + 1; j2++) {
            if (h[j1] - h[i1] == w[j2] - w[i2]) {
              cnt++;
            } else if (h[j1] - h[i1] < w[j2] - w[i2])
              break;
          }
        }
      }
    }

    cout << cnt << endl;
  }
  return 0;
}

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;

int main() {
  while (1) {
    int n, m;
    cin >> n >> m;
    if (n == 0)
      break;

    vector<int> h(n + 1), w(m + 1);
    for (int i = 0; i < n; i++)
      cin >> h[i + 1];
    for (int i = 0; i < m; i++)
      cin >> w[i + 1];
    for (int i = 1; i <= n; i++)
      h[i] += h[i - 1];
    for (int i = 1; i <= m; i++)
      w[i] += w[i - 1];

    vector<int> hs(1500001), ws(1500001);

    for (int i1 = n; i1 >= 0; i1--) {
      for (int j1 = i1 + 1; j1 < n + 1; j1++) {
        hs[h[j1] - h[i1]]++;
      }
    }

    for (int i2 = m; i2 >= 0; i2--) {
      for (int j2 = i2 + 1; j2 < m + 1; j2++) {
        ws[w[j2] - w[i2]]++;
      }
    }

    long long ans = 0;
    for (int i = 1; i <= 1500000; i++) {
      ans += hs[i] * ws[i];
    }

    cout << ans << endl;
  }
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (k); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define INF 114514810
#define MOD 1000000007
#define ALL(a) (a).begin(), (a).end()
#define ms(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(10)
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> P;
typedef long long ll;
struct edge {
  int from, to, cost;
};

int main() {
  int n, m;
  int h[2000];
  int w[2000];
  while (cin >> n >> m, n) {
    REP(i, n) cin >> h[i];
    REP(i, m) cin >> w[i];
    multiset<int> ari;
    int sh[2000];
    int sw[2000];
    sh[0] = sw[0] = 0;
    FOR(i, 1, n + 1) sh[i] = sh[i - 1] + h[i - 1];
    FOR(i, 1, m + 1) sw[i] = sw[i - 1] + w[i - 1];
    REP(i, n) FOR(j, i + 1, n + 1) { ari.insert(sh[j] - sh[i]); }
    int cnt = 0;
    REP(i, m) FOR(j, i + 1, m + 1) { cnt += ari.count(sw[j] - sw[i]); }
    cout << cnt << endl;
  }
  return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (k); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define INF 114514810
#define MOD 1000000007
#define ALL(a) (a).begin(), (a).end()
#define ms(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(10)
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> P;
typedef long long ll;
struct edge {
  int from, to, cost;
};

int main() {
  int n, m;
  int h[2000];
  int w[2000];
  while (cin >> n >> m, n) {
    REP(i, n) cin >> h[i];
    REP(i, m) cin >> w[i];
    map<int, int> ari;
    int sh[2000];
    int sw[2000];
    sh[0] = sw[0] = 0;
    FOR(i, 1, n + 1) sh[i] = sh[i - 1] + h[i - 1];
    FOR(i, 1, m + 1) sw[i] = sw[i - 1] + w[i - 1];
    REP(i, n) FOR(j, i + 1, n + 1) {
      int t = sh[j] - sh[i];
      if (ari.find(t) == ari.end())
        ari[t] = 1;
      else
        ari[t]++;
    }
    int cnt = 0;
    REP(i, m) FOR(j, i + 1, m + 1) { cnt += ari[sw[j] - sw[i]]; }
    cout << cnt << endl;
  }
  return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
#define FOR(i, a, b) for (int i = (a); i < (int)(b); i++)
#define rep(i, n) FOR(i, 0, n)
#define ALL(x) (x).begin(), (x).end()
#define ZERO(a) memset(a, 0, sizeof(a))

const int M = 1500;
ll h[M + 5], w[M + 5];

void solve(int H, int W) {
  ZERO(h);
  ZERO(w);
  FOR(i, 1, H + 1) scanf("%lld", &h[i]);
  FOR(i, 1, W + 1) scanf("%lld", &w[i]);

  rep(i, H) h[i + 1] += h[i];
  rep(i, W) w[i + 1] += w[i];

  vector<ll> widths;
  FOR(i, 1, W + 1) rep(j, i) widths.push_back(w[i] - w[j]);

  ll ans = 0;
  FOR(i, 1, H + 1) {
    rep(j, i) {
      ll height = h[i] - h[j];
      ans += count(ALL(widths), height);
    }
  }

  cout << ans << endl;
}

int main() {
  int H, W;
  while (cin >> H >> W, H)
    solve(H, W);
  return 0;
}

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
#define FOR(i, a, b) for (int i = (a); i < (int)(b); i++)
#define rep(i, n) FOR(i, 0, n)
#define ALL(x) (x).begin(), (x).end()
#define ZERO(a) memset(a, 0, sizeof(a))

const int M = 1500;
ll h[M + 5], w[M + 5];

void solve(int H, int W) {
  ZERO(h);
  ZERO(w);
  FOR(i, 1, H + 1) scanf("%lld", &h[i]);
  FOR(i, 1, W + 1) scanf("%lld", &w[i]);

  rep(i, H) h[i + 1] += h[i];
  rep(i, W) w[i + 1] += w[i];

  map<ll, ll> nums;
  FOR(i, 1, W + 1) rep(j, i) nums[w[i] - w[j]]++;

  ll ans = 0;
  FOR(i, 1, H + 1) {
    rep(j, i) {
      ll height = h[i] - h[j];
      ans += nums[height];
    }
  }

  cout << ans << endl;
}

int main() {
  int H, W;
  while (cin >> H >> W, H)
    solve(H, W);
  return 0;
}


#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < ((int)(n)); i++)
#define reg(i, a, b) for (int i = ((int)(a)); i <= ((int)(b)); i++)
#define irep(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
#define ireg(i, a, b) for (int i = ((int)(b)); i >= ((int)(a)); i--)
typedef long long ll;
typedef pair<ll, ll> mp;

int main(void) {
  ll ans[1000], cnt = 0;
  rep(i, 1000) ans[i] = 0;
  while (true) {
    int N, M;
    cin >> N >> M;
    if (N == 0 && M == 0)
      break;
    int h[1501], w[1501];
    reg(i, 1, N) cin >> h[i];
    reg(i, 1, M) cin >> w[i];
    ll DP[1501], max; // i???????????§????´???????
    DP[0] = 0;
    rep(i, N) DP[i + 1] = DP[i] + w[i + 1];
    max = DP[N];
    int mapW[max + 1];
    rep(i, max + 1) mapW[i] = 0;
    reg(i, 1, N) { //??§??????
      reg(j, 0, N - i) mapW[DP[j + i] - DP[j]]++;
    }
    rep(i, M) DP[i + 1] = DP[i] + h[i + 1];
    if (max < DP[M])
      max = DP[M];
    int mapH[max + 1];
    rep(i, max + 1) mapH[i] = 0;
    reg(i, 1, M) { //??§??????
      reg(j, 0, M - i) mapH[DP[j + i] - DP[j]]++;
    }
    rep(i, max + 1) { ans[cnt] += mapW[i] * mapH[i]; }
    cnt++;
  }
  rep(i, cnt) cout << ans[i] << endl;
  return 0;
}


#include <algorithm>
#include <fstream>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string>
#include <vector>

#define FOR(k, m, n) for (int(k) = (m); (k) < (n); (k)++)
#define REP(i, n) FOR((i), 0, (n))
#define LL long long
#define CLR(a) memset((a), 0, sizeof(a))
#define SZ(x) (int((x).size()))
#define WAITING(str)                                                           \\
  int str;                                                                     \\
  std::cin >> str;
#define DEBUGING(str) cout << str << endl
using namespace std;

const LL MOD = 1000000007; // 10^9+7
const int INF = (1 << 30);

// 計算
void calc(vector<LL> vh, vector<LL> vw) {
  map<LL, LL> mh, mw;

  REP(i, vh.size() - 1) vh[i + 1] += vh[i];
  REP(i, vh.size() - 1) vw[i + 1] += vw[i];

  REP(i, vh.size()) FOR(j, i + 1, vh.size()) {
    LL len = vh[j] - vh[i];
    if (mh.find(len) == mh.end())
      mh[len] = 1;
    else
      mh[len]++;
  }
  REP(i, vw.size()) FOR(j, i + 1, vw.size()) {
    LL len = vw[j] - vw[i];
    if (mw.find(len) == mw.end())
      mw[len] = 1;
    else
      mw[len]++;
  }

  LL res = 0;
  for (auto itr = mh.begin(); itr != mh.end(); ++itr) {
    LL len = itr->first;
    LL cnt = itr->second;

    auto wItr = mw.find(len);
    if (wItr != mw.end()) {
      res += cnt * wItr->second;
    }
  }
  // cerr << "=======";
  cout << res << endl;
}

// デバッグ
void debug() {
  int N;
  cin >> N;
}

// メイン関数
int main() {
  while (true) {
    int N, M;
    vector<LL> h, w;
    cin >> N >> M;
    if (N == 0)
      break;

    h.resize(N + 1);
    REP(i, N) cin >> h[i + 1];
    w.resize(M + 1);
    REP(i, M) cin >> w[i + 1];

    calc(h, w);
  }

  debug();

  return 0;
}

#include <algorithm>
#include <fstream>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string>
#include <vector>

#define FOR(k, m, n) for (int(k) = (m); (k) < (n); (k)++)
#define REP(i, n) FOR((i), 0, (n))
#define LL long long
#define CLR(a) memset((a), 0, sizeof(a))
#define SZ(x) (int((x).size()))
#define WAITING(str)                                                           \\
  int str;                                                                     \\
  std::cin >> str;
#define DEBUGING(str) cout << str << endl
using namespace std;

const LL MOD = 1000000007; // 10^9+7
const int INF = (1 << 30);

// 計算
void calc(vector<LL> vh, vector<LL> vw) {
  map<LL, LL> mh, mw;

  REP(i, vh.size() - 1) vh[i + 1] += vh[i];
  REP(i, vw.size() - 1) vw[i + 1] += vw[i];

  REP(i, vh.size()) FOR(j, i + 1, vh.size()) {
    LL len = vh[j] - vh[i];
    if (mh.find(len) == mh.end())
      mh[len] = 1;
    else
      mh[len]++;
  }
  REP(i, vw.size()) FOR(j, i + 1, vw.size()) {
    LL len = vw[j] - vw[i];
    if (mw.find(len) == mw.end())
      mw[len] = 1;
    else
      mw[len]++;
  }

  LL res = 0;
  for (auto itr = mh.begin(); itr != mh.end(); ++itr) {
    LL len = itr->first;
    LL cnt = itr->second;

    auto wItr = mw.find(len);
    if (wItr != mw.end()) {
      res += cnt * wItr->second;
    }
  }
  // cerr << "=======";
  cout << res << endl;
}

// デバッグ
void debug() {
  int N;
  cin >> N;
}

// メイン関数
int main() {
  while (true) {
    int N, M;
    vector<LL> h, w;
    cin >> N >> M;
    if (N == 0)
      break;

    h.resize(N + 1);
    REP(i, N) cin >> h[i + 1];
    w.resize(M + 1);
    REP(i, M) cin >> w[i + 1];

    calc(h, w);
  }

  // debug();

  return 0;
}


#include <math.h>
#include <stdio.h>
#include <string.h>

int main() {
  int tate[1501], yoko[1501], n, m, h, w, i, j, k, max, s, e;
  tate[0] = yoko[0] = 0;
  while (~scanf("%d%d", &n, &m)) {
    if (n == 0 && m == 0)
      break;

    for (i = 1; i <= n; i++) {
      scanf("%d", &h);
      tate[i] = tate[i - 1] + h;
    }
    for (i = 1; i <= m; i++) {
      scanf("%d", &h);
      yoko[i] = yoko[i - 1] + h;
    }

    max = 0;

    for (i = 0; i < n; i++) {
      for (j = 0; j < m; j++) {
        e = i + 1;
        s = j + 1;
        //				printf("%d %d\\n",e,s);
        while (e <= n && s <= m) {
          if (tate[e] - tate[i] == yoko[s] - yoko[j]) {
            ++max;
            ++s;
            ++e;
          }
          if (tate[e] - tate[i] > yoko[s] - yoko[j])
            ++s;
          if (tate[e] - tate[i] < yoko[s] - yoko[j])
            ++e;
        }
      }
    }
    printf("%d\\n", max);
  }
  return 0;
}

/**
 *  Square Route (Problem D, Practice Contest for Japan Domestic, 2007-06-24)
 *  by Kenji Inoue, 2007-06-28
 **/

#include <iostream>
#include <map>

using namespace std;

int main() {
  int h[1501], w[1501];

  int N, M;
  while (cin >> N >> M, (N || M)) {
    for (int i = 0; i < N; i++) {
      int t;
      cin >> t;
      h[i] = t;
    }
    for (int i = 0; i < M; i++) {
      int t;
      cin >> t;
      w[i] = t;
    }

    map<int, int> V, H;
    for (int i = 0; i < N; i++) {
      int len = 0;
      for (int j = i; j < N; j++) {
        len += h[j];
        V[len]++;
      }
    }
    for (int i = 0; i < M; i++) {
      int len = 0;
      for (int j = i; j < M; j++) {
        len += w[j];
        H[len]++;
      }
    }

    int count = 0;
    for (map<int, int>::iterator it = V.begin(); it != V.end(); it++) {
      int key = it->first;
      count += V[key] * H[key];
    }
    cout << count << endl;
  }

  return 0;
}


#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define INF 1e09

using std::cin;
using std::cout;
using std::endl;
using std::fixed;
using std::list;
using std::make_pair;
using std::map;
using std::max;
using std::min;
using std::pair;
using std::priority_queue;
using std::queue;
using std::set; // insert erase find count
using std::setprecision;
using std::stack;
using std::string;
using std::vector;

typedef vector<int> VI;
typedef vector<vector<int>> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<int, pair<int, int>> PIII;

int main(void) {
  while (1) {
    int n;
    int m;
    cin >> n >> m;
    if (n == 0 && m == 0) {
      break;
    }
    VI vn(n);
    VI vm(m);
    for (int i = 0; i < n; i++) {
      cin >> vn[i];
    }
    for (int i = 0; i < m; i++) {
      cin >> vm[i];
    }

    VI listn;
    for (int i = 0; i < n; i++) {
      int temp = vn[i];
      listn.push_back(temp);
      for (int j = i + 1; j < n; j++) {
        temp += vn[j];
        listn.push_back(temp);
      }
    }
    VI listm;
    for (int i = 0; i < m; i++) {
      int temp = vm[i];
      listm.push_back(temp);
      for (int j = i + 1; j < m; j++) {
        temp += vm[j];
        listm.push_back(temp);
      }
    }

    sort(listn.begin(), listn.end());
    sort(listm.begin(), listm.end());

    int count = 0;
    int now_i = 0;
    int now_j = 0;

    while (1) {
      if (now_i == (int)listn.size() || now_j == (int)listm.size()) {
        break;
      }
      if (listn[now_i] < listm[now_j]) {
        while (listn[now_i] < listm[now_j]) {
          now_i++;
        }
        continue;
      } else if (listn[now_i] > listm[now_j]) {
        while (listn[now_i] > listm[now_j]) {
          now_j++;
        }
        continue;
      } else {
        int count_i = 0;
        int count_j = 0;
        int temp_i = listn[now_i];
        int temp_j = listm[now_j];
        while (listn[now_i] == temp_i) {
          now_i++;
          count_i++;
        }
        while (listm[now_j] == temp_j) {
          now_j++;
          count_j++;
        }
        count += (count_i * count_j);
      }
    }
    cout << count << endl;
  }

  return 0;
}

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define INF 1e09

using std::cin;
using std::cout;
using std::endl;
using std::fixed;
using std::list;
using std::make_pair;
using std::map;
using std::max;
using std::min;
using std::pair;
using std::priority_queue;
using std::queue;
using std::set; // insert erase find count
using std::setprecision;
using std::stack;
using std::string;
using std::vector;

typedef vector<int> VI;
typedef vector<vector<int>> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<int, pair<int, int>> PIII;

int main(void) {
  while (1) {
    int n;
    int m;
    cin >> n >> m;
    if (n == 0 && m == 0) {
      break;
    }
    VI vn(n);
    VI vm(m);
    for (int i = 0; i < n; i++) {
      cin >> vn[i];
    }
    for (int i = 0; i < m; i++) {
      cin >> vm[i];
    }

    VI listn;
    for (int i = 0; i < n; i++) {
      int temp = vn[i];
      listn.push_back(temp);
      for (int j = i + 1; j < n; j++) {
        temp += vn[j];
        listn.push_back(temp);
      }
    }
    VI listm;
    for (int i = 0; i < m; i++) {
      int temp = vm[i];
      listm.push_back(temp);
      for (int j = i + 1; j < m; j++) {
        temp += vm[j];
        listm.push_back(temp);
      }
    }

    sort(listn.begin(), listn.end());
    sort(listm.begin(), listm.end());

    int count = 0;
    int now_i = 0;
    int now_j = 0;
    int size_i = (int)listn.size();
    int size_j = (int)listm.size();

    while (1) {
      if (now_i == size_i || now_j == size_j) {
        break;
      }
      if (listn[now_i] < listm[now_j]) {
        while (listn[now_i] < listm[now_j] && now_i != size_i) {
          now_i++;
        }
        continue;
      } else if (listn[now_i] > listm[now_j]) {
        while (listn[now_i] > listm[now_j] && now_j != size_j) {
          now_j++;
        }
        continue;
      } else {
        int count_i = 0;
        int count_j = 0;
        int temp_i = listn[now_i];
        int temp_j = listm[now_j];
        while (listn[now_i] == temp_i && now_i != size_i) {
          now_i++;
          count_i++;
        }
        while (listm[now_j] == temp_j && now_j != size_j) {
          now_j++;
          count_j++;
        }
        count += (count_i * count_j);
      }
    }
    cout << count << endl;
  }

  return 0;
}


#include <iostream>
using namespace std;

int main() {
  int n, m;
  int h[1500], w[1500];

  while (1) {
    cin >> n >> m;
    if (!n && !m)
      break;

    for (int i = 0; i < n; i++)
      cin >> h[i];
    for (int i = 0; i < m; i++)
      cin >> w[i];

    int ans = 0;

    for (int i = 0; i < n; i++) {
      int y = 0;
      for (int j = i; j < n; j++) {
        y += h[j];
        int s = 0;
        int g = 0;
        int x = w[s];
        while (s < m) {
          if (y == x) {
            ans++;
            x -= w[s];
            if (g < m - 1) {
              g++;
              x += w[g];
            }
            s++;
          } else if (y > x) {
            if (g < m - 1) {
              g++;
              x += w[g];
            } else {
              s = m;
            }
          } else {
            x -= w[s];
            if (s == g && g < m - 1) {
              g++;
              x += w[g];
            }
            s++;
          }
        }
      }
    }

    cout << ans << endl;
  }
}

#include <iostream>
using namespace std;

int main() {
  int n, m;
  int h[1500], w[1500];
  int y[1500000], x[1500000];
  int tmp;

  while (1) {
    cin >> n >> m;
    if (!n && !m)
      break;

    int max;
    if (n > m)
      max = n;
    else
      max = m;

    for (int i = 0; i < n; i++)
      cin >> h[i];
    for (int i = 0; i < m; i++)
      cin >> w[i];

    for (int i = 0; i < max * 1000; i++) {
      y[i] = 0;
      x[i] = 0;
    }

    for (int i = 0; i < n; i++) {
      tmp = 0;
      for (int j = i; j < n; j++) {
        tmp += h[j];
        y[tmp]++;
      }
    }

    for (int i = 0; i < m; i++) {
      tmp = 0;
      for (int j = i; j < m; j++) {
        tmp += w[j];
        x[tmp]++;
      }
    }

    int ans = 0;
    for (int i = 0; i < max * 1000; i++)
      ans += y[i] * x[i];

    cout << ans << endl;
  }
}


#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
const double EPS = 1e-9;
const double PI = acos(-1.0);

int main() {
  int n, m;
  while (cin >> n >> m, n | m) {
    vi h(n);
    REP(i, n) { cin >> h[i]; }
    vi w(m);
    REP(i, m) { cin >> w[i]; }
    int ans = 0;
    REP(i, n) {
      REP(j, m) {
        int k = i, l = j;
        int height = h[k], width = w[l];
        while (k < n && l < m) {
          bool down = false, right = false;
          if (height == width) {
            ans++;
            down = true;
            right = true;
          } else if (height < width) {
            down = true;
          } else {
            right = true;
          }
          if (down) {
            k++;
            if (k == n)
              break;
            height += h[k];
          }
          if (right) {
            l++;
            if (l == m)
              break;
            width += w[l];
          }
        }
      }
    }
    cout << ans << endl;
  }
}

#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
const double EPS = 1e-9;
const double PI = acos(-1.0);

int main() {
  int n, m;
  while (cin >> n >> m, n | m) {
    vi h(n);
    REP(i, n) { cin >> h[i]; }
    vi w(m);
    REP(i, m) { cin >> w[i]; }

    vi all_width;
    REP(i, m) {
      int sum = 0;
      FOR(j, i, m) {
        sum += w[j];
        all_width.push_back(sum);
      }
    }
    sort(ALL(all_width));

    int ans = 0;
    REP(i, n) {
      int total_height = 0;
      FOR(j, i, n) {
        total_height += h[j];
        vi::iterator lb = lower_bound(ALL(all_width), total_height);
        if (*lb == total_height) {
          ans += upper_bound(ALL(all_width), total_height) - lb;
        }
      }
    }

    cout << ans << endl;
  }
}


#include <iostream>
#include <map>
using namespace std;

int main() {

  int w, h;
  int x[1502] = {}, y[1502] = {};

  while (cin >> w >> h, w) {
    for (int i = 1; i <= h; i++) {
      cin >> y[i];
      y[i] += y[i - 1];
    }
    for (int i = 1; i <= w; i++) {
      cin >> x[i];
      x[i] += x[i - 1];
    }

    map<int, int> data;
    map<int, int> data2;
    int ans = 0;
    for (int i = 1; i <= h; i++) {
      for (int i2 = i; i2 <= h; i2++) {
        int cnt = 0;
        if (data[y[i2] - y[i - 1]] != 1) {
          data[y[i2] - y[i - 1]] = 1;
          for (int j = 1; j <= w; j++) {
            for (int j2 = j; j2 <= w; j2++) {
              if (y[i2] - y[i - 1] < x[j2] - x[j - 1])
                break;
              if (y[i2] - y[i - 1] == x[j2] - x[j - 1])
                cnt++;
            }
          }
          data2[y[i2] - y[i - 1]] = cnt;
        }
        ans += data2[y[i2] - y[i - 1]];
      }
    }

    cout << ans << endl;
  }

  return 0;
}

#include <algorithm>
#include <iostream>
using namespace std;

int X[1500001] = {}, Y[1500001] = {};

int main() {

  int w, h;

  while (cin >> h >> w) {
    if (w == 0 && h == 0)
      break;

    int ans = 0;
    int x[1501] = {}, y[1501] = {};
    for (int i = 0; i < h; i++)
      cin >> y[i];
    for (int i = 0; i < w; i++)
      cin >> x[i];

    for (int i = 0; i < 1500001; i++) {
      X[i] = 0;
      Y[i] = 0;
    }

    for (int i = 0; i < w; i++) {
      int sum = 0;
      for (int j = i; j < w; j++) {
        sum += x[j];
        X[sum]++;
      }
    }
    for (int i = 0; i < h; i++) {
      int sum = 0;
      for (int j = i; j < h; j++) {
        sum += y[j];
        Y[sum]++;
      }
    }

    for (int i = 0; i < 1500001; i++) {
      ans += X[i] * Y[i];
    }

    cout << ans << endl;
  }

  return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main(void) {
  int N, M;
  vector<int> w, h;
  vector<int> wsum, hsum;
  while (cin >> N >> M, N | M) {
    h.resize(N);
    hsum.resize(N + 1);
    w.resize(M);
    wsum.resize(M + 1);
    hsum[0] = wsum[0] = 0;
    for (int i = 0; i < N; i++) {
      cin >> h[i];
      if (i != 0) {
        hsum[i + 1] = hsum[i] + h[i];
      } else {
        hsum[i + 1] = h[i];
      }
    }
    for (int i = 0; i < M; i++) {
      cin >> w[i];
      if (i != 0) {
        wsum[i + 1] = wsum[i] + w[i];
      } else {
        wsum[i + 1] = w[i];
      }
    }
    /*
       for(int i=0; i < N; i++){
       cout << hsum[i] << " " << endl;
       }
       for(int i=0; i < M; i++){
       cout << wsum[i] << " " << endl;
       }
     */

    // DEBUG
    // vector< pair< pair<int, int>, pair<int, int> > > ans;
    int ans = 0;
    // GUBED
    for (int hend = 1; hend < N + 1; hend++) {
      for (int hbegin = 0; hbegin < hend; hbegin++) {
        for (int wend = 1; wend < M + 1; wend++) {
          for (int wbegin = 0; wbegin < wend; wbegin++) {
            if (hsum[hend] - hsum[hbegin] == wsum[wend] - wsum[wbegin]) {
              // ans.push_back(make_pair(make_pair(hbegin, hend),
              // make_pair(wbegin, wend)));
              ans++;
            }
          }
        }
      }
    }

    // cout << ans.size() << endl;
    cout << ans << endl;
  }
  return 0;
}

#include <bits/stdc++.h>
using namespace std;

int main(void) {
  int N, M;
  vector<int> w, h;
  vector<int> wsum, hsum;
  while (cin >> N >> M, N | M) {
    map<int, int> hm, wm;
    h.resize(N);
    hsum.resize(N + 1);
    w.resize(M);
    wsum.resize(M + 1);
    hsum[0] = wsum[0] = 0;
    for (int i = 0; i < N; i++) {
      cin >> h[i];
      if (i != 0) {
        hsum[i + 1] = hsum[i] + h[i];
      } else {
        hsum[i + 1] = h[i];
      }
    }
    for (int i = 0; i < M; i++) {
      cin >> w[i];
      if (i != 0) {
        wsum[i + 1] = wsum[i] + w[i];
      } else {
        wsum[i + 1] = w[i];
      }
    }

    for (int i = 0; i < M + 1; i++) {
      for (int j = i + 1; j < M + 1; j++) {
        int a = wsum[j] - wsum[i];
        if (wm.find(a) == wm.end()) {
          wm[a] = 1;
        } else {
          wm[a]++;
        }
      }
    }
    int ans = 0;
    for (int i = 0; i < N + 1; i++) {
      for (int j = i + 1; j < N + 1; j++) {
        int a = hsum[j] - hsum[i];
        if (wm.find(a) != wm.end()) {
          ans += wm[a];
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}


#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int main() {
  int n, m;
  while (cin >> n >> m, n) {
    int h[1500], w[1500];
    for (int i = 0; i < n; i++)
      cin >> h[i];
    for (int i = 0; i < m; i++)
      cin >> w[i];
    vector<int> sumh, sumw;
    int tmp[1500];
    tmp[0] = h[0];
    for (int i = 1; i < n; i++)
      tmp[i] = tmp[i - 1] + h[i];
    for (int i = 0; i < n; i++) {
      for (int j = i; j < n; j++) {
        sumh.push_back(tmp[j] - (i ? tmp[i - 1] : 0));
      }
    }
    tmp[0] = w[0];
    for (int i = 1; i < m; i++)
      tmp[i] = tmp[i - 1] + w[i];
    for (int i = 0; i < m; i++) {
      for (int j = i; j < m; j++) {
        sumw.push_back(tmp[j] - (i ? tmp[i - 1] : 0));
      }
    }
    sort(sumh.begin(), sumh.end());
    sort(sumw.begin(), sumw.end());
    int c = 0;
    int now = 0;
    int sto;
    for (int i = 0; i < sumh.size() && now < sumw.size(); i++) {
      while (sumh[i] > sumw[now])
        now++;
      sto = now;
      while (sumh[i] == sumw[now])
        now++, c++;
      now = sto;
    }
    cout << c << endl;
  }
  return 0;
}

#include <algorithm>
#include <iostream>
using namespace std;
int hc[1125750], wc[1125750];
main() {
  int n, m;
  while (cin >> n >> m, n) {
    int h[1501] = {}, w[1501] = {};
    for (int i = 1; i <= n; i++)
      cin >> h[i];
    for (int i = 1; i <= n; i++)
      h[i] += h[i - 1];
    for (int i = 1; i <= m; i++)
      cin >> w[i];
    for (int i = 1; i <= m; i++)
      w[i] += w[i - 1];
    int c = 0;
    for (int i = 0; i <= n; i++)
      for (int j = i + 1; j <= n; j++)
        hc[c++] = h[j] - h[i];
    int d = 0;
    for (int i = 0; i <= m; i++)
      for (int j = i + 1; j <= m; j++)
        wc[d++] = w[j] - w[i];
    int cnt = 0;
    sort(hc, hc + c);
    sort(wc, wc + d);
    for (int i = 0; i < c; i++) {
      cnt += upper_bound(wc, wc + d, hc[i]) - lower_bound(wc, wc + d, hc[i]);
    }
    cout << cnt << endl;
  }
}


#define _USE_MATH_DEFINES
#include "bits/stdc++.h"
using namespace std;

// #define int long long
#define DBG 1
#define dump(o)                                                                \\
  if (DBG) {                                                                   \\
    cerr << #o << " " << o << endl;                                            \\
  }
#define dumpc(o)                                                               \\
  if (DBG) {                                                                   \\
    cerr << #o;                                                                \\
    for (auto &e : (o))                                                        \\
      cerr << " " << e;                                                        \\
    cerr << endl;                                                              \\
  }
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define each(it, c) for (auto it = (c).begin(); it != (c).end(); it++)
#define all(c) c.begin(), c.end()
const int INF =
    sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9 + 7);
const double EPS = 1e-10;

signed main() {
  int N, M;
  while (cin >> N >> M, N || M) {
    vector<int> h(N + 1, 0), w(M + 1, 0);
    rep(i, 1, N + 1) cin >> h[i];
    rep(i, 1, M + 1) cin >> w[i];
    rep(i, 1, N + 1) h[i] += h[i - 1];
    rep(i, 1, M + 1) w[i] += w[i - 1];

    int ans(0);

    rep(i, 0, N + 1) {
      rep(j, 0, M + 1) {
        int ax = h[i], ay = w[j];
        rep(k, i + 1, N + 1) {
          rep(l, j + 1, M + 1) {
            int bx = h[k], by = w[l];
            if (abs(ax - bx) == abs(ay - by))
              ans++;
          }
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}

#define _USE_MATH_DEFINES
#include "bits/stdc++.h"
using namespace std;

// #define int long long
#define DBG 1
#define dump(o)                                                                \\
  if (DBG) {                                                                   \\
    cerr << #o << " " << o << endl;                                            \\
  }
#define dumpc(o)                                                               \\
  if (DBG) {                                                                   \\
    cerr << #o;                                                                \\
    for (auto &e : (o))                                                        \\
      cerr << " " << e;                                                        \\
    cerr << endl;                                                              \\
  }
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define each(it, c) for (auto it = (c).begin(); it != (c).end(); it++)
#define all(c) c.begin(), c.end()
const int INF =
    sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9 + 7);
const double EPS = 1e-10;

signed main() {
  int N, M;
  while (cin >> N >> M, N || M) {
    vector<int> h(N), w(M);
    rep(i, 0, N) cin >> h[i];
    rep(i, 0, M) cin >> w[i];
    int MAX = 1500009;
    vector<int> x(MAX, 0), y(MAX, 0);

    rep(i, 0, N) {
      int t(0);
      rep(j, i, N) {
        t += h[j];
        x[t]++;
      }
    }

    rep(i, 0, M) {
      int t(0);
      rep(j, i, M) {
        t += w[j];
        y[t]++;
      }
    }

    int ans(0);
    rep(i, 0, MAX) ans += x[i] * y[i];
    cout << ans << endl;
  }
  return 0;
}


#include "bits/stdc++.h"
#define ll long long
using namespace std;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N, M;
  while (true) {
    cin >> N >> M;
    if (N == 0 && M == 0)
      break;
    vector<ll> H(N + 1, 0), W(M + 1, 0);
    for (int i = 1; i <= N; i++) {
      cin >> H[i];
      H[i] += H[i - 1];
    }
    for (int i = 1; i <= M; i++) {
      cin >> W[i];
      W[i] += W[i - 1];
    }

    vector<ll> dph(N * H[N] + 1, 0);
    vector<ll> dpw(M * W[M] + 1, 0);

    ll ans = 0;

    for (int i = 0; i <= N; i++) {
      for (int j = i + 1; j <= N; j++) {
        dph[H[j] - H[i]]++;
      }
    }

    for (int k = 0; k <= M; k++) {
      for (int l = k + 1; l <= M; l++) {
        dpw[W[l] - W[k]]++;
      }
    }

    ll MIN_HW = min(H[N], W[M]);

    for (int i = 1; i <= MIN_HW; i++) {
      // cout << "i = " << i << endl;
      // cout << "dph = " << dph[i] << " dpw = " << dpw[i] << endl;
      ans += dph[i] * dpw[i];
    }

    /* ????¬? TLE
    for (int i = 0;i <= N;i++) {
            for(int j = i + 1; j <= N;j++){

                    for (int k = 0; k <= M;k++) {
                            for (int l = k + 1;l <= M;l++) {
                                    if (H[j] - H[i] == W[l] - W[k])ans++;
                            }
                    }

            }
    }
    */
    cout << ans << endl;
  }
}

#include "bits/stdc++.h"
#define ll long long
using namespace std;

ll dph[1500 * 1000 + 1];
ll dpw[1500 * 1000 + 1];

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N, M;
  while (true) {
    cin >> N >> M;
    if (N == 0 && M == 0)
      break;
    vector<ll> H(N + 1, 0), W(M + 1, 0);

    for (int i = 1; i <= 1500 * 1000; i++)
      dph[i] = dpw[i] = 0;

    for (int i = 1; i <= N; i++) {
      cin >> H[i];
      H[i] += H[i - 1];
    }
    for (int i = 1; i <= M; i++) {
      cin >> W[i];
      W[i] += W[i - 1];
    }
    ll ans = 0;

    for (int i = 0; i <= N; i++) {
      for (int j = i + 1; j <= N; j++) {
        dph[H[j] - H[i]]++;
      }
    }

    for (int k = 0; k <= M; k++) {
      for (int l = k + 1; l <= M; l++) {
        dpw[W[l] - W[k]]++;
      }
    }

    ll MIN_HW = min(H[N], W[M]);

    for (int i = 1; i <= MIN_HW; i++) {
      // cout << "i = " << i << endl;
      // cout << "dph = " << dph[i] << " dpw = " << dpw[i] << endl;
      ans += dph[i] * dpw[i];
    }

    /* ????¬? TLE
    for (int i = 0;i <= N;i++) {
            for(int j = i + 1; j <= N;j++){

                    for (int k = 0; k <= M;k++) {
                            for (int l = k + 1;l <= M;l++) {
                                    if (H[j] - H[i] == W[l] - W[k])ans++;
                            }
                    }

            }
    }
    */
    cout << ans << endl;
  }
}


#include <bits/stdc++.h>

#define loop(n, i) for (int i = 0; i < n; i++)
#define all(v) v.begin(), v.end()

using namespace std;
typedef long long ll;

int main() {
  while (1) {
    int n, m;
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    vector<int> h(n + 1), w(m + 1);
    loop(n, i) cin >> h[i + 1];
    loop(m, i) cin >> w[i + 1];

    loop(n, i) h[i + 1] += h[i];
    loop(m, i) w[i + 1] += w[i];

    multiset<int> H, W;
    loop(n + 1, i) loop(n + 1, j) {
      if (i >= j)
        continue;
      H.insert(h[j] - h[i]);
    }
    loop(m + 1, i) loop(m + 1, j) {
      if (i >= j)
        continue;
      W.insert(w[j] - w[i]);
    }

    int ans = 0;
    for (int v : H) {
      ans += W.count(v);
    }

    cout << ans << endl;
  }
  return 0;
}

#include <bits/stdc++.h>

#define loop(n, i) for (int i = 0; i < n; i++)
#define all(v) v.begin(), v.end()

using namespace std;
typedef long long ll;

int main() {
  while (1) {
    int n, m;
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    vector<int> h(n + 1), w(m + 1);
    loop(n, i) cin >> h[i + 1];
    loop(m, i) cin >> w[i + 1];

    loop(n, i) h[i + 1] += h[i];
    loop(m, i) w[i + 1] += w[i];

    vector<int> H, W;
    loop(n + 1, i) loop(n + 1, j) {
      if (i >= j)
        continue;
      H.push_back(h[j] - h[i]);
    }
    loop(m + 1, i) loop(m + 1, j) {
      if (i >= j)
        continue;
      W.push_back(w[j] - w[i]);
    }
    sort(all(W));

    int ans = 0;
    for (int v : H) {
      ans += distance(lower_bound(all(W), v), upper_bound(all(W), v));
    }

    cout << ans << endl;
  }
  return 0;
}


#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

vector<int> h, w;
vector<int> Sh, Sw;

void init() {
  h.resize(0);
  w.resize(0);
  Sh.resize(0);
  Sw.resize(0);
}

bool input() {
  int n, m;
  cin >> n >> m;
  if (n == 0 && m == 0) {
    return false;
  }
  for (int i = 0; i < n; i++) {
    int tmp;
    cin >> tmp;
    h.push_back(tmp);
  }
  for (int i = 0; i < m; i++) {
    int tmp;
    cin >> tmp;
    w.push_back(tmp);
  }
  return true;
}

int ans() {
  int res = 0;
  for (int i = 1; i < h.size() + 1; i++) {
    for (int j = 0; j < h.size() - i + 1; j++) {
      int tmp = 0;
      for (int k = 0; k < i && j + k < h.size(); k++) {
        tmp += h[j + k];
      }
      Sh.push_back(tmp);
    }
  }
  for (int i = 1; i < w.size() + 1; i++) {
    for (int j = 0; j < w.size() - i + 1; j++) {
      int tmp = 0;
      for (int k = 0; k < i && j + k < w.size(); k++) {
        tmp += w[j + k];
      }
      Sw.push_back(tmp);
    }
  }

  sort(Sw.begin(), Sw.end());
  sort(Sh.begin(), Sh.end());

  int i = 0, j = 0;

  while (i < Sw.size() && j < Sh.size()) {
    if (Sw[i] < Sh[j]) {
      i++;
      continue;
    } else if (Sw[i] > Sh[j]) {
      j++;
      continue;
    }
    int s_w = 1, s_h = 1;

    while (Sw[i] == Sw[i + 1]) {
      s_w++;
      i++;
    }
    while (Sh[j] == Sh[j + 1]) {
      s_h++;
      j++;
    }
    res += s_w * s_h;
    i++;
    j++;
  }
  return res;
}

int main() {
  while (init(), input()) {
    int a = ans();
    cout << a << endl;
  }
}

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

vector<int> h, w;
vector<int> Sh, Sw;

void init() {
  h.resize(0);
  w.resize(0);
  Sh.resize(0);
  Sw.resize(0);
}

bool input() {
  int n, m;
  cin >> n >> m;
  if (n == 0 && m == 0) {
    return false;
  }
  for (int i = 0; i < n; i++) {
    int tmp;
    cin >> tmp;
    h.push_back(tmp);
  }
  for (int i = 0; i < m; i++) {
    int tmp;
    cin >> tmp;
    w.push_back(tmp);
  }
  return true;
}

int ans() {
  int res = 0;
  for (int i = 0; i < h.size(); i++) {
    int tmp = 0;
    for (int j = i; j < h.size(); j++) {
      tmp += h[j];
      Sh.push_back(tmp);
    }
  }
  for (int i = 0; i < w.size(); i++) {
    int tmp = 0;
    for (int j = i; j < w.size(); j++) {
      tmp += w[j];
      Sw.push_back(tmp);
    }
  }

  sort(Sw.begin(), Sw.end());
  sort(Sh.begin(), Sh.end());

  int i = 0, j = 0;

  while (i < Sw.size() && j < Sh.size()) {
    if (Sw[i] < Sh[j]) {
      i++;
      continue;
    } else if (Sw[i] > Sh[j]) {
      j++;
      continue;
    }
    int s_w = 1, s_h = 1;

    while (Sw[i] == Sw[i + 1]) {
      s_w++;
      i++;
    }
    while (Sh[j] == Sh[j + 1]) {
      s_h++;
      j++;
    }
    res += s_w * s_h;
    i++;
    j++;
  }
  return res;
}

int main() {
  while (init(), input()) {
    int a = ans();
    cout << a << endl;
  }
}


#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define rep(i, n) for (int i = 0; i < n; i++)
#define ALL(T) T.begin(), T.end()
#define mp make_pair
#define pb push_back

using namespace std;

typedef pair<int, int> pii;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<string> vs;

const int INF = 1 << 24;

int main(void) {
  for (int n, m, res; cin >> n >> m, n; cout << res << endl) {
    res = 0;

    vi w(m), h(n);
    while (n--)
      cin >> h[n];
    while (m--)
      cin >> w[m];

    int curh, curw;
    rep(i, h.size()) {
      curh = 0;
      for (int j = i; j < h.size(); j++) {
        curh += h[j];
        rep(k, w.size()) {
          curw = 0;
          for (int l = k; l < w.size(); l++) {
            curw += w[l];
            if (curw == curh)
              res++;
          }
        }
      }
    }
  }

  return 0;
}

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
template <class T> inline T sqr(T x) { return x * x; }

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;

#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define pb push_back
#define mp make_pair
#define each(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define exist(s, e) ((s).find(e) != (s).end())
#define range(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) range(i, 0, n)
#define clr(a, b) memset((a), (b), sizeof(a))
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl;

const double eps = 1e-10;
const double pi = acos(-1.0);
const ll INF = 1LL << 62;
const int inf = 1 << 29;

int main(void) {
  for (int n, m, res; cin >> n >> m, n; cout << res << endl) {
    res = 0;
    map<int, int> sh;

    vi w(m), h(n);
    rep(i, n) cin >> h[i];
    rep(i, m) cin >> w[i];

    rep(i, n) {
      int sum = 0;
      range(j, i, n) {
        sum += h[j];
        sh[sum]++;
      }
    }

    rep(i, m) {
      int sum = 0;
      range(j, i, m) {
        sum += w[j];
        res += sh[sum];
      }
    }
  }

  return 0;
}


#include <algorithm>
#include <fstream>
#include <iostream>
#include <vector>

#define fin cin

using namespace std;

int n = 0, m = 0;
vector<int> h;
vector<int> w;

template <typename Vec> void push_sum(Vec &v, int size) {
  for (unsigned i = 0; i < size; i++) {
    int num = v[i];

    unsigned sub = i + 1;
    while (sub < size) {
      num += v[sub];
      v.push_back(num);
      sub++;
    }
  }
}

int main() {
  //    ifstream fin("in.txt");

  while (fin >> n >> m, n | m) {
    h.clear();
    w.clear();

    h.shrink_to_fit();
    w.shrink_to_fit();

    int tmp = 0;
    for (int i = 0; i < n; i++) {
      fin >> tmp;
      h.push_back(tmp);
    }
    for (int i = 0; i < m; i++) {
      fin >> tmp;
      w.push_back(tmp);
    }

    push_sum(h, n);
    push_sum(w, m);

    int ans = 0;
    int w_val[1024] = {};
    int h_val[1024] = {};

    // sort(h.begin(), h.end());
    // sort(w.begin(), w.end());

    for (auto itr = h.begin(); itr != h.end(); itr++) {
      h_val[*itr]++;
    }
    for (auto itr = w.begin(); itr != w.end(); itr++) {
      w_val[*itr]++;
    }

    for (int i = 0; i < 1001; i++)
      ans += h_val[i] * w_val[i];

    cout << ans << endl;
  }
  return 0;
}

#include <algorithm>
#include <fstream>
#include <iostream>
#include <vector>

#define fin cin

using namespace std;

int n = 0, m = 0;
vector<int> h;
vector<int> w;

template <typename Vec> void push_sum(Vec &v, int size) {
  for (unsigned i = 0; i < size; i++) {
    int num = v[i];

    unsigned sub = i + 1;
    while (sub < size) {
      num += v[sub];
      v.push_back(num);
      sub++;
    }
  }
}

int main() {
  //    ifstream fin("in.txt");

  while (fin >> n >> m, n | m) {

    h.clear();
    w.clear();

    h.shrink_to_fit();
    w.shrink_to_fit();

    int tmp = 0;
    for (int i = 0; i < n; i++) {
      fin >> tmp;
      h.push_back(tmp);
    }
    for (int i = 0; i < m; i++) {
      fin >> tmp;
      w.push_back(tmp);
    }

    push_sum(h, n);
    push_sum(w, m);

    int ans = 0;
    short h_val[1000 * 1500 + 1] = {};
    short w_val[1000 * 1500 + 1] = {};

    // sort(h.begin(), h.end());
    // sort(w.begin(), w.end());

    for (unsigned i = 0; i < h.size(); i++) {
      h_val[(h[i])]++;
    }
    for (unsigned i = 0; i < w.size(); i++) {
      w_val[(w[i])]++;
    }
    for (int i = 0; i < 1000 * 1500 + 1; i++) {
      ans += (h_val[i] * w_val[i]);
    }

    cout << ans << endl;
  }
  return 0;
}


#include <iostream>

using namespace std;

int h[1500], w[1500];
int n, m, sum, line_length, a;

void count(int n, int m) {
  if (n > 0) {
    for (int i = n - 1; i >= 0; i--) {
      line_length = h[n] - h[i];
      for (int j = 0; j <= m; j++) {
        for (int k = j + 1; k <= m; k++) {
          if ((w[k] - w[j]) == line_length) {
            sum += 1;
          }
        }
      }
    }
    count(n - 1, m);
  }
}

int solve(int n, int m) {
  sum = 0;
  count(n, m);
  return sum;
}

int main() {
  h[0] = 0;
  w[0] = 0;
  while (cin >> n >> m && n > 0) {
    for (int i = 1; i <= n; i++) {
      cin >> a;
      h[i] = h[i - 1] + a;
    }
    for (int i = 1; i <= m; i++) {
      cin >> a;
      w[i] = w[i - 1] + a;
    }
    cout << solve(n, m) << endl;
  }

  return 0;
}


#include <algorithm>
#include <iostream>
#include <math.h>
#include <string>
#include <utility>
using namespace std;
const int MAX_LEN = 10000000;
int N, M;
int h[MAX_LEN], w[MAX_LEN];
int x, y;
int solve() {
  int sum = 0;
  int k = N;
  int l = M;
  for (int i = 0; i < max(N, M); i++) {
    if (i < N)
      h[k] = h[i];
    if (i < M)
      w[l] = w[i];
    for (int j = i + 1; j < max(N, M); j++) {
      if (i < N) {
        h[k] += h[j];
        h[k + 1] = h[k];
        k++;
      }
      if (i < M) {
        w[l] += w[j];
        w[l + 1] = w[l];
        l++;
      }
    }
  }
  sort(h, h + k);
  sort(w, w + l);
  for (int i = 0; i < k; i++)
    for (int j = 0; j < l && j <= i; j++)
      if (w[j] == h[i])
        sum++;
  return sum;
}

int max(int a, int b) { return a > b ? a : b; }
int main() {
  while (cin >> N >> M && N != 0) {
    for (int i = 0; i < N; i++) {
      cin >> h[i];
    }
    for (int i = 0; i < M; i++) {
      cin >> w[i];
    }
    cout << solve() << endl;
  }
  return 0;
}

#include <iostream>
#include <vector>

using namespace std;

int main() {
  int h[1501], w[1501];

  int N, M;
  while (cin >> N >> M, (N || M)) {
    for (int i = 0; i < N; i++) {
      int t;
      cin >> t;
      h[i] = t;
    }
    for (int i = 0; i < M; i++) {
      int t;
      cin >> t;
      w[i] = t;
    }

    vector<int> V(1500001), H(1500001);
    for (int i = 0; i < N; i++) {
      int len = 0;
      for (int j = i; j < N; j++) {
        len += h[j];
        V[len]++;
      }
    }
    for (int i = 0; i < M; i++) {
      int len = 0;
      for (int j = i; j < M; j++) {
        len += w[j];
        H[len]++;
      }
    }

    int count = 0;
    for (int i = 1; i <= 1500000; i++) {
      count += V[i] * H[i];
    }
    cout << count << endl;
  }

  return 0;
}


#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef vector<int> vi;
vi ans, ans2;
const int inf = 1e9;
int polnum[205];
int dp[1000005];
int odddp[1000005];
int main() {
  int n, m;
  while (1) {
    cin >> n >> m;
    if (n == 0)
      break;
    int tmph, tmpw;
    vi h(n + 1);
    vi w(m + 1);
    h[0] = 0;
    w[0] = 0;
    int count = 0;
    int lw, rw, uh, dh;
    for (int i = 1; i <= n; i++) {
      cin >> tmph;
      h[i] += h[i - 1] + tmph;
    }
    for (int j = 1; j <= m; j++) {
      cin >> tmpw;
      w[j] += w[j - 1] + tmpw;
    }
    for (int i = 0; i <= n; i++) {
      for (int j = i + 1; j <= n; j++) {
        uh = h[i];
        dh = h[j];
        for (int k = 0; k <= m; k++) {
          for (int l = k + 1; l <= m; l++) {
            lw = w[k];
            rw = w[l];
            if (dh - uh == rw - lw)
              count++;
          }
        }
      }
    }
    cout << count << endl;
  }
  return 0;
}

#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef vector<int> vi;
int main() {
  int n, m;
  while (1) {
    cin >> n >> m;
    if (n == 0)
      break;
    vi h(n + 1);
    vi w(m + 1);
    int totalh = 0, totalw = 0;
    int count = 0;
    for (int i = 0; i < n; i++) {
      cin >> h[i];
      totalh += h[i];
    }
    for (int i = 0; i < m; i++) {
      cin >> w[i];
      totalw += w[i];
    }
    vi htype(totalh + 1);
    vi wtype(totalw + 1);
    for (int i = 0; i < n; i++) {
      int height = h[i];
      htype[height]++;
      for (int j = i + 1; j < n; j++) {
        height += h[j];
        htype[height]++;
      }
    }
    for (int i = 0; i < m; i++) {
      int width = w[i];
      wtype[width]++;
      for (int j = i + 1; j < m; j++) {
        width += w[j];
        wtype[width]++;
      }
    }
    for (int i = 1; i <= min(totalh, totalw); i++) {
      count += htype[i] * wtype[i];
    }
    cout << count << endl;
  }
  return 0;
}


#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main() {

  int w, h;
  cin >> w >> h;
  int wl[1501] = {}, hl[1501] = {};
  int wb, hb;
  int i, j, k;
  while (w != 0) {
    for (i = 0; i < w; i++) {
      cin >> wb;
      wl[i + 1] = wl[i] + wb;
    }
    for (i = 0; i < h; i++) {
      cin >> hb;
      hl[i + 1] = hl[i] + hb;
    }
    vector<int> ws, hs;

    for (i = 0; i <= w; i++) {
      for (j = i + 1; j <= w; j++) {
        ws.push_back(wl[j] - wl[i]);
      }
    }
    for (i = 0; i <= h; i++) {
      for (j = i + 1; j <= h; j++) {
        hs.push_back(hl[j] - hl[i]);
      }
    }

    sort(ws.begin(), ws.end());
    sort(hs.begin(), hs.end());
    /*
    for(i=0;i<ws.size();i++) cout << ws[i] << ",";
    cout << endl;
    for(i=0;i<hs.size();i++) cout << hs[i] << ",";
    cout << endl;
    */
    int wc = 0, hc = 0;
    int x, y;
    int o = 0;
    while (!(wc + 1 >= ws.size() && hc + 1 >= hs.size())) {

      if (ws[wc] == hs[hc]) {
        x = 0;
        y = 0;
        while (ws[wc + x] == ws[wc] && wc + x < ws.size())
          x++;
        while (hs[hc + y] == hs[hc] && hc + y < hs.size())
          y++;
        // cout << x*y << endl;
        o += x * y;
        wc += x;
        hc += y;
      } else if (ws[wc] > hs[hc]) {
        while (ws[wc] > hs[hc] && hc < hs.size())
          hc++;
      } else {
        while (ws[wc] < hs[hc] && wc < ws.size())
          wc++;
      }
      // cout << wc<<":"<<hc<<endl;
    }
    cout << o << endl;

    cin >> w >> h;
  }
  return 0;
}

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main() {

  int w, h;
  cin >> w >> h;
  int wl[1501] = {}, hl[1501] = {};
  int wb, hb;
  int i, j, k;
  while (w != 0) {
    for (i = 0; i < w; i++) {
      cin >> wb;
      wl[i + 1] = wl[i] + wb;
    }
    for (i = 0; i < h; i++) {
      cin >> hb;
      hl[i + 1] = hl[i] + hb;
    }
    vector<int> ws, hs;

    for (i = 0; i <= w; i++) {
      for (j = i + 1; j <= w; j++) {
        ws.push_back(wl[j] - wl[i]);
      }
    }
    for (i = 0; i <= h; i++) {
      for (j = i + 1; j <= h; j++) {
        hs.push_back(hl[j] - hl[i]);
      }
    }

    sort(ws.begin(), ws.end());
    sort(hs.begin(), hs.end());
    /*
    for(i=0;i<ws.size();i++) cout << ws[i] << ",";
    cout << endl;
    for(i=0;i<hs.size();i++) cout << hs[i] << ",";
    cout << endl;
    */
    int wc = 0, hc = 0;
    int x, y;
    int o = 0;
    while (!(wc >= ws.size() || hc >= hs.size())) {

      if (ws[wc] == hs[hc]) {
        x = 0;
        y = 0;
        while (ws[wc + x] == ws[wc] && wc + x < ws.size())
          x++;
        while (hs[hc + y] == hs[hc] && hc + y < hs.size())
          y++;
        // cout << x*y << endl;
        o += x * y;
        wc += x;
        hc += y;
      } else if (ws[wc] > hs[hc]) {
        while (ws[wc] > hs[hc] && hc < hs.size())
          hc++;
      } else {
        while (ws[wc] < hs[hc] && wc < ws.size())
          wc++;
      }
      // cout << wc<<":"<<hc<<endl;
    }
    cout << o << endl;

    cin >> w >> h;
  }
  return 0;
}


#include <bits/stdc++.h>
#define REP(i, n, N) for (int i = n; i < N; i++)
#define p(S) cout << S << endl
using namespace std;

int main() {
  int N, M;
  while (cin >> N >> M && N) {
    int h[1501], w[1501];
    map<int, int> allh, allw;
    int ans = 0, k;
    REP(i, 0, N) {
      cin >> h[i];
      k = 0;
      for (int j = i; j >= 0; j--) {
        k += h[j];
        allh[k]++;
      }
    }
    REP(i, 0, M) {
      cin >> w[i];
      k = 0;
      for (int j = i; j >= 0; j--) {
        k += w[j];
        allw[k]++;
      }
    }
    REP(i, 0, 1500000) { ans += allh[i] * allw[i]; }
    p(ans);
  }

  return 0;
}

#include <bits/stdc++.h>
#define REP(i, n, N) for (int i = n; i < N; i++)
#define p(S) cout << S << endl
using namespace std;
int allh[1500010], allw[1500010];
int main() {
  int N, M;
  while (cin >> N >> M && N) {
    int h[1501], w[1501];
    REP(i, 0, 1500010) allh[i] = allw[i] = 0;
    int ans = 0, k;
    REP(i, 0, N) {
      cin >> h[i];
      k = 0;
      for (int j = i; j >= 0; j--) {
        k += h[j];
        allh[k]++;
      }
    }
    REP(i, 0, M) {
      cin >> w[i];
      k = 0;
      for (int j = i; j >= 0; j--) {
        k += w[j];
        allw[k]++;
      }
    }
    REP(i, 0, 1500010) { ans += allh[i] * allw[i]; }
    p(ans);
  }

  return 0;
}


// include
//------------------------------------------
#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

// typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

// container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())

// repetition
//------------------------------------------
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

// constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI = acos(-1.0);

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  int N, M;
  while (cin >> N >> M, N) {
    VI h(N + 1, 0), w(M + 1, 0);
    REP(i, N) {
      int x;
      cin >> x;
      h[i + 1] = h[i] + x;
    }
    REP(i, M) {
      int x;
      cin >> x;
      w[i + 1] = w[i] + x;
    }
    int ans = 0;
    if (N < M) {
      for (int i = 0; i <= N; ++i)
        for (int j = 0; j < i; ++j) {
          int sz = h[i] - h[j], s = 0, e = 0;
          for (; e <= M;) {
            int tmp = w[e] - w[s];
            if (tmp == sz) {
              ++ans;
              ++e, ++s;
            } else if (tmp < sz) {
              ++e;
            } else {
              ++s;
            }
          }
        }
    } else {
      for (int i = 0; i <= M; ++i)
        for (int j = 0; j < i; ++j) {
          int sz = w[i] - w[j], s = 0, e = 0;
          for (; e <= N;) {
            int tmp = h[e] - h[s];
            if (tmp == sz) {
              ++ans;
              ++e, ++s;
            } else if (tmp < sz) {
              ++e;
            } else {
              ++s;
            }
          }
        }
    }
    cout << ans << endl;
  }

  return 0;
}

// include
//------------------------------------------
#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

// typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

// container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())

// repetition
//------------------------------------------
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

// constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI = acos(-1.0);

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  int N, M;
  while (cin >> N >> M, N) {
    VI h(N + 1, 0), w(M + 1, 0);
    REP(i, N) {
      int x;
      cin >> x;
      h[i + 1] = h[i] + x;
    }
    REP(i, M) {
      int x;
      cin >> x;
      w[i + 1] = w[i] + x;
    }
    LL ans = 0;
    map<int, int> szx, szy;
    for (int i = 0; i <= M; ++i)
      for (int j = 0; j < i; ++j)
        szx[w[i] - w[j]]++;

    for (int i = 0; i <= N; ++i)
      for (int j = 0; j < i; ++j) {
        ans += szx[h[i] - h[j]];
      }

    cout << ans << endl;
  }

  return 0;
}


#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

int main() {

  int N, M;
  int a;

  while (1) {
    vector<int> v1;
    vector<int> v2;
    vector<int> vh;
    vector<int> vw;
    int count = 0;
    int k = 0;
    int check = 0;
    int l = 0;

    cin >> N >> M;

    if (N == 0 && M == 0) {
      break;
    }

    for (int i = 0; i < N; ++i) {
      cin >> a;
      v1.push_back(a);
    }

    for (int i = 0; i < M; ++i) {
      cin >> a;
      v2.push_back(a);
    }

    for (int i = 0; i < N; ++i) {
      int temp1 = 0;

      for (int j = i; j < N; ++j) {
        temp1 += v1[j];
        vh.push_back(temp1);
      }
    }

    for (int i = 0; i < M; ++i) {
      int temp2 = 0;

      for (int j = i; j < M; ++j) {
        temp2 += v2[j];
        vw.push_back(temp2);
        ++l;
      }
    }

    sort(vh.begin(), vh.end());
    sort(vw.begin(), vw.end());

    for (auto e : vh) {
      while (1) {
        if (e == vw[k]) {
          ++count;
        } else if (e < vw[k]) {
          break;
        } else {
          check = k + 1;
        }

        ++k;

        if (k == l) {
          break;
        }
      }
      k = check;
    }
    cout << count << endl;
  }

  return 0;
}

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

int main() {

  int N, M;
  int a;

  while (1) {
    vector<int> v1;
    vector<int> v2;
    vector<int> vh;
    vector<int> vw;
    int count = 0;
    int k = 0;
    int check = 0;
    int l = 0;

    cin >> N >> M;

    if (N == 0 && M == 0) {
      break;
    }

    for (int i = 0; i < N; ++i) {
      cin >> a;
      v1.push_back(a);
    }

    for (int i = 0; i < M; ++i) {
      cin >> a;
      v2.push_back(a);
    }

    for (int i = 0; i < N; ++i) {
      int temp1 = 0;

      for (int j = i; j < N; ++j) {
        temp1 += v1[j];
        vh.push_back(temp1);
      }
    }

    for (int i = 0; i < M; ++i) {
      int temp2 = 0;

      for (int j = i; j < M; ++j) {
        temp2 += v2[j];
        vw.push_back(temp2);
        ++l;
      }
    }

    sort(vh.begin(), vh.end());
    sort(vw.begin(), vw.end());

    for (auto e : vh) {
      while (1) {
        if (k == l) {
          break;
        }
        if (e == vw[k]) {
          ++count;
        } else if (e < vw[k]) {
          break;
        } else {
          check = k + 1;
        }
        ++k;
      }
      k = check;
    }
    cout << count << endl;
  }

  return 0;
}


#include <iostream>
#include <vector>

using namespace std;

int main() {
  int n, m;
  while (cin >> n >> m && n > 0) {
    vector<int> h(n);
    vector<int> w(m);
    for (int i = 0; i < n; i++)
      cin >> h[i];
    for (int i = 0; i < m; i++)
      cin >> w[i];
    int ans = 0;
    int x, y;
    for (int i = 0; i < n; i++) {
      for (int j = i; j < n; j++) {
        // ?§??????¨??????????±???????
        x = 0;
        for (int k = i; k <= j; k++) {
          x += h[k];
        }

        for (int l = 0; l < m; l++) {
          y = 0;
          for (int p = l; p < m; p++) {
            y += w[p];
            if (y == x) {
              ans++;
              break;
            }
          }
        }
      }
    }
    cout << ans << endl;
  }
}


#include <iostream>

using namespace std;
int N, M;
int h[1501], w[1501];
int e;
int ans = 0;

int main() {
  while (1) {
    cin >> N;
    cin >> M;
    if (N == 0 && M == 0)
      break;
    for (int i = 1; i <= N; i++) {
      cin >> h[i];
      h[i] += h[i - 1];
    }
    for (int i = 1; i <= M; i++) {
      cin >> w[i];
      w[i] += w[i - 1];
    }
    h[0] = 0;
    w[0] = 0;
    ans = 0;
    for (int i = 0; i <= N; i++) {
      for (int j = 0; j <= M; j++) {
        for (int k = i + 1; k <= N; k++) {
          int edgh = h[k] - h[i];
          for (int l = j + 1; l <= M; l++) {
            if (edgh == w[l] - w[j])
              ans++;
            if (edgh <= w[l] - w[j])
              break;
          }
        }
      }
    }
    cout << ans << endl;
  }
}


#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

int main() {
  int N, M;
  vector<int> HH, WW;
  while (cin >> N >> M, N | M) {
    int H[N + 1];
    H[0] = 0;
    for (int i = 1; i <= N; i++) {
      cin >> H[i];
      H[i] += H[i - 1];
    }
    HH.clear();
    for (int i = 0; i <= N; i++) {
      for (int j = i + 1; j <= N; j++) {
        HH.push_back(H[j] - H[i]);
      }
    }
    int W[M + 1];
    W[0] = 0;
    for (int i = 1; i <= M; i++) {
      cin >> W[i];
      W[i] += W[i - 1];
    }
    WW.clear();
    for (int i = 0; i <= M; i++) {
      for (int j = i + 1; j <= M; j++) {
        WW.push_back(W[j] - W[i]);
      }
    }
    int c = 0;
    for (auto &h : HH) {
      for (auto &w : WW) {
        if (h == w)
          c++;
      }
    }
    cout << c << endl;
  }
}

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

int main() {
  int N, M;
  vector<int> HH, WW;
  while (cin >> N >> M, N | M) {
    int H[N + 1];
    H[0] = 0;
    for (int i = 1; i <= N; i++) {
      cin >> H[i];
      H[i] += H[i - 1];
    }
    HH.clear();
    for (int i = 0; i <= N; i++) {
      for (int j = i + 1; j <= N; j++) {
        HH.push_back(H[j] - H[i]);
      }
    }
    int W[M + 1];
    W[0] = 0;
    for (int i = 1; i <= M; i++) {
      cin >> W[i];
      W[i] += W[i - 1];
    }
    WW.clear();
    for (int i = 0; i <= M; i++) {
      for (int j = i + 1; j <= M; j++) {
        WW.push_back(W[j] - W[i]);
      }
    }
    sort(HH.begin(), HH.end());
    sort(WW.begin(), WW.end());
    int c = 0;
    for (auto &h : HH) {
      auto lo = lower_bound(WW.begin(), WW.end(), h);
      auto up = upper_bound(WW.begin(), WW.end(), h);
      for (auto i = lo; i != up; ++i) {
        if (h == *i)
          c++;
      }
    }
    cout << c << endl;
  }
}


#include <iostream>
#include <map>
#include <set>
#include <vector>
using namespace std;
const int MAX = 1500000;
int check(vector<int> h, vector<int> w, int x, int y, int b) {
  if (h[y] && w[x] && h[y + b] && w[x + b])
    return 1;
  return 0;
}
int main() {
  int n, m;
  while (cin >> n >> m, n || m) {
    vector<int> h(n + 1, 0);
    vector<int> w(n + 1, 0);
    h[0] = 0;
    w[0] = 0;
    for (int i = 1; i <= n; i++) {
      int tmp;
      cin >> tmp;
      h[i] = h[i - 1] + tmp;
    }
    for (int i = 1; i <= m; i++) {
      int tmp;
      cin >> tmp;
      w[i] = w[i - 1] + tmp;
    }
    map<int, int> cnt;
    for (int i = 0; i <= n; i++) {
      for (int j = 0; j <= m; j++) {
        cnt[h[i] - w[j]]++;
      }
    }
    int ans = 0;
    map<int, int>::iterator it = cnt.begin();
    while (it != cnt.end()) {
      ans += (*it).second * ((*it).second - 1) / 2;
      it++;
    }
    cout << ans << endl;
  }
  return 0;
}

#include <iostream>
#include <map>
#include <set>
#include <vector>
using namespace std;
const int MAX = 1500000;
int check(vector<int> h, vector<int> w, int x, int y, int b) {
  if (h[y] && w[x] && h[y + b] && w[x + b])
    return 1;
  return 0;
}
int main() {
  int n, m;
  while (cin >> n >> m, n || m) {
    vector<int> h(n + 1, 0);
    vector<int> w(m + 1, 0);
    h[0] = 0;
    w[0] = 0;
    for (int i = 1; i <= n; i++) {
      int tmp;
      cin >> tmp;
      h[i] = h[i - 1] + tmp;
    }
    for (int i = 1; i <= m; i++) {
      int tmp;
      cin >> tmp;
      w[i] = w[i - 1] + tmp;
    }
    map<int, int> cnt;
    for (int i = 0; i <= n; i++) {
      for (int j = 0; j <= m; j++) {
        cnt[h[i] - w[j]]++;
      }
    }
    int ans = 0;
    map<int, int>::iterator it = cnt.begin();
    while (it != cnt.end()) {
      ans += (*it).second * ((*it).second - 1) / 2;
      it++;
    }
    cout << ans << endl;
  }
  return 0;
}


#include <iostream>
#include <vector>
#define MAX 100000
using namespace std;

int main() {
  int N, M;
  vector<int> w, h;
  int p = 0;
  int x[MAX] = {0};
  while ((cin >> N >> M) && N) {
    long long tt = 0;
    w.resize(M);
    h.resize(N);
    for (int i = 0; i < N; i++) {
      cin >> h[i];
    }

    for (int j = 0; j < M; j++) {
      cin >> w[j];
    }
    for (int i = 0; i < N; i++) {
      p = 0;
      for (int j = i; j < N; j++) {
        p += h[j];
        x[p]++;
      }
    }
    for (int i = 0; i < M; i++) {
      p = 0;
      for (int j = i; j < M; j++) {
        p += w[j];
        tt += x[p];
      }
    }
    cout << tt << endl;
  }
  return 0;
}

#include <iostream>
#include <vector>
#define MAX 1000000
#include <bits/stdc++.h>
using namespace std;

int main() {
  int N, M;
  vector<int> w, h;
  int p = 0;
  // int x[MAX]={0};
  while ((cin >> N >> M) && N) {
    int tt = 0;
    w.resize(M);
    h.resize(N);
    for (int i = 0; i < N; i++) {
      cin >> h[i];
    }

    for (int j = 0; j < M; j++) {
      cin >> w[j];
    }
    map<int, int> x;
    for (int i = 0; i < N; i++) {
      p = 0;
      for (int j = i; j < N; j++) {
        p += h[j];
        x[p]++;
      }
    }
    for (int i = 0; i < M; i++) {
      p = 0;
      for (int j = i; j < M; j++) {
        p += w[j];
        tt += x[p];
      }
    }
    cout << tt << endl;
  }
  return 0;
}


#include <iostream>
#include <vector>
#define MAX 100000
using namespace std;

int main() {
  int N, M;
  vector<int> w, h;
  int p = 0;
  int x[MAX] = {MAX};
  while ((cin >> N) && (cin >> M)) {
    int tt = 0;
    w.resize(M);
    h.resize(N);
    for (int i = 0; i < N; i++) {
      cin >> h[i];
    }

    for (int j = 0; j < M; j++) {
      cin >> w[j];
    }

    for (int i = 0; i < N; i++) {
      p = 0;
      for (int j = i; j < N; j++) {
        p += h[j];
        x[p]++;
      }
    }
    for (int i = 0; i < M; i++) {
      p = 0;
      for (int j = i; j < M; j++) {
        p += w[j];
        tt += x[p];
      }
    }
    cout << tt << endl;
  }
  return 0;
}

#include <bits/stdc++.h>
using namespace std;

int main() {
  int N, M, h[1500], w[1500];

  while (cin >> N >> M, N) {
    for (int i = 0; i < N; i++) {
      cin >> h[i];
    }
    for (int i = 0; i < M; i++) {
      cin >> w[i];
    }

    map<int, int> foo, piyo;
    for (int i = 0; i < N; i++) {
      int sum = 0;
      for (int j = i; j < N; j++) {
        sum += h[j];
        foo[sum]++;
      }
    }
    for (int i = 0; i < M; i++) {
      int sum = 0;
      for (int j = i; j < M; j++) {
        sum += w[j];
        piyo[sum]++;
      }
    }

    int cnt = 0;
    for (map<int, int>::iterator it = foo.begin(); it != foo.end(); it++) {
      cnt += it->second * piyo[it->first];
    }
    cout << cnt << endl;
  }
}


#include <stdio.h>
#include <string.h>

int main(void) {
  int sw[150001], sum, n, m, i, j, k, count, w[1500], h[1500];
  while (1) {
    scanf("%d%d", &n, &m);
    if (n == 0 && m == 0)
      break;
    for (i = 0; i < n; i++)
      scanf("%d", &h[i]);
    for (i = 0; i < m; i++)
      scanf("%d", &w[i]);
    memset(sw, 0, sizeof(sw));
    for (i = 0; i < m; i++) {
      for (j = i; j < m; j++) {
        sum = 0;
        for (k = i; k <= j; k++)
          sum += w[k];
        sw[sum]++;
      }
    }
    count = 0;
    for (i = 0; i < n; i++) {
      for (j = i; j < n; j++) {
        sum = 0;
        for (k = i; k <= j; k++)
          sum += h[k];
        if (sw[sum])
          count += sw[sum];
      }
    }
    printf("%d\\n", count);
  }
  return 0;
}

#include <stdio.h>
#include <string.h>

int main(void) {
  int sw[1500001], sum, n, m, i, j, k, count, w[1500], h[1500];
  while (1) {
    scanf("%d%d", &n, &m);
    if (n == 0 && m == 0)
      break;
    for (i = 0; i < n; i++)
      scanf("%d", &h[i]);
    for (i = 0; i < m; i++)
      scanf("%d", &w[i]);
    memset(sw, 0, sizeof(sw));
    for (i = 0; i < m; i++) {
      for (j = i; j < m; j++) {
        sum = 0;
        for (k = i; k <= j; k++)
          sum += w[k];
        sw[sum]++;
      }
    }
    count = 0;
    for (i = 0; i < n; i++) {
      for (j = i; j < n; j++) {
        sum = 0;
        for (k = i; k <= j; k++)
          sum += h[k];
        if (sw[sum])
          count += sw[sum];
      }
    }
    printf("%d\\n", count);
  }
  return 0;
}


#include <iostream>

using namespace std;

int main() {

  while (1) {
    int N, M;
    cin >> N >> M;
    if (!(N || M))
      break;

    int h[N], w[M];
    for (int i = 0; i < N; i++)
      cin >> h[i];
    for (int i = 0; i < M; i++)
      cin >> w[i];

    int x[M + 1], y[N + 1];
    x[0] = y[0] = 0;
    for (int i = 0; i < M; i++) {
      x[i + 1] = x[i] + w[i];
    }
    for (int i = 0; i < N; i++) {
      y[i + 1] = y[i] + h[i];
    }
    int s = 0;
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < M; j++) {
        int d = i + 1;
        int r = j + 1;
        while (d <= N && r <= M) {
          if (y[d] - y[i] == x[r] - x[j]) {
            s++;
            d++;
            r++;
          } else if (y[d] - y[i] > x[r] - x[j]) {
            r++;
          } else if (y[d] - y[i] < x[r] - x[j]) {
            d++;
          }
        }
      }
    }

    cout << s << endl;
  }

  return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main() {
  int n, m;
  int w[1500], h[1500];
  cin >> n >> m;
  for (int i = 0; i < n; i++)
    scanf("%d", &w[i]);
  for (int i = 0; i < m; i++)
    scanf("%d", &h[i]);
  while (n) {
    vector<int> x;
    vector<int> y;
    for (int i = 0; i < n; i++) {
      int a = 0;
      for (int j = i; j < n; j++) {
        a += w[j];
        x[a]++;
      }
    }
    for (int i = 0; i < m; i++) {
      int b = 0;
      for (int j = i; j < m; j++) {
        b += h[j];
        y[b]++;
      }
    }

    int ave = 0;
    for (int i = 0; i < 1500 * 1000; i++) {
      // if(y[i]!=0)cout << i << " " << y[i] << " / ";
      ave += x[i] * y[i];
    }
    cout << ave << endl;

    cin >> n >> m;
    for (int i = 0; i < n; i++)
      scanf("%d", &w[i]);
    for (int i = 0; i < m; i++)
      scanf("%d", &h[i]);
  }
  return 0;
}


#include <algorithm>
#include <cstdio>

#define N 1500

static int my[N + 1][N + 1];

using namespace std;

/** Application main entry point. */
int main(int argc, char *argv[]) {
  int i;

  for (;;) {
    int px[N + 1], py[N + 1];
    /** Allocated in Heap Memory Area:
     * int my[ N + 1 ][ N + 1 ]; */
    int x1, x2, y1, y2;
    int res = 0;
    int n, m;

    scanf("%d%d", &n, &m);
    if (!(n | m))
      break;

    px[0] = 0;
    py[0] = 0;
    for (i = 1; i <= n; ++i) {
      scanf("%d", px + i);
      px[i] += px[i - 1];
    }
    for (i = 1; i <= m; ++i) {
      scanf("%d", py + i);
      py[i] += py[i - 1];
    }

    for (y1 = 0; y1 <= m; ++y1)
      for (y2 = y1 + 1; y2 <= m; ++y2) {
        my[y1][y2] = py[y2] - py[y1];
      }
    for (x1 = 0; x1 <= n; ++x1)
      for (x2 = x1 + 1; x2 <= n; ++x2) {
        const int dx = px[x2] - px[x1];

        for (y1 = 0; y1 <= m; ++y1) {
          res += !!(binary_search(&my[y1][y1 + 1], &my[y1][m + 1], dx));
        }
      }

    printf("%d\\n", res);
  }

  return (0);
}

#include <algorithm>
#include <cstdio>

#define N 1500

static int my[N * N];

using namespace std;

/** Application main entry point. */
int main(int argc, char *argv[]) {
  int i;

  for (;;) {
    int px[N + 1], py[N + 1];
    /** Allocated in Heap Memory Area:
     * int my[ N * N ]; */
    int x1, x2, y1, y2;
    int res = 0;
    int n, m;

    scanf("%d%d", &n, &m);
    if (!(n | m))
      break;

    px[0] = 0;
    py[0] = 0;
    for (i = 1; i <= n; ++i) {
      scanf("%d", px + i);
      px[i] += px[i - 1];
    }
    for (i = 1; i <= m; ++i) {
      scanf("%d", py + i);
      py[i] += py[i - 1];
    }

    i = 0;
    for (y1 = 0; y1 <= m; ++y1)
      for (y2 = y1 + 1; y2 <= m; ++y2) {
        my[i++] = py[y2] - py[y1];
      }
    sort(my, my + i);

    for (x1 = 0; x1 <= n; ++x1)
      for (x2 = x1 + 1; x2 <= n; ++x2) {
        const int dx = px[x2] - px[x1];

        res += upper_bound(my, my + i, dx) - lower_bound(my, my + i, dx);
      }

    printf("%d\\n", res);
  }

  return (0);
}


#include <iostream>
#include <vector>

#define LIM 2000000

using namespace std;

int main() {

  int n, m, h[2000], w[2000];

  while (cin >> n >> m, (n || m)) {

    for (int i = 0; i < n; ++i) {
      cin >> h[i];
    }

    for (int i = 0; i < m; ++i) {
      cin >> w[i];
    }

    int res = 0;

    for (int p = 0; p < n; ++p) {

      int hsum = 0;

      for (int q = p; q < n; ++q) {

        hsum += h[q];

        for (int r = 0; r < m; ++r) {

          int wsum = 0;

          for (int s = r; s < m; ++s) {

            wsum += w[s];

            if (hsum == wsum)
              res++;
          }
        }
      }
    }

    cout << res << endl;
  }

  return 0;
}

#include <algorithm>
#include <iostream>
#include <vector>

#define LIM 2000000

using namespace std;

int main() {

  int n, m, h[2000], w[2000];

  while (cin >> n >> m, (n || m)) {

    for (int i = 0; i < n; ++i) {
      cin >> h[i];
    }

    for (int i = 0; i < m; ++i) {
      cin >> w[i];
    }

    int res = 0;
    vector<int> hlen, wlen;

    for (int p = 0; p < n; ++p) {

      int hsum = 0;

      for (int q = p; q < n; ++q) {

        hsum += h[q];
        hlen.push_back(hsum);
      }
    }

    for (int r = 0; r < m; ++r) {

      int wsum = 0;

      for (int s = r; s < m; ++s) {

        wsum += w[s];
        wlen.push_back(wsum);
      }
    }

    sort(hlen.begin(), hlen.end());
    sort(wlen.begin(), wlen.end());

    for (int i = 0; i < hlen.size(); ++i) {

      vector<int>::iterator it1 =
                                lower_bound(wlen.begin(), wlen.end(), hlen[i]),
                            it2 =
                                upper_bound(wlen.begin(), wlen.end(), hlen[i]);

      res += it2 - it1;
    }

    cout << res << endl;
  }

  return 0;
}


// c
#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define REP(i, b, n) for (int i = b; i < n; i++)
#define REPR(i, b, n) for (int i = n - 1; i >= b; i--)
#define CLR(mat) memset(mat, 0, sizeof(mat))
#define NCLR(mat) memset(mat, -1, sizeof(mat))
#define ALL(c) (c).begin(), (c).end()
#define RALL(c) (c).rbegin(), (c).rend()
#define EACH(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define BIT(n, b) ((n >> b) & 1)
#define PB push_back
#define MP make_pair

using namespace std;

static const double PI = acos(-1.0);
static const double EPS = 1e-9;
typedef long long ll;
typedef pair<int, int> pii;

int n, m;
int h[3000], w[3000];

int main() {

  while (cin >> n >> m, (n || m)) {

    multiset<int> st;
    int res = 0;

    for (int i = 0; i < n; ++i) {
      cin >> h[i];
    }

    for (int i = 0; i < m; ++i) {
      cin >> w[i];
    }

    for (int i = 0; i < n; ++i) {

      int d = 0;

      for (int j = i; j < n; ++j) {
        st.insert(d += h[j]);
      }
    }

    for (int i = 0; i < m; ++i) {

      int d = 0;

      for (int j = i; j < m; ++j) {
        res += st.count(d += w[j]);
      }
    }

    cout << res << endl;
  }

  return 0;
}

// c
#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define REP(i, b, n) for (int i = b; i < n; i++)
#define REPR(i, b, n) for (int i = n - 1; i >= b; i--)
#define CLR(mat) memset(mat, 0, sizeof(mat))
#define NCLR(mat) memset(mat, -1, sizeof(mat))
#define ALL(c) (c).begin(), (c).end()
#define RALL(c) (c).rbegin(), (c).rend()
#define EACH(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define BIT(n, b) ((n >> b) & 1)
#define PB push_back
#define MP make_pair

using namespace std;

static const double PI = acos(-1.0);
static const double EPS = 1e-9;
typedef long long ll;
typedef pair<int, int> pii;

int n, m;
int h[3000], w[3000];

int main() {

  while (cin >> n >> m, (n || m)) {

    vector<int> hsum, wsum;
    int res = 0;

    for (int i = 0; i < n; ++i) {
      cin >> h[i];
    }

    for (int i = 0; i < m; ++i) {
      cin >> w[i];
    }

    for (int i = 0; i < n; ++i) {

      int d = 0;

      for (int j = i; j < n; ++j) {
        hsum.push_back(d += h[j]);
      }
    }

    sort(hsum.begin(), hsum.end());

    for (int i = 0; i < m; ++i) {

      int d = 0;

      for (int j = i; j < m; ++j) {

        d += w[j];

        res += (int)(upper_bound(hsum.begin(), hsum.end(), d) -
                     lower_bound(hsum.begin(), hsum.end(), d));
      }
    }

    cout << res << endl;
  }

  return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define iota(i, n, b, s)                                                       \\
  for (int i = int(b); i != int((b) + (s) * (n)); i += (s))
#define range(i, n, m)                                                         \\
  iota(i, (((n) > (m)) ? ((n) - (m)) : ((m) - (n))), (n), ((n) > (m) ? -1 : 1))
#define rep(i, n) iota(i, (n), 0, 1)
#define loop for (;;)

#define INF (1e9)
#define EPS (1e-9)
#define cons(a, b) (make_pair(a, b))
#define car(a) (a.first)
#define cdr(a) (a.second)
#define cadr(a) (car(cdr(a)))
#define cddr(a) (cdr(cdr(a)))
#define all(a) a.begin(), a.end()
#define trace(var) cerr << ">>> " << #var << " = " << var << endl;

typedef long long Integer;
typedef double Real;
typedef vector<int> vi;
typedef vector<string> vs;
typedef map<string, int> Dictionary;
const Real PI = acos(-1);

template <class S, class T> ostream &operator<<(ostream &os, pair<S, T> p) {
  os << '(' << car(p) << ", " << cdr(p) << ')';
  return os;
}

template <class T> ostream &operator<<(ostream &os, vector<T> v) {
  if (v.size() == 0) {
    os << "(empty)";
    return os;
  }
  os << v[0];
  for (int i = 1, len = v.size(); i < len; ++i)
    os << ' ' << v[i];
  return os;
}

int dx[] = {-1, 0, 1, 0};
int dy[] = {0, -1, 0, 1};

int count(vector<int> ls, int x) {
  const int n = ls.size();
  int left;
  int right;

  {{{// search left
     if (ls[0] == x){left = 0;
}
else {
  int l = 0;
  int r = n;
  // ls[l] < x <= ls[r]
  while (l < r) {
    if (l + 1 == r) {
      if (ls[l] == x)
        left = l;
      else if (ls[r] == x)
        left = r;
      else
        return 0;
      break;
    }
    int m = (l + r) / 2;
    if (ls[m] < x) {
      l = m;
    } else {
      r = m;
    }
  }
}
}
}
}

{
  {
    { // search right
      if (ls[n - 1] == x) {
        right = n;
      } else {
        int l = 0;
        int r = n;
        // ls[l] <= x < ls[r]
        while (l < r) {
          if (l + 1 == r) {
            if (ls[l] == x)
              right = r;
            else if (ls[r] == x)
              right = r + 1;
            else
              return 0;
            break;
          }
          int m = (l + r) / 2;
          if (ls[m] <= x) {
            l = m;
          } else {
            r = m;
          }
        }
      }
    }
  }
}

return right - left;
}

int main() {

more:;
  int n, m;
  cin >> n >> m;
  if (!n && !m)
    return 0;

  vector<int> as = {0};
  vector<int> bs = {0};

  int a = 0;
  rep(i, n) {
    int h;
    cin >> h;
    a += h;
    as.push_back(a);
  }
  int b = 0;
  rep(i, m) {
    int h;
    cin >> h;
    b += h;
    bs.push_back(b);
  }

  vector<int> heights;
  rep(i, m) {
    range(j, i + 1, m + 1) { heights.push_back(bs[j] - bs[i]); }
  }

  sort(all(heights));

  int ans = 0;
  rep(i, n) {
    range(j, i + 1, n + 1) {
      int width = as[j] - as[i];
      ans += count(heights, width);
    }
  }
  cout << ans << endl;
  goto more;

  return 0;
}

#include <bits/stdc++.h>
using namespace std;

#define iota(i, n, b, s)                                                       \\
  for (int i = int(b); i != int((b) + (s) * (n)); i += (s))
#define range(i, n, m)                                                         \\
  iota(i, (((n) > (m)) ? ((n) - (m)) : ((m) - (n))), (n), ((n) > (m) ? -1 : 1))
#define rep(i, n) iota(i, (n), 0, 1)
#define loop for (;;)

#define INF (1e9)
#define EPS (1e-9)
#define cons(a, b) (make_pair(a, b))
#define car(a) (a.first)
#define cdr(a) (a.second)
#define cadr(a) (car(cdr(a)))
#define cddr(a) (cdr(cdr(a)))
#define all(a) a.begin(), a.end()
#define trace(var) cerr << ">>> " << #var << " = " << var << endl;

typedef long long Integer;
typedef double Real;
typedef vector<int> vi;
typedef vector<string> vs;
typedef map<string, int> Dictionary;
const Real PI = acos(-1);

template <class S, class T> ostream &operator<<(ostream &os, pair<S, T> p) {
  os << '(' << car(p) << ", " << cdr(p) << ')';
  return os;
}

template <class T> ostream &operator<<(ostream &os, vector<T> v) {
  if (v.size() == 0) {
    os << "(empty)";
    return os;
  }
  os << v[0];
  for (int i = 1, len = v.size(); i < len; ++i)
    os << ' ' << v[i];
  return os;
}

int dx[] = {-1, 0, 1, 0};
int dy[] = {0, -1, 0, 1};

int count(vector<int> ls, int x) {
  const int n = ls.size();
  int left;
  int right;

  {{{// search left
     if (ls[0] == x){left = 0;
}
else {
  int l = 0;
  int r = n;
  // ls[l] < x <= ls[r]
  while (l < r) {
    if (l + 1 == r) {
      if (ls[l] == x)
        left = l;
      else if (ls[r] == x)
        left = r;
      else
        return 0;
      break;
    }
    int m = (l + r) / 2;
    if (ls[m] < x) {
      l = m;
    } else {
      r = m;
    }
  }
}
}
}
}

{
  {
    { // search right
      if (ls[n - 1] == x) {
        right = n;
      } else {
        int l = 0;
        int r = n;
        // ls[l] <= x < ls[r]
        while (l < r) {
          if (l + 1 == r) {
            if (ls[l] == x)
              right = r;
            else if (ls[r] == x)
              right = r + 1;
            else
              return 0;
            break;
          }
          int m = (l + r) / 2;
          if (ls[m] <= x) {
            l = m;
          } else {
            r = m;
          }
        }
      }
    }
  }
}

return right - left;
}

int main() {

more:;
  int n, m;
  cin >> n >> m;
  if (!n && !m)
    return 0;

  vector<int> as = {0};
  vector<int> bs = {0};

  int a = 0;
  rep(i, n) {
    int h;
    cin >> h;
    a += h;
    as.push_back(a);
  }
  int b = 0;
  rep(i, m) {
    int h;
    cin >> h;
    b += h;
    bs.push_back(b);
  }

  map<int, int> ws;
  map<int, int> hs;

  rep(i, n) {
    range(j, i + 1, n + 1) { ws[as[j] - as[i]]++; }
  }

  rep(i, m) {
    range(j, i + 1, m + 1) { hs[bs[j] - bs[i]]++; }
  }

  int ans = 0;
  for (auto &pr : ws) {
    int w = car(pr);
    int c = cdr(pr);
    ans += hs[w] * c;
  }
  cout << ans << endl;
  goto more;

  return 0;
}


#include <bits/stdc++.h>

#define INF 1e9
#define llINF 1e18
#define MOD 1e9 + 7
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define ll long long
using namespace std;
int main() {
  int n, m;
  while (cin >> n >> m, n) {
    int num[2500100] = {};
    int takasa[1600] = {};
    short num2[1600] = {};
    ll ans = 0;
    for (int i = 1; i <= n; i++) {
      int a;
      cin >> a;
      takasa[i] = takasa[i - 1] + a;
    }
    for (int i = 1; i <= m; i++) {
      int a;
      cin >> a;
      num2[i] = num2[i - 1] + a;
      for (int j = 0; j < i; j++) {
        num[num2[i] - num2[j]]++;
      }
    }
    for (int i = 0; i <= n; i++) {
      for (int j = 0; j < i; j++) {
        ans += num[takasa[i] - takasa[j]];
      }
    }
    cout << ans << endl;
  }
  return 0;
}

#include <bits/stdc++.h>

#define INF 1e9
#define llINF 1e18
#define MOD 1e9 + 7
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define ll long long
using namespace std;
int main() {
  int n, m;
  while (cin >> n >> m, n) {
    int num[2500100] = {};
    int takasa[10600] = {};
    int num2[10600] = {};
    ll ans = 0;
    for (int i = 1; i <= n; i++) {
      int a;
      cin >> a;
      takasa[i] = takasa[i - 1] + a;
    }
    for (int i = 1; i <= m; i++) {
      int a;
      cin >> a;
      num2[i] = num2[i - 1] + a;
      for (int j = 0; j < i; j++) {
        num[num2[i] - num2[j]]++;
      }
    }
    for (int i = 0; i <= n; i++) {
      for (int j = 0; j < i; j++) {
        ans += num[takasa[i] - takasa[j]];
      }
    }
    cout << ans << endl;
  }
  return 0;
}