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

int main() {
  int n, m, h, w, ans;
  vector<int> hvec, wvec;
  map<int, int> hmap, wmap;
  map<int, int>::iterator hit, wit;

  while (1) {
    cin >> n >> m;
    if (n == 0 || m == 0)
      break;

    hvec.clear();
    hmap.clear();
    for (int i = 0; i < n; i++) {
      cin >> h;
      hvec.push_back(h);
      int hsum = 0;
      for (int j = i; j > -1; j--) {
        hsum += hvec.at(j);
        hmap[hsum]++;
      }
    }
    wvec.clear();
    wmap.clear();
    for (int i = 0; i < m; i++) {
      cin >> w;
      wvec.push_back(w);
      int wsum = 0;
      for (int j = i; j > -1; j--) {
        wsum += wvec.at(j);
        wmap[wsum]++;
      }
    }

    ans = 0;
    for (hit = hmap.begin(); hit != hmap.end(); hit++) {
      for (wit = wmap.begin(); wit != wmap.end(); wit++) {
        if ((*hit).first == (*wit).first) {
          ans += (*hit).second * (*wit).second;
          break;
        }
      }
    }

    cout << ans << endl;
  }
}

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

int main() {
  int n, m;
  while (cin >> n >> m, n && m) {
    vector<int> vec;
    map<int, int> mapX, mapY;
    // nの入力
    for (int i = 0; i < n; i++) {
      int hi;
      cin >> hi;
      vec.push_back(hi);
    }
    for (int i = 0; i < n; i++) {
      int h = 0;
      for (int j = i; j < n; j++) {
        h += vec[j];
        mapY[h]++;
      }
    }
    vec.clear();
    // mの入力
    for (int i = 0; i < m; i++) {
      int hi;
      cin >> hi;
      vec.push_back(hi);
    }
    for (int i = 0; i < m; i++) {
      int h = 0;
      for (int j = i; j < m; j++) {
        h += vec[j];
        mapX[h]++;
      }
    }

    // 正方形を数える
    int count = 0;
    map<int, int>::iterator iteX, iteY;
    for (iteY = mapY.begin(); iteY != mapY.end(); iteY++) {
      iteX = mapX.end();
      if (mapX.find(iteY->first) != iteX) {
        count += mapX[iteY->first] * iteY->second;
      }
    }
    cout << count << 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>

using namespace std;

#define rep(i, j) REP((i), 0, (j))
#define REP(i, j, k) for (int i = (j); (i) < (k); ++i)
#define BW(a, x, b) ((a) <= (x) && (x) <= (b))
#define ALL(v) (v).begin(), (v).end()
#define LENGTHOF(x) (sizeof(x) / sizeof(*(x)))
#define AFILL(a, b) fill((int *)a, (int *)(a + LENGTHOF(a)), b)
#define SQ(x) ((x) * (x))
#define Mod(x, mod) (((x)+(mod)%(mod))
#define MP make_pair
#define PB push_back
#define Fi first
#define Se second
#define INF (1 << 29)
#define EPS 1e-10
#define MOD 1000000007

typedef pair<int, int> pi;
typedef pair<int, pi> pii;
typedef vector<int> vi;
typedef queue<int> qi;
typedef long long ll;

int N, M;
vector<int> v1, v2;

int main() {
  while (cin >> N >> M && N && M) {
    v1.clear();
    v2.clear();
    vector<int> h(N);
    vector<int> w(M);
    rep(i, N) cin >> h[i + 1];
    rep(i, M) cin >> w[i + 1];

    rep(i, N) h[i + 1] += h[i];
    rep(i, M) w[i + 1] += w[i];

    for (int i = 0; i <= N; i++) {
      for (int j = i + 1; j <= N; j++) {
        v1.push_back(h[j] - h[i]);
      }
    }

    for (int i = 0; i <= M; i++) {
      for (int j = i + 1; j <= M; j++) {
        v2.push_back(w[j] - w[i]);
      }
    }
    sort(v1.begin(), v1.end());
    sort(v2.begin(), v2.end());

    int res = 0;
    rep(i, v1.size()) {
      vector<int>::iterator it = lower_bound(v2.begin(), v2.end(), v1[i]);
      while (*it == v1[i]) {
        res++;
        it++;
      }
    }

    cout << res << endl;
  }
  return 0;
}

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

using namespace std;

#define rep(i, j) REP((i), 0, (j))
#define REP(i, j, k) for (int i = (j); (i) < (k); ++i)
#define BW(a, x, b) ((a) <= (x) && (x) <= (b))
#define ALL(v) (v).begin(), (v).end()
#define LENGTHOF(x) (sizeof(x) / sizeof(*(x)))
#define AFILL(a, b) fill((int *)a, (int *)(a + LENGTHOF(a)), b)
#define SQ(x) ((x) * (x))
#define Mod(x, mod) (((x)+(mod)%(mod))
#define MP make_pair
#define PB push_back
#define Fi first
#define Se second
#define INF (1 << 29)
#define EPS 1e-10
#define MOD 1000000007

typedef pair<int, int> pi;
typedef pair<int, pi> pii;
typedef vector<int> vi;
typedef queue<int> qi;
typedef long long ll;

int N, M;
vector<int> v1, v2;
vector<int> h(N), w(M);

int main() {
  while (cin >> N >> M && N && M) {
    v1.clear();
    v2.clear();
    h.resize(N + 1);
    w.resize(M + 1);
    rep(i, N) cin >> h[i + 1];
    rep(i, M) cin >> w[i + 1];

    rep(i, N) h[i + 1] += h[i];
    rep(i, M) w[i + 1] += w[i];

    for (int i = 0; i <= N; i++) {
      for (int j = i + 1; j <= N; j++) {
        v1.push_back(h[j] - h[i]);
      }
    }

    for (int i = 0; i <= M; i++) {
      for (int j = i + 1; j <= M; j++) {
        v2.push_back(w[j] - w[i]);
      }
    }
    sort(v1.begin(), v1.end());
    sort(v2.begin(), v2.end());

    int res = 0;
    rep(i, v1.size()) {
      vector<int>::iterator it = lower_bound(v2.begin(), v2.end(), v1[i]);
      while (it != v2.end() && *it == v1[i]) {
        res++;
        it++;
      }
    }

    cout << res << endl;
  }
  return 0;
}


#include <algorithm>
#include <array>
#include <assert.h>
#include <bitset>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;

const int mod = 1000000007;
struct Mod {
public:
  int num;
  Mod() : num(0) { ; }
  Mod(long long int n) : num((n % mod + mod) % mod) { ; }
  Mod(int n) : num((n % mod + mod) % mod) { ; }
  operator int() { return num; }
};

Mod operator+(const Mod a, const Mod b) { return Mod((a.num + b.num) % mod); }
Mod operator+(const long long int a, const Mod b) { return Mod(a) + b; }
Mod operator++(Mod &a) { return a + Mod(1); }
Mod operator-(const Mod a, const Mod b) {
  return Mod((mod + a.num - b.num) % mod);
}
Mod operator-(const long long int a, const Mod b) { return Mod(a) - b; }
Mod operator--(Mod &a) { return a - Mod(1); }
Mod operator*(const Mod a, const Mod b) {
  return Mod(((long long)a.num * b.num) % mod);
}
Mod operator*(const long long int a, const Mod b) { return Mod(a) * b; }
Mod operator+=(Mod &a, const Mod b) { return a = a + b; }
Mod operator+=(long long int &a, const Mod b) { return a = a + b; }
Mod operator-=(Mod &a, const Mod b) { return a = a - b; }
Mod operator-=(long long int &a, const Mod b) { return a = a - b; }
Mod operator*=(Mod &a, const Mod b) { return a = a * b; }
Mod operator*=(long long int &a, const Mod b) { return a = a * b; }
Mod operator^(const Mod a, const int n) {
  if (n == 0)
    return Mod(1);
  Mod res = (a * a) ^ (n / 2);
  if (n % 2)
    res = res * a;
  return res;
}
Mod inv(const Mod a) { return a ^ (mod - 2); }
Mod operator/(const Mod a, const Mod b) {
  assert(b.num != 0);
  return a * inv(b);
}
Mod operator/(const long long int a, const Mod b) {
  assert(b.num != 0);
  return Mod(a) * inv(b);
}
Mod operator/=(Mod &a, const Mod b) {
  assert(b.num != 0);
  return a = a * inv(b);
}

#define MAX_N 1024000

Mod fact[MAX_N], factinv[MAX_N];
void init() {
  fact[0] = Mod(1);
  factinv[0] = 1;
  for (int i = 0; i < MAX_N - 1; ++i) {
    fact[i + 1] = fact[i] * Mod(i + 1);
    factinv[i + 1] = factinv[i] / Mod(i + 1);
  }
}
Mod comb(const int a, const int b) {
  return fact[a] * factinv[b] * factinv[a - b];
}

template <typename T>
vector<vector<T>> keisann(const vector<vector<T>> l,
                          const vector<vector<T>> r) {
  vector<vector<T>> ans(l.size(), vector<T>(r[0].size()));
  assert(l[0].size() == r.size());
  for (unsigned int h = 0; h < l.size(); ++h) {
    for (unsigned int i = 0; i < r.size(); ++i) {
      for (unsigned int w = 0; w < r[0].size(); ++w) {

        ans[h][w] += l[h][i] * r[i][w];
      }
    }
  }
  return ans;
}
template <typename T>
vector<vector<T>> powgyou(vector<vector<T>> a, const long long int n) {
  assert(a.size() == a[0].size());
  if (!n) {
    vector<vector<T>> e(a.size(), vector<T>(a[0].size()));
    for (unsigned int i = 0; i < a.size(); ++i) {
      e[i][i] = 1;
    }
    return e;
  }
  if (n == 1)
    return a;
  else {
    vector<vector<T>> ans(a.size(), vector<T>(a[0].size(), 0));
    ans = powgyou(a, n / 2);
    ans = keisann(ans, ans);
    if (n % 2) {
      ans = keisann(ans, a);
    }
    return ans;
  }
}

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c) for (auto i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()

const int INF = 2147483647;
const long long int L_INF = 9223372036854775807;

typedef int Weight;
struct Edge {
  int src, dst;
  Weight weight;
  Edge(int src, int dst, Weight weight) : src(src), dst(dst), weight(weight) {}
};
bool operator<(const Edge &e, const Edge &f) {
  return e.weight != f.weight ? e.weight > f.weight : // !!INVERSE!!
             e.src != f.src ? e.src < f.src
                            : e.dst < f.dst;
}
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;

typedef vector<Weight> Array;
typedef vector<Array> Matrix;
#define RESIDUE(s, t) (capacity[s][t] - flow[s][t])

vector<pair<int, int>> uses;

// 流量0の逆辺も張らないと正しく求まらないので注意
Weight maximumFlow(const Graph &ag, int s, int t) {

  Graph g(ag);
  for (int i = 0; i < ag.size(); ++i) {
    for (int j = 0; j < ag[i].size(); ++j) {
      int d = ag[i][j].dst;
      int s = ag[i][j].src;

      bool ok = false;
      for (int k = 0; k < ag[d].size(); ++k) {
        if (ag[d][k].src == s) {
          ok = true;
          break;
        }
      }
      if (!ok) {
        g[d].push_back(Edge(d, s, 0));
      }
    }
  }

  int n = g.size();
  Matrix flow(n, Array(n)), capacity(n, Array(n));
  REP(u, n) FOR(e, g[u]) capacity[e->src][e->dst] += e->weight;

  Weight total = 0;
  while (1) {
    queue<int> Q;
    Q.push(s);
    vector<int> prev(n, -1);
    prev[s] = s;
    while (!Q.empty() && prev[t] < 0) {
      int u = Q.front();
      Q.pop();
      FOR(e, g[u]) if (prev[e->dst] < 0 && RESIDUE(u, e->dst) > 0) {
        prev[e->dst] = u;
        Q.push(e->dst);
      }
    }
    if (prev[t] < 0)
      return total; // prev[x] == -1 <=> t-side
    Weight inc = INF;
    for (int j = t; prev[j] != j; j = prev[j])
      inc = min(inc, RESIDUE(prev[j], j));
    for (int j = t; prev[j] != j; j = prev[j])
      flow[prev[j]][j] += inc, flow[j][prev[j]] -= inc;
    total += inc;
  }
}

map<long long int, int> soinnsuu(long long int a) {
  map<long long int, int> ans;
  for (long long i = 2; i * i <= a; ++i) {
    while (a % i == 0) {
      ans[i]++;
      a /= i;
    }
  }
  if (a != 1)
    ans[a]++;

  return ans;
}

bool isso(long long int a) {
  if (a == 1)
    return false;
  for (long long int i = 2; i * i <= a; ++i) {
    if (!(a % i)) {
      return false;
    } else {
    }
  }
  return true;
}

struct vert {
  int deg;
  int s;
  int id;
  vert(int a, int b, int c) {
    deg = a;
    s = b;
    id = c;
  }
};

#include <algorithm>
#include <array>
#include <assert.h>
#include <bitset>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
class Compare {
public:
  bool operator()(const pair<int, int> &l, const pair<int, int> &r) {
    return l.second > r.second;
  }
};

int fri[100][100];
int N, M;

long long int getans(vector<int> be, int now, vector<int> hapiness) {
  if (now == N) {
    return accumulate(hapiness.begin(), hapiness.end(), 0);
  }

  bool canjoin = true;
  long long int ans(getans(be, now + 1, hapiness));

  if (be.size() == 0) {
    be.push_back(now);
    hapiness.push_back(0);
    ans = max(ans, getans(be, now + 1, hapiness));
  } else if (be.size() == 1) {
    be.push_back(now);
    hapiness.push_back(0);
    hapiness[0] = fri[now][be[0]];
    hapiness[1] = fri[now][be[0]];
    ans = max(ans, getans(be, now + 1, hapiness));
  } else {
    int aminhap = 99999999;
    for (int i = 0; i < be.size(); ++i) {
      if (fri[now][be[i]]) {
        aminhap = min(aminhap, fri[now][be[i]]);
        hapiness[i] = min(hapiness[i], fri[now][be[i]]);
      } else {
        canjoin = false;
        break;
      }
    }
    if (canjoin) {

      be.push_back(now);
      hapiness.push_back(aminhap);
      ans = max(ans, getans(be, now, hapiness));
    }
  }

  return ans;
}

int main() {
  while (1) {

    int N;
    cin >> N;
    int M;
    cin >> M;
    if (!N)
      break;
    vector<int> hs, ws;
    for (int i = 0; i < N; ++i) {
      int h;
      cin >> h;
      hs.push_back(h);
    }
    for (int i = 0; i < M; ++i) {
      int w;
      cin >> w;
      ws.push_back(w);
    }

    map<int, long long int> hnums, wnums;

    for (int s = 1; s <= hs.size(); ++s) {
      for (int u = 0; u <= hs.size() - s; ++u) {
        int num = 0;
        for (int y = u; y < u + s; ++y) {
          num += hs[y];
        }
        hnums[num]++;
      }
    }
    for (int s = 1; s <= hs.size(); ++s) {
      for (int d = 0; d <= ws.size() - s; ++d) {
        int num = 0;
        for (int x = d; x < d + s; ++x) {
          num += ws[x];
        }
        wnums[num]++;
      }
    }
    long long int ans = 0;
    for (auto it = hnums.begin(); it != hnums.end(); ++it) {
      ans += (*it).second * wnums[(*it).first];
    }
    cout << ans << endl;
  }
  return 0;
}

#include <algorithm>
#include <array>
#include <assert.h>
#include <bitset>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;

const int mod = 1000000007;
struct Mod {
public:
  int num;
  Mod() : num(0) { ; }
  Mod(long long int n) : num((n % mod + mod) % mod) { ; }
  Mod(int n) : num((n % mod + mod) % mod) { ; }
  operator int() { return num; }
};

Mod operator+(const Mod a, const Mod b) { return Mod((a.num + b.num) % mod); }
Mod operator+(const long long int a, const Mod b) { return Mod(a) + b; }
Mod operator++(Mod &a) { return a + Mod(1); }
Mod operator-(const Mod a, const Mod b) {
  return Mod((mod + a.num - b.num) % mod);
}
Mod operator-(const long long int a, const Mod b) { return Mod(a) - b; }
Mod operator--(Mod &a) { return a - Mod(1); }
Mod operator*(const Mod a, const Mod b) {
  return Mod(((long long)a.num * b.num) % mod);
}
Mod operator*(const long long int a, const Mod b) { return Mod(a) * b; }
Mod operator+=(Mod &a, const Mod b) { return a = a + b; }
Mod operator+=(long long int &a, const Mod b) { return a = a + b; }
Mod operator-=(Mod &a, const Mod b) { return a = a - b; }
Mod operator-=(long long int &a, const Mod b) { return a = a - b; }
Mod operator*=(Mod &a, const Mod b) { return a = a * b; }
Mod operator*=(long long int &a, const Mod b) { return a = a * b; }
Mod operator^(const Mod a, const int n) {
  if (n == 0)
    return Mod(1);
  Mod res = (a * a) ^ (n / 2);
  if (n % 2)
    res = res * a;
  return res;
}
Mod inv(const Mod a) { return a ^ (mod - 2); }
Mod operator/(const Mod a, const Mod b) {
  assert(b.num != 0);
  return a * inv(b);
}
Mod operator/(const long long int a, const Mod b) {
  assert(b.num != 0);
  return Mod(a) * inv(b);
}
Mod operator/=(Mod &a, const Mod b) {
  assert(b.num != 0);
  return a = a * inv(b);
}

#define MAX_N 1024000

Mod fact[MAX_N], factinv[MAX_N];
void init() {
  fact[0] = Mod(1);
  factinv[0] = 1;
  for (int i = 0; i < MAX_N - 1; ++i) {
    fact[i + 1] = fact[i] * Mod(i + 1);
    factinv[i + 1] = factinv[i] / Mod(i + 1);
  }
}
Mod comb(const int a, const int b) {
  return fact[a] * factinv[b] * factinv[a - b];
}

template <typename T>
vector<vector<T>> keisann(const vector<vector<T>> l,
                          const vector<vector<T>> r) {
  vector<vector<T>> ans(l.size(), vector<T>(r[0].size()));
  assert(l[0].size() == r.size());
  for (unsigned int h = 0; h < l.size(); ++h) {
    for (unsigned int i = 0; i < r.size(); ++i) {
      for (unsigned int w = 0; w < r[0].size(); ++w) {

        ans[h][w] += l[h][i] * r[i][w];
      }
    }
  }
  return ans;
}
template <typename T>
vector<vector<T>> powgyou(vector<vector<T>> a, const long long int n) {
  assert(a.size() == a[0].size());
  if (!n) {
    vector<vector<T>> e(a.size(), vector<T>(a[0].size()));
    for (unsigned int i = 0; i < a.size(); ++i) {
      e[i][i] = 1;
    }
    return e;
  }
  if (n == 1)
    return a;
  else {
    vector<vector<T>> ans(a.size(), vector<T>(a[0].size(), 0));
    ans = powgyou(a, n / 2);
    ans = keisann(ans, ans);
    if (n % 2) {
      ans = keisann(ans, a);
    }
    return ans;
  }
}

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c) for (auto i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()

const int INF = 2147483647;
const long long int L_INF = 9223372036854775807;

typedef int Weight;
struct Edge {
  int src, dst;
  Weight weight;
  Edge(int src, int dst, Weight weight) : src(src), dst(dst), weight(weight) {}
};
bool operator<(const Edge &e, const Edge &f) {
  return e.weight != f.weight ? e.weight > f.weight : // !!INVERSE!!
             e.src != f.src ? e.src < f.src
                            : e.dst < f.dst;
}
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;

typedef vector<Weight> Array;
typedef vector<Array> Matrix;
#define RESIDUE(s, t) (capacity[s][t] - flow[s][t])

vector<pair<int, int>> uses;

// 流量0の逆辺も張らないと正しく求まらないので注意
Weight maximumFlow(const Graph &ag, int s, int t) {

  Graph g(ag);
  for (int i = 0; i < ag.size(); ++i) {
    for (int j = 0; j < ag[i].size(); ++j) {
      int d = ag[i][j].dst;
      int s = ag[i][j].src;

      bool ok = false;
      for (int k = 0; k < ag[d].size(); ++k) {
        if (ag[d][k].src == s) {
          ok = true;
          break;
        }
      }
      if (!ok) {
        g[d].push_back(Edge(d, s, 0));
      }
    }
  }

  int n = g.size();
  Matrix flow(n, Array(n)), capacity(n, Array(n));
  REP(u, n) FOR(e, g[u]) capacity[e->src][e->dst] += e->weight;

  Weight total = 0;
  while (1) {
    queue<int> Q;
    Q.push(s);
    vector<int> prev(n, -1);
    prev[s] = s;
    while (!Q.empty() && prev[t] < 0) {
      int u = Q.front();
      Q.pop();
      FOR(e, g[u]) if (prev[e->dst] < 0 && RESIDUE(u, e->dst) > 0) {
        prev[e->dst] = u;
        Q.push(e->dst);
      }
    }
    if (prev[t] < 0)
      return total; // prev[x] == -1 <=> t-side
    Weight inc = INF;
    for (int j = t; prev[j] != j; j = prev[j])
      inc = min(inc, RESIDUE(prev[j], j));
    for (int j = t; prev[j] != j; j = prev[j])
      flow[prev[j]][j] += inc, flow[j][prev[j]] -= inc;
    total += inc;
  }
}

map<long long int, int> soinnsuu(long long int a) {
  map<long long int, int> ans;
  for (long long i = 2; i * i <= a; ++i) {
    while (a % i == 0) {
      ans[i]++;
      a /= i;
    }
  }
  if (a != 1)
    ans[a]++;

  return ans;
}

bool isso(long long int a) {
  if (a == 1)
    return false;
  for (long long int i = 2; i * i <= a; ++i) {
    if (!(a % i)) {
      return false;
    } else {
    }
  }
  return true;
}

struct vert {
  int deg;
  int s;
  int id;
  vert(int a, int b, int c) {
    deg = a;
    s = b;
    id = c;
  }
};

#include <algorithm>
#include <array>
#include <assert.h>
#include <bitset>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
class Compare {
public:
  bool operator()(const pair<int, int> &l, const pair<int, int> &r) {
    return l.second > r.second;
  }
};

int main() {
  while (1) {
    int N;
    cin >> N;
    int M;
    cin >> M;
    if (!N)
      break;
    vector<int> hs, ws;
    for (int i = 0; i < N; ++i) {
      int h;
      cin >> h;
      hs.push_back(h);
    }
    for (int i = 0; i < M; ++i) {
      int w;
      cin >> w;
      ws.push_back(w);
    }

    map<int, long long int> hnums, wnums;

    for (int u = 0; u <= hs.size() - 1; ++u) {
      int num = 0;
      for (int y = u; y < hs.size(); ++y) {
        num += hs[y];
        hnums[num]++;
      }
    }

    for (int d = 0; d <= ws.size() - 1; ++d) {
      int num = 0;
      for (int x = d; x < ws.size(); ++x) {
        num += ws[x];
        wnums[num]++;
      }
    }

    long long int ans = 0;
    for (auto it = hnums.begin(); it != hnums.end(); ++it) {
      ans += (*it).second * wnums[(*it).first];
    }
    cout << ans << endl;
  }
  return 0;
}


#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define fi first
#define se second
#define fcout(n) cout << fixed << setprecision((n))
#define cinl(str) getline(cin, (str))
using namespace std;
bool value(int y, int x, int R, int C) {
  return 0 <= y && y < R && 0 <= x && x < C;
}
typedef pair<int, int> pii;
typedef long long ll;
typedef vector<int> vi;
typedef vector<long long> vll;
typedef vector<vi> vvi;
double pie = acos(-1);
int INF = 1000000007;
int dx[4] = {0, -1, 0, 1};
int dy[4] = {-1, 0, 1, 0};

long long hsum2[1500005], wsum2[1500005];

int main() {
  int n, m;
  long long ans = 0;
  int h[1505], w[1505];
  long long hsum1[1505], wsum1[1505];
  while (true) {
    cin >> n >> m;
    if (n + m == 0)
      return 0;
    else {
      for (int i = 0; i < 1505; i++) {
        hsum1[i] = 0;
        wsum1[i] = 0;
      }
      fill(hsum2, hsum2 + 1500005, 0);
      fill(wsum2, wsum2 + 1500005, 0);

      for (int i = 0; i < n; i++) {
        cin >> h[i];
        if (i == 0) {
          hsum1[i] += h[i];
        } else {
          hsum1[i] = hsum1[i - 1] + h[i];
        }
      }
      for (int i = 0; i < m; i++) {
        cin >> w[i];
        if (i == 0) {
          wsum1[i] += w[i];
        } else {
          wsum1[i] += wsum1[i - 1] + w[i];
        }
      }
      for (int i = 0; i < n; i++) {
        hsum2[hsum1[i]]++;
        for (int j = i + 1; j < n; j++) {
          hsum2[hsum1[j] - hsum1[i]]++;
        }
      }

      for (int i = 0; i < m; i++) {
        wsum2[wsum1[i]]++;
        for (int j = 0; j < m; j++) {
          wsum2[wsum1[j] - wsum1[i]]++;
        }
      }
    }
    for (int i = 0; i < 1500005; i++) {
      ans += hsum2[i] * wsum2[i];
    }
    cout << ans << endl;
    ans = 0;
  }
}

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define fi first
#define se second
#define fcout(n) cout << fixed << setprecision((n))
#define cinl(str) getline(cin, (str))
using namespace std;
bool value(int y, int x, int R, int C) {
  return 0 <= y && y < R && 0 <= x && x < C;
}
typedef pair<int, int> pii;
typedef long long ll;
typedef vector<int> vi;
typedef vector<long long> vll;
typedef vector<vi> vvi;
double pie = acos(-1);
int INF = 1000000007;
int dx[4] = {0, -1, 0, 1};
int dy[4] = {-1, 0, 1, 0};

long long hsum2[1500005], wsum2[1500005];

int main() {
  int n, m;
  long long ans = 0;
  int h[1505], w[1505];
  long long hsum1[1505], wsum1[1505];
  while (true) {
    cin >> n >> m;
    if (n + m == 0)
      return 0;
    else {
      for (int i = 0; i < 1505; i++) {
        hsum1[i] = 0;
        wsum1[i] = 0;
      }
      fill(hsum2, hsum2 + 1500005, 0);
      fill(wsum2, wsum2 + 1500005, 0);

      for (int i = 0; i < n; i++) {
        cin >> h[i];
        if (i == 0) {
          hsum1[i] += h[i];
        } else {
          hsum1[i] = hsum1[i - 1] + h[i];
        }
      }
      for (int i = 0; i < m; i++) {
        cin >> w[i];
        if (i == 0) {
          wsum1[i] += w[i];
        } else {
          wsum1[i] += wsum1[i - 1] + w[i];
        }
      }
      for (int i = 0; i < n; i++) {
        hsum2[hsum1[i]]++;
        for (int j = i + 1; j < n; j++) {
          hsum2[hsum1[j] - hsum1[i]]++;
        }
      }

      for (int i = 0; i < m; i++) {
        wsum2[wsum1[i]]++;
        for (int j = i + 1; j < m; j++) {
          wsum2[wsum1[j] - wsum1[i]]++;
        }
      }
    }
    for (int i = 0; i < 1500005; i++) {
      ans += hsum2[i] * wsum2[i];
    }
    cout << ans << endl;
    ans = 0;
  }
}


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

int main() {
  int n, m;
  while (cin >> n >> m, n + m) {
    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 count = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        int hh = i;
        int ww = j;
        int h_sum = 0;
        int w_sum = 0;
        count--;
        while (hh < n && ww < m) {
          while (hh < n && ww < m && h_sum != w_sum) {
            while (h_sum == 0 || (hh < n && h_sum < w_sum)) {
              h_sum += h[hh++];
            }
            while (w_sum == 0 || (ww < m && h_sum > w_sum)) {
              w_sum += w[ww++];
            }
          }
          if (h_sum == w_sum) {
            count++;
            if (hh < n)
              h_sum += h[hh++];
          }
        }
        if (h_sum != w_sum) {
          while (hh >= n && ww < m && h_sum > w_sum)
            w_sum += w[ww++];
          while (ww >= m && hh < n && h_sum < w_sum)
            h_sum += h[hh++];
          if (h_sum == w_sum)
            count++;
        }
      }
    }
    cout << count << endl;
  }
  return 0;
}

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

