Problem:
Problem F: Computation of Minimum Length of Pipeline

The Aizu Wakamatsu city office decided to lay a hot water pipeline covering the whole area of the city to heat houses. The pipeline starts from some hot springs and connects every district in the city. The pipeline can fork at a hot spring or a district, but no cycle is allowed. The city office wants to minimize the length of pipeline in order to build it at the least possible expense.

Write a program to compute the minimal length of the pipeline. The program reads an input that consists of the following three parts:

Input

The first part consists of two positive integers in one line, which represent the number s of hot springs and the number d of districts in the city, respectively.

The second part consists of s lines: each line contains d non-negative integers. The i-th integer in the j-th line represents the distance between the j-th hot spring and the i-th district if it is non-zero. If zero it means they are not connectable due to an obstacle between them.

The third part consists of d-1 lines. The i-th line has d - i non-negative integers. The i-th integer in the j-th line represents the distance between the j-th and the (i + j)-th districts if it is non-zero. The meaning of zero is the same as mentioned above.

For the sake of simplicity, you can assume the following:

The number of hot springs and that of districts do not exceed 50.

Each distance is no more than 100.

Each line in the input file contains at most 256 characters.

Each number is delimited by either whitespace or tab.

The input has several test cases. The input terminate with a line which has two 0. The number of test cases is less than 20.

Output

Output the minimum length of pipeline for each test case.

Sample Input

3 5
12 8 25 19 23
9 13 16 0 17
20 14 16 10 22
17 27 18 16
9 7 0
19 5
21
0 0

Output for the Sample Input

38

Hint

The first line correspondings to the first part: there are three hot springs and five districts. The following three lines are the second part: the distances between a hot spring and a district. For instance, the distance between the first hot spring and the third district is 25. The last four lines are the third part: the distances between two districts. For instance, the distance between the second and the third districts is 9. The second hot spring and the fourth district are not connectable The second and the fifth districts are not connectable, either.

Solution in C++ (judge verdict: Runtime Error)

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

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;

struct Edge{
  int u,v,cost;
  bool operator<(const Edge& a)const{ return cost < a.cost; }
};

struct UnionFind{
  int par[100000];
  int rank[100000];

  UnionFind(int n){ init(n);}
  void init(int n){
    rep(i,n){
      par[i] = i;
      rank[i] = 0;
    }
  }
  int find(int x){
    if(x==par[x]) return x;
    par[x] = find(par[x]);
  }
  int same(int x, int y){
    return find(x) == find(y);
  }
  void unite(int x, int y){
    x = find(x); y = find(y);
    if(x==y) return;

    if(rank[x] < rank[y]){
      par[x] = y;
    }else{
      par[y] = x;
      if(rank[x]==rank[y]) rank[x]++;
    }
  }
};

int S,D,E;
vector<Edge>es;

int main(){
  while(cin >> S >> D, S){
    es.clear();
  rep(i,S){
    rep(j,D){
      int x;
      cin >> x;
      if(!x) continue;
      es.push_back((Edge){i,S+j,x});
    }
  }
  int pos = S*D;
  rep(i,D-1) rep(j,D-i-1){
    int x; cin >> x;
    if(!x) continue;
    es.push_back((Edge){S+i,S+i+j+1,x});
  }
  

  UnionFind uf(100000);
  //  for(int i=1;i<S;i++) uf.unite(0,i);
  sort(es.begin(), es.end());
  int res = 0;

  rep(i,es.size()){
    Edge e = es[i];
    if(!uf.same(e.v, e.u)){
      int f1=0,f2=0;
      rep(j,S){
	if(!f1 && uf.same(j, e.u)) f1 = 1;
	if(!f2 && uf.same(j, e.v)) f2 = 1;
      }
      //  if(uf.same(e.v,0) && uf.same(e.u, 0)) continue;
      //      cout << e.u << " " << e.v << " " << e.cost << endl;
      if(!f1 || !f2){
	uf.unite(e.v, e.u);
	res += e.cost;
      }
    }
  }
  cout << res << endl;
  }
  return 0;
}