int hh[1000 * 1500 + 1];
int ww[1000 * 1500 + 1];

int main() {
  int n, m;
  while (cin >> n >> m, n + m) {
    vector<int> h(n);
    vector<int> w(m);
    memset(hh, 0, sizeof(hh));
    memset(ww, 0, sizeof(ww));
    int _max = 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 sum = 0;
      for (int j = i; j < n; j++) {
        sum += h[j];
        hh[sum]++;
        _max = max(_max, sum);
      }
    }

    for (int i = 0; i < m; i++) {
      int sum = 0;
      for (int j = i; j < m; j++) {
        sum += w[j];
        ww[sum]++;
        _max = max(_max, sum);
      }
    }

    int sum = 0;
    for (int i = 0; i <= _max; i++) {
      if (ww[i] != 0 && hh[i] != 0) {
        sum += (hh[i] * ww[i]);
      }
    }
    cout << sum << endl;
  }
  return 0;
}


#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <list>
#include <queue>
#include <stack>
#include <utility>
#include <vector>
using namespace std;
int in() {
  int x;
  cin >> x;
  return x;
}
int main() {
  int n, m;
  while (cin >> n >> m && n && m) {
    vector<int> h, w;
    int mp[2000] = {};
    int ans = 0;
    h.push_back(0);
    w.push_back(0);
    for (int i = 0; i < n; i++)
      h.push_back(in());
    for (int i = 0; i < m; i++)
      w.push_back(in());
    for (int i = 1; i <= n; i++)
      h[i] += h[i - 1];
    for (int l = 1; l <= m; l++)
      w[l] += w[l - 1];
    for (int i = 0; i < n; i++) {
      for (int l = i + 1; l <= n; l++) {
        mp[h[l] - h[i]]++;
      }
    }
    for (int i = 0; i < m; i++) {
      for (int l = i + 1; l <= m; l++) {
        if (mp[w[l] - w[i]] != 0) {
          ans += mp[w[l] - w[i]];
          // cout<<i<<" "<<l<<endl;;
        }
      }
    }

    cout << ans << endl;
  }
}

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <list>
#include <queue>
#include <stack>
#include <utility>
#include <vector>
using namespace std;
int in() {
  int x;
  cin >> x;
  return x;
}
vector<int> h, w;
int mp[1500001] = {};
int main() {
  int n, m;
  while (cin >> n >> m && n && m) {

    int ans = 0;
    h.push_back(0);
    w.push_back(0);
    for (int i = 0; i < n; i++)
      h.push_back(in());
    for (int i = 0; i < m; i++)
      w.push_back(in());
    for (int i = 1; i <= n; i++)
      h[i] += h[i - 1];
    for (int l = 1; l <= m; l++)
      w[l] += w[l - 1];
    for (int i = 0; i < n; i++) {
      for (int l = i + 1; l <= n; l++) {
        mp[h[l] - h[i]]++;
      }
    }
    for (int i = 0; i < m; i++) {
      for (int l = i + 1; l <= m; l++) {
        if (mp[w[l] - w[i]] != 0) {
          ans += mp[w[l] - w[i]];
          // cout<<i<<" "<<l<<endl;;
        }
      }
    }
    h.clear();
    w.clear();
    for (int i = 0; i < 1500001; i++)
      mp[i] = 0;
    cout << ans << endl;
  }
}


#include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
using vvi = vector<vi>;
using vs = vector<string>;
using msi = map<string, int>;
using pii = pair<int, int>;
using vlai = valarray<int>;
using ll = long long;
#define rep(i, n) for (int i = 0; i < n; i++)
#define range(i, s, n) for (int i = s; i < n; i++)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()

int main(void) {
  int n, m;
  while (cin >> n >> m, n) {
    vi v(n + 1, 0), h(m + 1, 0);
    rep(i, n) cin >> v[i + 1];
    rep(i, m) cin >> h[i + 1];
    partial_sum(all(v), v.begin());
    partial_sum(all(h), h.begin());
    multiset<int> h2;
    rep(i, m + 1) range(j, i + 1, m + 1) h2.insert(h[j] - h[i]);
    ll ans = 0;
    rep(i, n + 1) range(j, i + 1, n + 1) ans += h2.count(v[j] - v[i]);
    cout << ans << endl;
  }
}

#include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
using vvi = vector<vi>;
using vs = vector<string>;
using msi = map<string, int>;
using pii = pair<int, int>;
using vlai = valarray<int>;
using ll = long long;
#define rep(i, n) for (int i = 0; i < n; i++)
#define range(i, s, n) for (int i = s; i < n; i++)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()

int main(void) {
  int n, m;
  while (cin >> n >> m, n) {
    vi v(n + 1, 0), h(m + 1, 0);
    rep(i, n) cin >> v[i + 1];
    rep(i, m) cin >> h[i + 1];
    partial_sum(all(v), v.begin());
    partial_sum(all(h), h.begin());
    map<int, int> v2, h2;
    rep(i, n + 1) range(j, i + 1, n + 1) {
      if (v2.find(v[j] - v[i]) == v2.end())
        v2[v[j] - v[i]] = 0;
      v2[v[j] - v[i]]++;
    }
    rep(i, m + 1) range(j, i + 1, m + 1) {
      if (h2.find(h[j] - h[i]) == h2.end())
        h2[h[j] - h[i]] = 0;
      h2[h[j] - h[i]]++;
    }
    ll ans = 0;
    for (auto i : v2) {
      if (h2.find(i.first) != h2.end()) {
        ans += i.second * h2[i.first];
      }
    }
    cout << ans << endl;
  }
}


#include <algorithm>
#include <iostream>
#define MAX_RANGE 1500
#define MAX_VALUE 1000
using namespace std;

int h[MAX_RANGE], w[MAX_RANGE];

int htype[MAX_VALUE + 1], wtype[MAX_VALUE + 1];

void calcAns(int hsum, int wsum) {
  int ans = 0;
  for (int i = 1; i <= min(hsum, wsum); i++)
    ans += htype[i] * wtype[i];
  cout << ans << endl;
}

void calcTypeNumber(int n, int m) {
  for (int i = 0; i < n; i++) {
    int temp = h[i];
    htype[temp]++;
    for (int j = i + 1; j < n; j++) {
      temp += h[j];
      htype[temp]++;
    }
  }

  for (int i = 0; i < m; i++) {
    int temp = w[i];
    wtype[temp]++;
    for (int j = i + 1; j < m; j++) {
      temp += w[j];
      wtype[temp]++;
    }
  }
}

void initArray() {
  for (int i = 0; i < MAX_VALUE + 1; i++) {
    htype[i] = wtype[i] = 0;
  }
}

int main() {
  while (true) {
    int n, m;
    cin >> n >> m;
    if (n == 0 && m == 0)
      return 0;

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

    int hsum = 0, wsum = 0;
    for (int i = 0; i < n; i++)
      hsum += h[i];
    for (int i = 0; i < m; i++)
      wsum += w[i];

    initArray();

    calcTypeNumber(n, m);

    calcAns(hsum, wsum);
  }
}

#include <algorithm>
#include <iostream>
#define MAX_RANGE 1500
#define MAX_VALUE 1000
using namespace std;

int h[MAX_RANGE], w[MAX_RANGE];

int htype[MAX_VALUE * MAX_RANGE + 1], wtype[MAX_VALUE * MAX_RANGE + 1];

void calcAns(int hsum, int wsum) {
  int ans = 0;
  for (int i = 1; i <= min(hsum, wsum); i++)
    ans += htype[i] * wtype[i];
  cout << ans << endl;
}

void calcTypeNumber(int n, int m) {
  for (int i = 0; i < n; i++) {
    int temp = h[i];
    htype[temp]++;
    for (int j = i + 1; j < n; j++) {
      temp += h[j];
      htype[temp]++;
    }
  }

  for (int i = 0; i < m; i++) {
    int temp = w[i];
    wtype[temp]++;
    for (int j = i + 1; j < m; j++) {
      temp += w[j];
      wtype[temp]++;
    }
  }
}

void initArray() {
  for (int i = 0; i < MAX_VALUE * MAX_RANGE + 1; i++) {
    htype[i] = wtype[i] = 0;
  }
}

int main() {
  while (true) {
    int n, m;
    cin >> n >> m;
    if (n == 0 && m == 0)
      return 0;

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

    int hsum = 0, wsum = 0;
    for (int i = 0; i < n; i++)
      hsum += h[i];
    for (int i = 0; i < m; i++)
      wsum += w[i];

    initArray();

    calcTypeNumber(n, m);

    calcAns(hsum, wsum);
  }
}


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

const int MAX = 1501;

signed main() {
  while (true) {
    int n, m;
    cin >> n >> m;
    if (!n && !m)
      return 0;
    int hcount[MAX], wcount[MAX];
    fill(hcount, hcount + MAX, 0);
    fill(wcount, wcount + MAX, 0);
    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 hsum[n], wsum[m];
    hsum[0] = h[0];
    for (int i = 1; i < n; i++) {
      hsum[i] = hsum[i - 1] + h[i];
    }
    wsum[0] = w[0];
    for (int i = 1; i < m; i++) {
      wsum[i] = wsum[i - 1] + w[i];
    }
    for (int i = 0; i < n; i++) {
      hcount[hsum[i]]++;
      for (int j = i - 1; j >= 0; j--) {
        hcount[hsum[i] - hsum[j]]++;
      }
    }
    for (int i = 0; i < m; i++) {
      wcount[wsum[i]]++;
      for (int j = i - 1; j >= 0; j--) {
        wcount[wsum[i] - wsum[j]]++;
      }
    }
    int count = 0;
    for (int i = 1; i < MAX; i++) {
      count += hcount[i] * wcount[i];
    }
    cout << count << endl;
  }
}

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

const int MAX = 1500001;

int hcount[MAX], wcount[MAX];

signed main() {
  while (true) {
    int n, m;
    cin >> n >> m;
    if (!n && !m)
      return 0;
    fill(hcount, hcount + MAX, 0);
    fill(wcount, wcount + MAX, 0);
    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 hsum[n], wsum[m];
    hsum[0] = h[0];
    for (int i = 1; i < n; i++) {
      hsum[i] = hsum[i - 1] + h[i];
    }
    wsum[0] = w[0];
    for (int i = 1; i < m; i++) {
      wsum[i] = wsum[i - 1] + w[i];
    }
    for (int i = 0; i < n; i++) {
      hcount[hsum[i]]++;
      for (int j = i - 1; j >= 0; j--) {
        hcount[hsum[i] - hsum[j]]++;
      }
    }
    for (int i = 0; i < m; i++) {
      wcount[wsum[i]]++;
      for (int j = i - 1; j >= 0; j--) {
        wcount[wsum[i] - wsum[j]]++;
      }
    }
    int count = 0;
    for (int i = 1; i < MAX; i++) {
      count += hcount[i] * wcount[i];
    }
    cout << count << endl;
  }
}


#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;

int s1[2000], s2[2000];
int main() {
  int n, m;
  while (scanf("%d%d", &n, &m), n) {
    rep(i, n) scanf("%d", &s1[i + 1]);
    rep(i, m) scanf("%d", &s2[i + 1]);
    for (int i = 1; i <= n; i++)
      s1[i] += s1[i - 1];
    for (int i = 1; i <= m; i++)
      s2[i] += s2[i - 1];
    int cnt = 0;
    rep(i, n + 1) rep(j, m + 1) rep(k, i) rep(l, j) {
      if (s1[i] - s1[k] == s2[j] - s2[l])
        cnt++;
    }
    printf("%d\\n", cnt);
  }
}

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;

int s1[2000], s2[2000];
int main() {
  int n, m;
  while (scanf("%d%d", &n, &m), n) {
    rep(i, n) scanf("%d", &s1[i + 1]);
    rep(i, m) scanf("%d", &s2[i + 1]);
    for (int i = 1; i <= n; i++)
      s1[i] += s1[i - 1];
    for (int i = 1; i <= m; i++)
      s2[i] += s2[i - 1];
    int cnt = 0;
    map<int, int> mp1, mp2;
    rep(i, n + 1) rep(j, i) mp1[s1[i] - s1[j]]++;
    rep(i, m + 1) rep(j, i) mp2[s2[i] - s2[j]]++;
    for (auto p : mp1) {
      cnt += p.second * mp2[p.first];
    }
    printf("%d\\n", cnt);
  }
}


#include <iostream>
#include <unordered_map>
#include <vector>
using namespace std;
typedef long long ll;
int main() {
  int N, M;
  while (cin >> N >> M && (N | M)) {
    vector<int> x(N + 1), y(M + 1);
    for (int i = 0; i < N; i++) {
      cin >> y[i + 1];
      y[i + 1] += y[i];
    }
    for (int i = 0; i < M; i++) {
      cin >> x[i + 1];
      x[i + 1] += x[i];
    }
    N++, M++;
    unordered_map<int, int> mp;
    for (int i = 0; i < N; i++)
      for (int j = 0; j < M; j++)
        mp[y[i] - x[j]]++;
    // 斜め45度線
    ll ans = 0;
    for (auto e : mp)
      if (e.second >= 2)
        ans += (ll)e.second * (e.second - 1) / 2;
    cout << ans << endl;
  }
  return 0;
}

#include <iostream>
#include <unordered_map>
#include <vector>
using namespace std;
int main() {
  int N, M;
  while (cin >> N >> M && (N | M)) {
    vector<int> y(N + 1), x(M + 1);
    for (int i = 0; i < N; i++) {
      cin >> y[i + 1];
      y[i + 1] += y[i];
    }
    for (int i = 0; i < M; i++) {
      cin >> x[i + 1];
      x[i + 1] += x[i];
    }
    N++, M++;
    unordered_map<int, int> mp;
    for (int i = 0; i < N; i++)
      for (int j = 0; j < M; j++)
        mp[y[i] - x[j]]++;
    // 斜め45度線
    int ans = 0;
    for (auto e : mp)
      if (e.second >= 2)
        ans += e.second * (e.second - 1) / 2;
    cout << ans << endl;
  }
  return 0;
}


#include <iostream>
using namespace std;

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

    int hcurs[m * (m - 1) / 2];
    int wcurs[n * (n - 1) / 2];
    int ii = 0;
    hcurs[ii++] = hcum[0];
    for (int i2 = 1; i2 < m; i2++) {
      hcurs[ii++] = hcum[i2];
    }
    for (int i1 = 1; i1 < m; i1++) {
      for (int i2 = i1; i2 < m; i2++) {
        hcurs[ii++] = hcum[i2] - hcum[i1 - 1];
      }
    }
    int jj = 0;
    wcurs[jj++] = wcum[0];
    for (int j2 = 1; j2 < n; j2++) {
      wcurs[jj++] = wcum[j2];
    }
    for (int j1 = 1; j1 < n; j1++) {
      for (int j2 = j1; j2 < n; j2++) {
        wcurs[jj++] = wcum[j2] - wcum[j1 - 1];
      }
    }

    int cnt = 0;
    for (int i = 0; i < ii; i++) {
      for (int j = 0; j < jj; j++) {
        if (hcurs[i] == wcurs[j]) {
          cnt++;
        }
      }
    }
    cout << cnt << endl;
  }
}

#include <iostream>
using namespace std;

int main() {
  int m = 0, n = 0;
  while (cin >> m >> n && (m > 0 && n > 0)) {
    int h[m];
    int w[n];
    int hcum[m];
    int wcum[n];

    cin >> h[0];
    hcum[0] = h[0];
    for (int i = 1; i < m; i++) {
      cin >> h[i];
      hcum[i] = hcum[i - 1] + h[i];
    }
    cin >> w[0];
    wcum[0] = w[0];
    for (int i = 1; i < n; i++) {
      cin >> w[i];
      wcum[i] = wcum[i - 1] + w[i];
    }

    int hend = hcum[m - 1];
    int wend = wcum[n - 1];
    int hcurs[hend];
    int wcurs[wend];
    for (int i = 0; i < hend; i++)
      hcurs[i] = 0;
    for (int i = 0; i < wend; i++)
      wcurs[i] = 0;

    for (int i = 0; i < m; i++)
      hcurs[hcum[i] - 1] += 1;
    for (int i = 0; i < n; i++)
      wcurs[wcum[i] - 1] += 1;

    for (int i = 1; i < m; i++) {
      for (int j = i; j < m; j++) {
        int l = hcum[j] - hcum[i - 1];
        hcurs[l - 1] += 1;
      }
    }

    for (int i = 1; i < n; i++) {
      for (int j = i; j < n; j++) {
        int l = wcum[j] - wcum[i - 1];
        wcurs[l - 1] += 1;
      }
    }

    int cnt = 0;
    int l = hend < wend ? hend : wend;
    for (int i = 0; i < l; i++) {
      cnt += hcurs[i] * wcurs[i];
    }
    cout << cnt << endl;
  }
}


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

int main() {
  int N, M;
  int i;
  while (cin >> N && cin >> M && N > 0 && N > 0) {
    int count = 0;
    vector<int> h(N + 1, 0), w(M + 1, 0);
    for (i = 1; i <= N; i++) {
      cin >> h[i];
      h[i] += h[i - 1];
    }
    for (i = 1; i <= M; i++) {
      cin >> w[i];
      w[i] += w[i - 1];
    }

    for (int j = 0; j <= M - 1; j++) {
      for (int k = j + 1; k <= M; k++) {
        int width = w[k] - w[j];
        int from = 0, to = 1;
        while (to <= N) {
          int height = h[to] - h[from];
          if (height == width)
            count++;
          if (height > width)
            from++;
          else
            to++;
        }
      }
    }

    cout << count << endl;
  }

  return 0;
}

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

int main() {
  int N, M;
  while (cin >> N >> M && !(N == 0 && M == 0)) {
    map<int, int> hor, ver;
    int h[1500], w[1500];
    for (int i = 0; i < N; i++) {
      cin >> h[i];
      int s = 0;
      for (int j = i; j >= 0; j--) {
        s += h[j];
        ver[s]++;
      }
    }
    for (int i = 0; i < M; i++) {
      cin >> w[i];
      int s = 0;
      for (int j = i; j >= 0; j--) {
        s += w[j];
        hor[s]++;
      }
    }

    int count = 0;
    for (map<int, int>::iterator p = ver.begin(); p != ver.end(); ++p)
      count += (p->second) * (hor[p->first]);

    cout << count << endl;
  }

  return 0;
}


#include <algorithm>
#include <array>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define rep(i, n) for (int(i) = 0; (i) < (n); ++(i))
#define rangeRep(i, n, m) for (int(i) = (n); (i) < (m); ++(i))

int intervalX[1500];
int intervalY[1500];
int intervalsX[1500 * 1500];
int intervalsY[1500 * 1500];

class Solve {
public:
  Solve(){};
  ~Solve(){};

  void solver() {
    int n, m;
    while (std::cin >> n >> m, n, m) {
      std::memset(intervalX, 0, sizeof(intervalX));
      std::memset(intervalY, 0, sizeof(intervalY));
      std::memset(intervalsX, 0, sizeof(intervalsX));
      std::memset(intervalsY, 0, sizeof(intervalsY));
      // input
      rep(i, n) { std::cin >> intervalX[i]; }
      rep(i, m) { std::cin >> intervalY[i]; }
      // All patterns enumeration
      int numOfIntervalsX = 0;
      int numOfIntervalsY = 0;
      int squares = 0;
      int edgeXNo = 0;
      int cnt = 0;
      rep(i, n) {
        rep(j, n - i) {
          rangeRep(k, j, j + i + 1) { intervalsX[cnt] += intervalX[k]; }
          ++cnt;
          ++numOfIntervalsX;
        }
      }
      cnt = 0;
      rep(i, m) {
        edgeXNo = 0;
        rep(j, m - i) {
          rangeRep(k, j, j + i + 1) { intervalsY[cnt] += intervalY[k]; }
          rep(k, numOfIntervalsX) {
            if (intervalsX[k] == intervalsY[cnt])
              squares++;
          }
          ++cnt;
        }
      }
      std::cout << squares << "\\n";
    }
    std::cout << std::endl;
  }

private:
};

int main() {
  Solve solve;
  solve.solver();
  return 0;
}

#include <algorithm>
#include <array>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define rep(i, n) for (int(i) = 0; (i) < (n); ++(i))
#define rangeRep(i, n, m) for (int(i) = (n); (i) < (m); ++(i))

int intervalX[1500];
int intervalY[1500];
int intervalsX[1500 * 1000];
int intervalsY[1500 * 1000];

class Solve {
public:
  Solve(){};
  ~Solve(){};

  void solver() {
    int n, m;
    while (std::cin >> n >> m, n, m) {
      std::memset(intervalX, 0, sizeof(intervalX));
      std::memset(intervalY, 0, sizeof(intervalY));
      std::memset(intervalsX, 0, sizeof(intervalsX));
      std::memset(intervalsY, 0, sizeof(intervalsY));
      // input
      rep(i, n) { std::cin >> intervalX[i]; }
      rep(i, m) { std::cin >> intervalY[i]; }
      // All patterns enumeration
      int squares = 0;
      int edgeXNo = 0;
      int cnt = 0;
      int maxLen = 0;
      rep(i, n) {
        rep(j, n - i) {
          int len = 0;
          rangeRep(k, j, j + i + 1) { len += intervalX[k]; }
          intervalsX[len]++;
          if (maxLen < len)
            maxLen = len;
          ++cnt;
        }
      }
      rep(i, m) {
        edgeXNo = 0;
        rep(j, m - i) {
          int len = 0;
          rangeRep(k, j, j + i + 1) { len += intervalY[k]; }
          intervalsY[len]++;
          if (maxLen < len)
            maxLen = len;
        }
      }
      rep(i, maxLen + 1) { squares += intervalsX[i] * intervalsY[i]; }
      std::cout << squares << std::endl;
    }
  }

private:
};

int main() {
  Solve solve;
  solve.solver();
  return 0;
}


#include <iostream>

int h[1000], w[1000];

int main() {
  int n, m;
  while (std::cin >> n >> m && (n || m)) {
    for (int i = 0; i < n; i++)
      std::cin >> h[i];
    for (int i = 0; i < m; i++)
      std::cin >> w[i];

    int cnt = 0;
    for (int sy = 0; sy < n; sy++) {
      for (int sx = 0; sx < m; sx++) {
        for (int y = 0; y < n - sy; y++) {
          for (int x = 0; x < m - sx; x++) {
            int wSum = 0, hSum = 0;
            for (int j = x; j <= x + sx; j++) {
              wSum += w[j];
            }
            for (int i = y; i <= y + sy; i++) {
              hSum += h[i];
            }
            if (wSum == hSum)
              cnt++;
          }
        }
      }
    }
    std::cout << cnt << std::endl;
  }

  return 0;
}

#include <cstring>
#include <iostream>

long long h[1500001], w[1500001];
long long hh[1500], ww[1500];

int main() {
  int n, m;
  while (std::cin >> n >> m && (n || m)) {
    std::memset(h, 0, sizeof(h));
    std::memset(w, 0, sizeof(w));

    int numX = 0, numY = 0;
    for (int i = 0; i < n; i++) {
      std::cin >> hh[i];
    }
    for (int i = 0; i < m; i++) {
      std::cin >> ww[i];
    }

    for (int sy = 0; sy < n; sy++) {
      for (int y = 0; y < n - sy; y++) {
        long long num = 0;
        for (int i = 0; i <= sy; i++) {
          num += hh[i + y];
        }
        h[num]++;
      }
    }

    for (int sx = 0; sx < m; sx++) {
      for (int x = 0; x < m - sx; x++) {
        int num = 0;
        for (int i = 0; i <= sx; i++) {
          num += ww[i + x];
        }
        w[num]++;
      }
    }

    long long cnt = 0;
    for (int i = 0; i < 1500001; i++) {
      cnt += h[i] * w[i];
    }

    std::cout << cnt << std::endl;
  }

  return 0;
}


#include <iostream>
using namespace std;
int main() {
  int i, j, k, s;
  int N, M;
  long long count = 0;
  while (1) {
    cin >> N >> M;
    if (N == 0)
      break;
    int h[N], w[M];
    for (i = 0; i < N; i++)
      cin >> h[i];
    for (i = 0; i < M; i++)
      cin >> w[i];
    int x = (N + 1) * N / 2;
    int y = (M + 1) * M / 2;
    int a[x];
    int b[y];
    k = 0;
    while (k < x) {
      for (i = 0; i < N; i++) {
        s = 0;
        for (j = i; j < N; j++) {
          s = s + h[j];
          a[k] = s;
          k++;
        }
      }
    }
    k = 0;
    while (k < y) {
      for (i = 0; i < N; i++) {
        s = 0;
        for (j = i; j < M; j++) {
          s = s + w[j];
          b[k] = s;
          k++;
        }
      }
    }
    for (i = 0; i < x; i++)
      for (j = 0; j < y; j++) {
        if (a[i] == b[j])
          count++;
      }
    cout << count << endl;
  }
  return 0;
}

#include <bits/stdc++.h>
using namespace std;
int H, W;
int h[1500], w[1500];

void solve() {
  for (int i = 0; i < H; i++)
    scanf("%d", &h[i]);
  for (int i = 0; i < W; i++)
    scanf("%d", &w[i]);

  int mxh = accumulate(h, h + H, 0);
  int mxw = accumulate(w, w + W, 0);

  vector<int> th(mxh + 1, 0), tw(mxw + 1, 0);

  for (int i = 0; i < H; i++) {
    int sum = 0;
    for (int j = i; j < H; j++) {
      sum += h[j];
      th[sum]++;
    }
  }
  for (int i = 0; i < W; i++) {
    int sum = 0;
    for (int j = i; j < W; j++) {
      sum += w[j];
      tw[sum]++;
    }
  }
  long long ans = 0;
  for (int i = 1; i <= min(mxh, mxw); i++) {
    ans += (long long)th[i] * tw[i];
  }

  printf("%d\\n", ans);
}
int main() {
  while (scanf("%d%d", &H, &W), H || W)
    solve();
}


#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <tuple>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<ll> V;
typedef complex<double> Point;

#define PI acos(-1.0)
#define EPS 1e-10
const ll INF = 1e12;
const ll MOD = 1e9 + 7;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, N) for (int i = 0; i < (N); i++)
#define ALL(s) (s).begin(), (s).end()
#define EQ(a, b) (abs((a) - (b)) < EPS)
#define EQV(a, b) (EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()))
#define fi first
#define se second
#define N_SIZE (1LL << 20)
#define NIL -1
#define MAX_N 100100

ll sq(ll num) { return num * num; }
ll mod_pow(ll x, ll n) {
  if (n == 0)
    return 1;
  if (n == 1)
    return x % MOD;
  ll res = sq(mod_pow(x, n / 2));
  res %= MOD;
  if (n % 2 == 1) {
    res *= x;
    res %= MOD;
  }
  return res;
}
ll mod_add(ll a, ll b) { return (a + b) % MOD; }
ll mod_sub(ll a, ll b) { return (a - b + MOD) % MOD; }
ll mod_mul(ll a, ll b) { return a * b % MOD; }

ll n, m;
vector<ll> sl[2];
ll h[2000];
ll w[2000];

int main() {
  while (cin >> n >> m && n + m) {
    sl[0].clear();
    sl[1].clear();
    h[0] = 0;
    w[0] = 0;
    rep(i, n) cin >> h[i + 1];
    rep(i, m) cin >> w[i + 1];
    rep(i, n + 1) h[i] += h[i - 1];
    rep(i, m + 1) w[i] += w[i - 1];
    rep(i, n + 1) {
      FOR(j, i + 1, n + 1) { sl[0].push_back(h[j] - h[i]); }
    }
    rep(i, m + 1) {
      FOR(j, i + 1, m + 1) { sl[1].push_back(w[j] - w[i]); }
    }
    ll ans = 0;
    for (auto it = sl[0].begin(); it != sl[0].end(); it++) {
      for (auto it2 = sl[1].begin(); it2 != sl[1].end(); it2++) {
        if (*it == *it2)
          ans++;
      }
    }
    cout << ans << endl;
  }
}

#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <tuple>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<ll> V;
typedef complex<double> Point;

#define PI acos(-1.0)
#define EPS 1e-10
const ll INF = 1e12;
const ll MOD = 1e9 + 7;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, N) for (int i = 0; i < (N); i++)
#define ALL(s) (s).begin(), (s).end()
#define EQ(a, b) (abs((a) - (b)) < EPS)
#define EQV(a, b) (EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()))
#define fi first
#define se second
#define N_SIZE (1LL << 20)
#define NIL -1
#define MAX_N 100100

ll sq(ll num) { return num * num; }
ll mod_pow(ll x, ll n) {
  if (n == 0)
    return 1;
  if (n == 1)
    return x % MOD;
  ll res = sq(mod_pow(x, n / 2));
  res %= MOD;
  if (n % 2 == 1) {
    res *= x;
    res %= MOD;
  }
  return res;
}
ll mod_add(ll a, ll b) { return (a + b) % MOD; }
ll mod_sub(ll a, ll b) { return (a - b + MOD) % MOD; }
ll mod_mul(ll a, ll b) { return a * b % MOD; }

ll n, m;
ll cnt[2][2000000];
ll h[2000];
ll w[2000];

int main() {
  while (cin >> n >> m && n + m) {
    rep(i, 2) rep(j, 2000000) cnt[i][j] = 0;
    h[0] = 0;
    w[0] = 0;
    rep(i, n) cin >> h[i + 1];
    rep(i, m) cin >> w[i + 1];
    rep(i, n + 1) h[i] += h[i - 1];
    rep(i, m + 1) w[i] += w[i - 1];
    rep(i, n + 1) {
      FOR(j, i + 1, n + 1) { cnt[0][h[j] - h[i]]++; }
    }
    rep(i, m + 1) {
      FOR(j, i + 1, m + 1) { cnt[1][w[j] - w[i]]++; }
    }
    ll ans = 0;
    rep(i, 2000000) { ans += (cnt[0][i] * cnt[1][i]); }
    cout << ans << endl;
  }
}


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

using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<ll, ll> P;
typedef vector<P> vP;

int main() {
  while (true) {

    int n, m;
    cin >> n >> m;
    if (n == 0 && m == 0)
      return 0;
    vi x(3009, 0);
    vi y(3009, 0);
    x[0] = 1;
    y[0] = 1;
    vi h(max(n, m) + 1, 0);
    int sum = 0;
    for (int i = 1; i <= n; i++) {
      int temp;
      cin >> temp;
      sum += temp;
      h[i] = sum;
    }
    for (int i = 0; i <= n; i++) {
      for (int j = i + 1; j <= n; j++) {
        x[h[j] - h[i]] += 1;
      }
    }
    sum = 0;
    for (int i = 1; i <= m; i++) {
      int temp;
      cin >> temp;
      sum += temp;
      h[i] = sum;
    }
    for (int i = 0; i <= m; i++) {
      for (int j = i + 1; j <= m; j++) {
        y[h[j] - h[i]] += 1;
      }
    }
    int ans = 0;
    for (int i = 1; i < 3009; i++) {
      ans += x[i] * y[i];
    }
    cout << ans << endl;
  }
  return 0;
}

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

using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<ll, ll> P;
typedef vector<P> vP;

int main() {
  while (true) {

    int n, m;
    cin >> n >> m;
    if (n == 0 && m == 0)
      return 0;
    vi x(3000009, 0);
    vi y(3000009, 0);
    x[0] = 1;
    y[0] = 1;
    vi h(max(n, m) + 1, 0);
    int sum = 0;
    for (int i = 1; i <= n; i++) {
      int temp;
      cin >> temp;
      sum += temp;
      h[i] = sum;
    }
    for (int i = 0; i <= n; i++) {
      for (int j = i + 1; j <= n; j++) {
        x[h[j] - h[i]] += 1;
      }
    }
    sum = 0;
    for (int i = 1; i <= m; i++) {
      int temp;
      cin >> temp;
      sum += temp;
      h[i] = sum;
    }
    for (int i = 0; i <= m; i++) {
      for (int j = i + 1; j <= m; j++) {
        y[h[j] - h[i]] += 1;
      }
    }
    int ans = 0;
    for (int i = 1; i < 3000009; i++) {
      ans += x[i] * y[i];
    }
    cout << ans << endl;
  }
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int(i) = (0); (i) < (int)(n); ++(i))
using ll = long long;

int main() {

  int n, m;
  while (cin >> n >> m and (n or m)) {

    int h[1001], w[1001];
    vector<int> hh(1500001, 0), ww(1500001, 0);

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

    rep(i, n) {
      int s = 0;
      for (int j = i; j < n; ++j) {
        s += h[j];
        hh[s]++;
      }
    }

    rep(i, m) {
      int s = 0;
      for (int j = i; j < m; ++j) {
        s += w[j];
        ww[s]++;
      }
    }

    int ans = 0;
    rep(i, 1500001) { ans += hh[i] * ww[i]; }

    cout << ans << endl;
  }

  return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int(i) = (0); (i) < (int)(n); ++(i))
using ll = long long;

int main() {

  int n, m;
  while (cin >> n >> m and (n or m)) {

    int h[1501], w[1501];
    vector<int> hh(1500001, 0), ww(1500001, 0);

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

    rep(i, n) {
      int s = 0;
      for (int j = i; j < n; ++j) {
        s += h[j];
        hh[s]++;
      }
    }

    rep(i, m) {
      int s = 0;
      for (int j = i; j < m; ++j) {
        s += w[j];
        ww[s]++;
      }
    }

    int ans = 0;
    rep(i, 1500001) { ans += hh[i] * ww[i]; }

    cout << ans << endl;
  }

  return 0;
}


#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<ll> V;
typedef complex<double> Point;

#define PI acos(-1.0)
#define EPS 1e-10
const ll INF = (1LL << 31) - 1;
const ll MOD = 1e9 + 7;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, N) for (int i = 0; i < (N); i++)
#define ALL(s) (s).begin(), (s).end()
#define EQ(a, b) (abs((a) - (b)) < EPS)
#define EQV(a, b) (EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()))
#define fi first
#define se second
#define N_SIZE (1LL << 20)
#define NIL -1
#define MAX_N 100100 * 3

int n[2];
ll cnt[2][2000];
ll h[2][2000];

int main() {
  while (cin >> n[0] >> n[1]) {
    if (n[0] + n[1] == 0) {
      return 0;
    }
    REP(i, 2) {
      fill(cnt[i], cnt[i] + 2000, 0);
      fill(h[i], h[i] + 2000, 0);
    }
    REP(i, 2) {
      FOR(j, 1, n[i] + 1) cin >> h[i][j];
      REP(j, n[i]) h[i][j + 1] += h[i][j];
    }
    REP(i, 2) {
      REP(j, n[i]) {
        FOR(k, j + 1, n[i] + 1) { cnt[i][h[i][k] - h[i][j]]++; }
      }
    }
    ll ans = 0;
    REP(i, 1001) { ans += (cnt[0][i] * cnt[1][i]); }
    cout << ans << endl;
  }
}

#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<ll> V;
typedef complex<double> Point;

#define PI acos(-1.0)
#define EPS 1e-10
const ll INF = (1LL << 31) - 1;
const ll MOD = 1e9 + 7;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, N) for (int i = 0; i < (N); i++)
#define ALL(s) (s).begin(), (s).end()
#define EQ(a, b) (abs((a) - (b)) < EPS)
#define EQV(a, b) (EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()))
#define fi first
#define se second
#define N_SIZE (1LL << 20)
#define NIL -1
#define MAX_N 100100 * 3

int n[2];
ll cnt[2][1500010];
ll h[2][1500010];

int main() {
  while (cin >> n[0] >> n[1]) {
    if (n[0] + n[1] == 0) {
      return 0;
    }
    REP(i, 2) {
      fill(cnt[i], cnt[i] + 1500010, 0);
      fill(h[i], h[i] + 1500010, 0);
    }
    REP(i, 2) {
      FOR(j, 1, n[i] + 1) cin >> h[i][j];
      REP(j, n[i]) h[i][j + 1] += h[i][j];
    }
    REP(i, 2) {
      REP(j, n[i]) {
        FOR(k, j + 1, n[i] + 1) { cnt[i][h[i][k] - h[i][j]]++; }
      }
    }
    ll ans = 0;
    REP(i, 1500010) { ans += (cnt[0][i] * cnt[1][i]); }
    cout << ans << endl;
  }
}


#include <iostream>

using namespace std;

int main() {
  int n, m, h[1501], w[1501], s, t, u;

  while (cin >> n >> m && (n + m)) {
    t = 0;
    for (int i = 0; i < n; i++) {
      cin >> h[i];
    }
    for (int i = 0; i < m; i++) {
      cin >> w[i];
    }

    for (int k = 0; k < n; k++) {
      u = 0;
      for (int i = k; i < n; i++) {
        s = 0;
        u += h[i];
        for (int j = 0; j < m; j++) {
          s += w[j];
          if (u == s) {
            t++;
            s = 0;
          } else if (u < s) {
            s = 0;
            if (u == w[j]) {
              t++;
            } else if (u > w[j]) {
              s = w[j];
            }
          }
        }
      }
    }

    cout << t << endl;
  }

  return 0;
}

#include <iostream>
#include <map>

using namespace std;

int main() {
  int n, m, h[1501], w[1501], s, t;

  while (cin >> n >> m && (n + m)) {
    map<int, int> hmp1;
    map<int, bool> hmp2;

    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++) {
      s = h[i];
      if (hmp2[s]) {
        hmp1[s]++;
      } else {
        hmp2[s] = true;
        hmp1[s] = 1;
      }
      for (int j = i + 1; j < n; j++) {
        s += h[j];
        if (hmp2[s]) {
          hmp1[s]++;
        } else {
          hmp2[s] = true;
          hmp1[s] = 1;
        }
      }
    }

    t = 0;
    for (int i = 0; i < m; i++) {
      s = w[i];
      if (hmp2[s]) {
        t += hmp1[s];
      }
      for (int j = i + 1; j < m; j++) {
        s += w[j];
        if (hmp2[s]) {
          t += hmp1[s];
        }
      }
    }

    cout << t << endl;
  }

  return 0;
}


#include <algorithm>
#include <iostream>
#include <stdio.h>
using namespace std;

int main() {

  int N1, N2;

  cin >> N1;
  while (N1--) {
    int R;
    cin >> N2 >> R;
    int hm = 0;
    int y, x = 0;
    int n[100003] = {};
    for (int i = 0; i < N2; i++) {
      cin >> y;
      if (i > 0)
        n[i] = y - x;
      x = y;
    }

    for (int i = 0; i < N2; i++)
      for (int j = N2 - 1; j > i; j--)
        if (n[j] > n[i]) {
          int t = n[j];
          n[j] = n[i];
          n[i] = t;
        }

    long long int s = 0;
    for (int i = 0; i < N2 - 1; i++) {
      if (i >= R - 1)
        s += n[i];
    }
    cout << s << endl;
  }
  return 0;
}

#include <algorithm>
#include <iostream>
#include <stdio.h>
using namespace std;

int main() {

  int N1, N2;

  cin >> N1;
  while (N1--) {
    int R;
    cin >> N2 >> R;
    int hm = 0;
    int y, x = 0;
    int n[100003] = {};
    for (int i = 0; i < N2; i++) {
      cin >> y;
      if (i > 0)
        n[i] = y - x;
      x = y;
    }
    /*
    for(int i=0;i<N2;i++)
            for(int j=N2-1;j>i;j--)
                    if(n[j]>n[i]){int t=n[j];n[j]=n[i];n[i]=t;}
            */
    sort(n, n + N2);
    long long int s = 0;
    int W = 0;
    for (int i = N2 - 1; i >= 0; i--) {
      if (W >= R - 1)
        s += n[i];
      W++;
    } // cout<<n[i]<<endl;W++;}
    cout << s << endl;
  }
  return 0;
}


#include <iostream>

using namespace std;

// 昇順挿入ソート(ALDS1_1_A)
void UpperSort(int DataNum, int *Data) {
  int v = 0, j = 0;
  for (int i = 1; i < DataNum; ++i) {
    v = Data[i]; // ソート
    j = i - 1;
    while (j >= 0 && Data[j] > v) {
      Data[j + 1] = Data[j];
      --j;
    }
    Data[j + 1] = v;
  }
}

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

  int DataSetNum = 0;
  int HouseNum = 0;       // 家の数
  int GeneNum = 0;        // 発電機の数
  int *HousePos;          // 家の位置
  int *CableLength;       // 送電線長さ
  int AllCableLength = 0; // 全線距離

  cin >> DataSetNum; // データセット数取得
  for (int datanum = 0; datanum < DataSetNum; ++datanum) {
    // 各データ入力
    cin >> HouseNum;
    cin >> GeneNum;
    if (HouseNum <= GeneNum) { // 家より発電機が多ければ距離0
      int dummy = 0;           // データ読み捨て
      for (int i = 0; i < HouseNum; ++i) {
        cin >> dummy;
      }
      cout << 0 << endl;
      continue;
    }
    HousePos = new int[HouseNum];
    for (int i = 0; i < HouseNum; ++i) {
      cin >> HousePos[i];
    }
    CableLength = new int[HouseNum - 1];
    for (int i = 0; i < HouseNum - 1; ++i) {
      CableLength[i] = HousePos[i + 1] - HousePos[i];
    }
    AllCableLength = HousePos[HouseNum - 1] - HousePos[0]; // 全線距離

    // 距離ソート
    UpperSort(HouseNum - 1, CableLength);
    // 長いほうから削る
    for (int i = 0; i < GeneNum - 1; ++i) {
      AllCableLength -= CableLength[HouseNum - i - 2];
      CableLength[HouseNum - i - 2] = 0;
    }
    // 出力
    cout << AllCableLength << endl;

    delete[] CableLength;
    delete[] HousePos;
  }

  return 0;
}

#include <algorithm>
#include <iostream>

using namespace std;

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

  int DataSetNum = 0;
  int HouseNum = 0;       // 家の数
  int GeneNum = 0;        // 発電機の数
  int *HousePos;          // 家の位置
  int *CableLength;       // 送電線長さ
  int AllCableLength = 0; // 全線距離

  cin >> DataSetNum; // データセット数取得
  for (int datanum = 0; datanum < DataSetNum; ++datanum) {
    // 各データ入力
    cin >> HouseNum;
    cin >> GeneNum;
    if (HouseNum <= GeneNum) { // 家より発電機が多ければ距離0
      int dummy = 0;           // データ読み捨て
      for (int i = 0; i < HouseNum; ++i) {
        cin >> dummy;
      }
      cout << 0 << endl;
      continue;
    }
    HousePos = new int[HouseNum];
    for (int i = 0; i < HouseNum; ++i) {
      cin >> HousePos[i];
    }
    CableLength = new int[HouseNum - 1];
    for (int i = 0; i < HouseNum - 1; ++i) {
      CableLength[i] = HousePos[i + 1] - HousePos[i];
    }
    AllCableLength = HousePos[HouseNum - 1] - HousePos[0]; // 全線距離

    // 距離ソート
    sort(&CableLength[0], &CableLength[0] + HouseNum - 1);
    // 長いほうから削る
    for (int i = 0; i < GeneNum - 1; ++i) {
      AllCableLength -= CableLength[HouseNum - i - 2];
      CableLength[HouseNum - i - 2] = 0;
    }
    // 出力
    cout << AllCableLength << endl;

    delete[] CableLength;
    delete[] HousePos;
  }

  return 0;
}


#include <algorithm>
#include <iostream>
#include <math.h>
using namespace std;
int T, N, K, X[1000], A[1000];
int main() {
  cin >> T;
  for (int t = 0; t < T; ++t) {
    int sum = 0;
    cin >> N >> K;
    for (int i = 0; i < N; ++i) {
      cin >> X[i];
    }
    for (int i = 0; i + 1 < N; ++i) {
      A[i] = X[i + 1] - X[i];
    }
    sort(A, A + N);
    for (int j = 0; j < N - K + 1; ++j) {
      sum = sum + A[j];
    }
    cout << sum << endl;
  }
  return 0;
}

#include <algorithm>
#include <iostream>
#include <math.h>
using namespace std;
int T, N, K, X[1000000 + 10], A[1000000 + 10];
int main() {
  cin >> T;
  for (int t = 0; t < T; ++t) {
    int sum = 0;
    cin >> N >> K;
    for (int i = 0; i < N; ++i) {
      cin >> X[i];
    }
    for (int i = 0; i < N - 1; ++i) {
      A[i] = X[i + 1] - X[i];
    }
    sort(A, A + N - 1);
    for (int j = 0; j < N - K; ++j) {
      sum = sum + A[j];
    }
    cout << sum << endl;
  }
  return 0;
}


#include <algorithm>
#include <functional>
#include <iostream>

using namespace std;

int main() {
  int t;
  cin >> t;
  for (int l = 0; l < t; l++) {
    int n, k;
    cin >> n >> k;
    if (k <= n) {
      cout << "n=" << n << endl;
      int sum = 0;
      int home[100001];
      for (int j = 1; j <= n; j++) {
        cin >> home[j];
      }
      sum = home[n] - home[1];
      int dis[100000];
      for (int i = 1; i <= n - 1; i++) {
        dis[i] = home[i + 1] - home[i];
      }
      std::sort(dis + 1, dis + n, greater<int>());
      for (int i = 1; i <= k - 1; i++) {
        sum -= dis[i];
      }
      cout << sum << endl;
    } else {
      cout << "0" << endl;
    }
  }
  return 0;
}

#include <algorithm>
#include <functional>
#include <iostream>

using namespace std;

int main() {
  int t;
  cin >> t;
  for (int l = 0; l < t; l++) {
    int n, k;
    cin >> n >> k;
    if (k < n) {
      int sum = 0;
      int home[100001];
      for (int j = 1; j <= n; j++) {
        cin >> home[j];
      }
      sum = home[n] - home[1];
      int dis[100000];
      for (int i = 1; i <= n - 1; i++) {
        dis[i] = home[i + 1] - home[i];
      }
      std::sort(dis + 1, dis + n, greater<int>());
      for (int i = 1; i <= k - 1; i++) {
        sum -= dis[i];
      }
      cout << sum << endl;
    } else {
      for (int i = 0; i < n; i++) {
        int a;
        cin >> a;
      }
      cout << "0" << endl;
    }
  }
  return 0;
}


#include <algorithm>
#include <iostream>
#include <math.h>
#include <queue>
#include <string>
#include <utility>
#include <vector>

#define MAX 500

using namespace std;

int main() {
  int t;
  cin >> t;
  for (int i = 0; i < t; i++) {
    int n, k;
    cin >> n >> k;
    vector<int> x(n);
    vector<int> y(n - 1);
    for (int j = 0; j < n; j++) {
      cin >> x[j];
    }
    /*if (n <= k) {
        cout << 0 << endl;
        continue;
    }*/
    for (int j = 0; j < n - 1; j++) {
      y[j] = x[j + 1] - x[j];
    }
    sort(y.begin(), y.end());
    int ans = x[n - 1] - x[0];
    for (int j = 0; j < k - 1; j++) {
      ans -= y[n - j - 2];
    }
    cout << ans << endl;
  }
}

#include <algorithm>
#include <iostream>
#include <math.h>
#include <queue>
#include <string>
#include <utility>
#include <vector>

using namespace std;

int main() {
  int t;
  cin >> t;
  for (int i = 0; i < t; i++) {
    int n, k;
    cin >> n >> k;
    vector<int> x(n);
    vector<int> y(n - 1);
    for (int j = 0; j < n; j++) {
      cin >> x[j];
    }
    if (n <= k) {
      cout << 0 << endl;
      continue;
    }
    for (int j = 0; j < n - 1; j++) {
      y[j] = x[j + 1] - x[j];
    }
    sort(y.begin(), y.end());
    int ans = x[n - 1] - x[0];
    for (int j = 0; j < k - 1; j++) {
      ans -= y[n - j - 2];
    }
    cout << ans << endl;
  }
}


#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int n, k, a[1000000];
vector<int> vec;
int main() {
  while (true) {
    vec.clear();
    cin >> n >> k;
    if (n == 0 && k == 0) {
      break;
    }
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }
    sort(a, a + n);
    for (int i = 0; i < n - 1; i++) {
      vec.push_back(a[i + 1] - a[i]);
    }
    sort(vec.begin(), vec.end());
    int sum = 0, cnt = 1;
    for (int i = vec.size() - 1; i >= 0; i--) {
      if (cnt >= k)
        sum += vec[i];
      cnt++;
    }
    cout << sum << endl;
  }
  return 0;
}

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int n, k, a[1000000];
vector<int> vec;
int main() {
  int t;
  cin >> t;
  for (int i = 0; i < t; i++) {
    cin >> n >> k;
    vec.clear();
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }
    sort(a, a + n);
    for (int i = 0; i < n - 1; i++) {
      vec.push_back(a[i + 1] - a[i]);
    }
    sort(vec.begin(), vec.end());
    int sum = 0, cnt = 1;
    for (int i = vec.size() - 1; i >= 0; i--) {
      if (cnt >= k)
        sum += vec[i];
      cnt++;
    }
    cout << sum << endl;
  }
  return 0;
}


#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)
#define EACH(t, i, c) for (t::iterator i = (c).begin(); i != (c).end(); ++i)
const double EPS = 1e-10;
const double PI = acos(-1.0);

const int MAX = INT_MAX / 20;
int main() {
  int t;
  cin >> t;
  REP(data, t) {
    int n, k;
    cin >> n >> k;
    vi x(n);
    REP(i, n) cin >> x[i];

    vvi dp(k, vi(n + 1, MAX));
    dp[0][0] = 0;

    REP(i, k) {
      FOR(j, 1, n + 1) {
        dp[i][j] = min(dp[i][j - 1] + (j == 1 ? 0 : x[j - 1] - x[j - 2]),
                       (i > 0 ? dp[i - 1][j - 1] : MAX));
      }
    }

    int best = MAX;
    REP(i, k) { best = min(best, dp[i][n]); }
    cout << best << 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)
#define EACH(t, i, c) for (t::iterator i = (c).begin(); i != (c).end(); ++i)
const double EPS = 1e-10;
const double PI = acos(-1.0);

const int MAX = INT_MAX / 20;
int main() {
  int t;
  cin >> t;
  REP(data, t) {
    int n, k;
    cin >> n >> k;
    vi x(n);
    REP(i, n) cin >> x[i];

    vi d(n - 1);
    ll total = 0;
    REP(i, n - 1) {
      d[i] = x[i + 1] - x[i];
      total += d[i];
    }

    sort(ALL(d), greater<int>());

    REP(i, min(k - 1, n - 1)) { total -= d[i]; }

    cout << total << endl;
  }
}


#include <algorithm>
#include <iostream>
using namespace std;
void solve() {
  int n, k;
  cin >> n >> k;
  int a[110];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  if (k >= n) {
    cout << "0\\n";
    return;
  }
  int a1[110];
  for (int i = 0; i < n - 1; i++)
    a1[i] = a[i + 1] - a[i];
  sort(a1, a1 + (n - 1));
  int ans = a[n - 1] - a[0];
  for (int i = 0; i < k - 1; i++) {
    ans -= a1[n - i - 2];
  }
  cout << ans << endl;
}
int main() {
  int d;
  cin >> d;
  for (int i = 0; i < d; i++)
    solve();
}

#include <algorithm>
#include <iostream>
using namespace std;
void solve() {
  int n, k;
  cin >> n >> k;
  int a[114514];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  if (k >= n) {
    cout << "0\\n";
    return;
  }
  int a1[114514];
  for (int i = 0; i < n - 1; i++)
    a1[i] = a[i + 1] - a[i];
  sort(a1, a1 + (n - 1));
  int ans = a[n - 1] - a[0];
  for (int i = 0; i < k - 1; i++) {
    ans -= a1[n - i - 2];
  }
  cout << ans << endl;
}
int main() {
  int d;
  cin >> d;
  for (int i = 0; i < d; i++)
    solve();
}


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

int main() {
  int m, n, k;
  vector<int> data;
  vector<int> x;
  vector<int> sum;
  int dis[MAX] = {0};
  cin >> m;
  data.resize(m);
  for (int i = 0; i < m; i++) {
    // sum=0;
    cin >> n >> k;
    x.resize(n);
    sum.resize(n, 0);
    // dis.resize(n-1);
    for (int j = 0; j < n; j++) {
      cin >> x[j];
      if (j > 0)
        dis[j - 1] = x[j] - x[j - 1];
    }
    sort(dis, dis + n - 1);
    // int sum=0;
    for (int p = 0; p < n - k; p++) {
      sum[i] += dis[p];
    }
    // cout<<sum<<endl;
  }
  for (int i = 0; i < m; i++)
    cout << sum[i] << endl;
  return 0;
}

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

int main() {
  int m, n, k;
  // vector<int> data;
  vector<int> x;
  vector<int> sum;
  int dis[MAX] = {0};
  cin >> m;
  sum.resize(m, 0);
  // data.resize(m);
  for (int i = 0; i < m; i++) {
    // sum=0;
    cin >> n >> k;
    x.resize(n, 0);

    for (int j = 0; j < n; j++) {
      cin >> x[j];
      if (j > 0)
        dis[j - 1] = x[j] - x[j - 1];
    }
    sort(dis, dis + n - 1);
    // int sum=0;
    for (int p = 0; p < n - k; p++) {
      sum[i] += dis[p];
    }
    // cout<<sum<<endl;
  }
  for (int i = 0; i < m; i++)
    cout << sum[i] << endl;
  return 0;
}


#include <algorithm>
#include <iostream>

using namespace std;

int main() {
  int c;
  cin >> c;
  for (int dataCount = 0; dataCount < c; dataCount++) {
    int n, k;
    cin >> n >> k;
    int x[n];
    for (int i = 0; i < n; i++) {
      cin >> x[i];
    }
    if (n <= k) {
      cout << 0 << endl;
      continue;
    }
    int distance[n - 1];
    for (int i = 0; i < n - 1; i++) {
      distance[i] = x[i + 1] - x[i];
    }
    bool done[n - 1];
    fill(done, done + (n - 1), false);
    int sum = 0;
    for (int i = 0; i < k - 1; i++) {
      int maxVal = -1;
      int maxIndex = -1;
      for (int j = 0; j < n - 1; j++) {
        if (done[j])
          continue;
        if (maxVal < distance[j]) {
          maxVal = distance[j];
          maxIndex = j;
        }
      }
      sum += maxVal;
      done[maxIndex] = true;
    }
    cout << x[n - 1] - x[0] - sum << endl;
  }
  return 0;
}

#include <algorithm>
#include <iostream>

using namespace std;

int main() {
  int c;
  cin >> c;
  for (int dataCount = 0; dataCount < c; dataCount++) {
    int n, k;
    cin >> n >> k;
    int x[n];
    for (int i = 0; i < n; i++) {
      cin >> x[i];
    }
    if (n <= k) {
      cout << 0 << endl;
      continue;
    }
    int distance[n - 1];
    for (int i = 0; i < n - 1; i++) {
      distance[i] = x[i + 1] - x[i];
    }
    bool done[n - 1];
    fill(done, done + (n - 1), false);
    int sum = 0;
    sort(distance, distance + (n - 1), greater<>());
    for (int i = 0; i < k - 1; i++) {
      sum += distance[i];
    }
    cout << x[n - 1] - x[0] - sum << endl;
  }
  return 0;
}


#include <stdio.h>
#include <string.h>
#define MAXNUM 1000000

int main(void) {
  int e[MAXNUM - 1], x[MAXNUM], t, n, k, i, j, l, max_i, sum;
  scanf("%d", &t);
  for (i = 0; i < t; i++) {
    scanf("%d%d", &n, &k);
    k--;
    for (j = 0; j < n; j++)
      scanf("%d", &x[j]);
    memset(e, 0, sizeof(e));
    sum = 0;
    if (n <= k + 1) {
      printf("0\\n");
      continue;
    }
    for (j = 0; j < k; j++) {
      max_i = -1;
      for (l = 0; l < n - 1; l++)
        if (!e[l] && (max_i == -1 || x[max_i + 1] - x[max_i] < x[l + 1] - x[l]))
          max_i = l;
      e[max_i] = 1;
      sum += x[max_i + 1] - x[max_i];
    }
    printf("%d\\n", x[n - 1] - x[0] - sum);
  }
  return 0;
}

#include <queue>
#include <stdio.h>
#include <string.h>
#define MAXNUM 100000

using namespace std;

int main(void) {
  int e[MAXNUM - 1], x[MAXNUM], t, n, k, i, j, l, max_i, sum;
  priority_queue<int> que;
  scanf("%d", &t);
  for (i = 0; i < t; i++) {
    scanf("%d%d", &n, &k);
    k--;
    for (j = 0; j < n; j++)
      scanf("%d", &x[j]);
    memset(e, 0, sizeof(e));
    if (n <= k + 1) {
      printf("0\\n");
      continue;
    }
    for (j = 0; j < n - 1; j++)
      que.push(x[j + 1] - x[j]);
    sum = 0;
    for (j = 0; j < k; j++) {
      sum += que.top();
      que.pop();
    }
    while (que.size())
      que.pop();
    printf("%d\\n", x[n - 1] - x[0] - sum);
  }
  return 0;
}


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

using namespace std;

int main() {
  int t;

  cin >> t;

  for (int i = 0; i < t; ++i) {
    int n, k;
    vector<int> x;
    vector<int> distance;
    int temp;
    int total = 0;

    cin >> n >> k;

    for (int j = 0; j < n; ++j) {
      cin >> temp;
      x.push_back(temp);
    }

    total = temp - x[0];

    for (int j = 0; j < n - 1; ++j)
      distance.push_back(x[j + 1] - x[j]);

    sort(distance.begin(), distance.end(), greater<int>());

    for (int j = 0; j < k - 1; ++j)
      total -= distance[j];

    cout << total << endl;
  }

  return 0;
}

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

using namespace std;

int main() {
  int t;

  cin >> t;

  for (int i = 0; i < t; ++i) {
    int n, k;
    vector<int> x;
    vector<int> distance;
    int temp;
    int total = 0;

    cin >> n >> k;

    for (int j = 0; j < n; ++j) {
      cin >> temp;
      x.push_back(temp);
    }

    if (n <= k)
      cout << 0 << endl;
    else {
      total = temp - x[0];

      for (int j = 0; j < n - 1; ++j)
        distance.push_back(x[j + 1] - x[j]);

      sort(distance.begin(), distance.end(), greater<int>());

      for (int j = 0; j < k - 1; ++j)
        total -= distance[j];

      cout << total << endl;
    }
  }
  return 0;
}


#include <bits/stdc++.h>

#define loop(i, a, b) for (int i = (a); i < int(b); i++)
#define rep(i, b) loop(i, 0, b)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()

using namespace std;

typedef vector<int> vi;

int main() {
  int t;
  cin >> t;
  rep(_, t) {
    int n, k;
    cin >> n >> k;
    vi x(n);
    rep(i, n) cin >> x[i];
    sort(all(x));
    vi d(n - 1);
    int s = 0;
    rep(i, n - 1) {
      d[i] = x[i + 1] - x[i];
      s += d[i];
    }
    sort(rall(d));

    rep(i, k - 1) { s -= d[i]; }
    s = max(0, s);
    cout << s << endl;
  }
}

#include <bits/stdc++.h>

#define loop(i, a, b) for (int i = (a); i < int(b); i++)
#define rep(i, b) loop(i, 0, b)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()

using namespace std;

typedef vector<int> vi;

int main() {
  int t;
  cin >> t;
  rep(_, t) {
    int n, k;
    cin >> n >> k;
    vi x(n);
    rep(i, n) cin >> x[i];
    sort(all(x));
    vi d(n - 1);
    int s = 0;
    rep(i, n - 1) {
      d[i] = x[i + 1] - x[i];
      s += d[i];
    }
    sort(rall(d));

    int end = min(k - 1, n - 1);

    rep(i, end) { s -= d[i]; }
    s = max(0, s);
    cout << s << endl;
  }
}


#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define SIZE 1000
#define INF 1000000005LL

using namespace std;
typedef long long int ll;
typedef pair<int, int> P;

int n;
ll M;
ll T[SIZE];
ll K;

bool check(ll m) {
  printf("=%d=", m);
  for (int a = 0; a < n; a++) {
    if (T[a] > m)
      continue;
    for (int b = a + 1; b < n; b++) {
      if (T[a] + T[b] > m)
        continue;
      for (int c = b + 1; c < n; c++) {
        if (T[a] + T[b] + T[c] > m)
          continue;
        for (int d = c + 1; d < n; d++) {
          if (T[a] + T[b] + T[c] + T[d] == m)
            return true;
          printf("(%d)", T[a] + T[b] + T[c] + T[d]);
        }
      }
    }
  }
  return false;
}

bool solve() {
  ll l = 0, m, r;
  scanf("%d %lld", &n, &M);
  if (n == 0)
    return false;
  r = M + 1;
  for (int i = 0; i < n; i++)
    scanf("%lld", &T[i]);
  while (l + 1 < r) {
    m = (l + r) / 2;
    if (check(m))
      l = m;
    else
      r = m;
  }
  printf("%lld\\n", r);
  return true;
}

int main() {
  while (solve()) {
  };
  return 0;
}

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define SIZE 100005
#define INF 1000000005LL

using namespace std;
typedef long long int ll;
typedef pair<int, int> P;

ll K;
ll T[SIZE];
ll S[SIZE];

void solve(int n, int k) {
  if (n <= k) {
    printf("0\\n");
    return;
  } else if (k == 1) {
    printf("%lld\\n", T[n - 1] - T[0]);
    return;
  }
  ll road = T[n - 1] - T[0];
  for (int i = 1; i < n; i++) {
    S[i - 1] = T[i] - T[i - 1];
  }
  sort(S, S + n - 1);
  for (int i = 0; i < (k - 1); i++) {
    road -= S[n - 2 - i];
  }
  printf("%lld\\n", road);
}

void imain() {
  int n, k;
  scanf("%d %d", &n, &k);
  for (int i = 0; i < n; i++)
    scanf("%lld", &T[i]);
  solve(n, k);
}

int main() {
  int N;
  scanf("%d", &N);
  for (int i = 0; i < N; i++) {
    imain();
  }
}


#include <bits/stdc++.h>
typedef long long LL;
#define SORT(c) sort((c).begin(), (c).end())

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

using namespace std;
int main(void) {
  int t;
  cin >> t;
  while (t--) {
    int n, k;
    cin >> n >> k;
    vector<int> dist;
    int pre;
    dist.push_back(0);
    REP(i, n) {
      int tmp;
      cin >> tmp;
      if (i)
        dist.push_back(tmp - pre);
      pre = tmp;
    }
    SORT(dist);
    REP(i, n - 1) dist[i + 1] += dist[i];
    cout << dist[n - k] << endl;
  }
  return 0;
}

#include <bits/stdc++.h>
typedef long long LL;
#define SORT(c) sort((c).begin(), (c).end())

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

using namespace std;
int main(void) {
  int t;
  cin >> t;
  while (t--) {
    int n, k;
    cin >> n >> k;
    vector<int> dist;
    int pre;
    dist.push_back(0);
    REP(i, n) {
      int tmp;
      cin >> tmp;
      if (i)
        dist.push_back(tmp - pre);
      pre = tmp;
    }
    SORT(dist);
    REP(i, n - 1) dist[i + 1] += dist[i];
    cout << dist[max(n - k, 0)] << endl;
  }
  return 0;
}


#include <cstdio>
int main() {
  int i, t;
  scanf("%d", &t);
  for (i = 0; i < t; i++) {
    int s, n, k;
    scanf("%d %d", &n, &k);
    if (k >= n) {
      printf("0\\n");
    } else {
      int x[n + 1];
      int cnt;
      for (cnt = 1; cnt <= n; cnt++) {
        scanf("%d", &x[cnt]);
      }
      // データの取得

      // X[m]=x(m+1)-x(m) (m=1,,,,n-1)を取得する
      int m, X[n];
      for (m = 1; m <= n - 1; m++) {
        X[m] = x[m + 1] - x[m];
      }

      // X[m]を降順にソート
      int i, j;
      for (j = 0; j < n; j++) {
        for (i = 1; i <= n - 2; i++) {
          if (X[i + 1] > X[i]) {
            int temp = X[i];
            X[i] = X[i + 1];

            X[i + 1] = temp;
          }
        }
      }

      // k台の発電機により、k-1のエリアと両端には電線を引かなくてよい
      // ゆえにもとめる電線の長さLは
      // L=(x(n)-x(1)) - (X(1)+X(2)+,,,X(k-1)) である。

      int l1, l2 = {0};
      l1 = x[n] - x[1];
      for (s = 1; s <= k - 1; s++) {
        l2 = l2 + X[s];
      }
      int L;
      L = l1 - l2;

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

  return 0;
}

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

int main() {
  int i, t;
  scanf("%d", &t);
  for (i = 0; i < t; i++) {
    int s, n, k;
    scanf("%d %d", &n, &k);
    int x[n];
    int cnt;
    for (cnt = 1; cnt <= n; cnt++) {
      scanf("%d", &x[cnt]);
    }
    // データの取得

    // X[m]=x(m+1)-x(m) (m=1,,,,n-1)を取得する
    int m, X[n];
    X[0] = 0;
    for (m = 1; m <= n - 1; m++) {
      X[m] = x[m + 1] - x[m];
    }

    sort(X, X + n);
    // k台の発電機により、k-1のエリアと両端には電線を引かなくてよい
    // ゆえにもとめる電線の長さLは
    // L=(x(n)-x(1)) - (X(1)+X(2)+,,,X(k-1)) である。

    int l1, l2 = {0};
    l1 = x[n] - x[1];
    for (s = 1; s <= k - 1; s++) {
      l2 = l2 + X[n - s];
    }
    int L;
    L = l1 - l2;
    if (n <= k) {
      printf("0\\n");
    } else {
      printf("%d\\n", L);
    }
  }
  return 0;
}


#include <algorithm>
#include <iostream>

using namespace std;

int map[50000], Min, n, k;

void search(int sum, int cnt, int i) {
  if (!sum) {
    Min = sum;
    return;
  }
  if (cnt == k) {
    if (Min > sum)
      Min = sum;
    return;
  }
  for (int j = i; j < n; ++j) {
    ::search(sum - map[j], cnt + 1, j + 1);
  }
}

int main(void) {
  int t;
  cin >> t;
  while (t--) {
    cin >> n >> k;
    --n;
    int prev, now, sum = 0;
    cin >> prev;
    for (int i = 0; i < n; ++i) {
      cin >> now;
      map[i] = now - prev;
      sum += map[i];
      prev = now;
    }
    Min = sum;
    search(sum, 1, 0);
    cout << Min << endl;
  }
  return 0;
}

#include <algorithm>
#include <iostream>

using namespace std;

int map[100001];

int main(void) {
  int t;
  cin >> t;
  while (t--) {
    int n, k;
    cin >> n >> k;
    --n;
    int prev, now;
    cin >> prev;
    for (int i = 0; i < n; ++i) {
      cin >> now;
      map[i] = now - prev;
      prev = now;
    }
    sort(map, map + n);
    n = n - k + 1;
    long sum = 0;
    for (int i = 0; i < n; ++i)
      sum += map[i];
    cout << sum << endl;
  }
  return 0;
}