Problem: You are recording a result of a secret experiment, which consists of a large set of N-dimensional vectors. Since the result may become very large, you are thinking of compressing it. Fortunately you already have a good compression method for vectors with small absolute values, all you have to do is to preprocess the vectors and make them small.

You can record the set of vectors in any order you like. Let's assume you process them in the order v_1, v_2,..., v_M. Each vector v_i is recorded either as is, or as a difference vector. When it is recorded as a difference, you can arbitrarily pick up an already chosen vector v_j (j<i) and a real value r. Then the actual vector value recorded is (v_i - r v_j). The values of r and j do not affect the compression ratio so much, so you don't have to care about them.

Given a set of vectors, your task is to write a program that calculates the minimum sum of the squared length of the recorded vectors.



Input

The input is like the following style.

N M
v_{1,1} v_{1,2} ... v_{1,N}
...
v_{M,1} v_{M,2} ... v_{M,N}


The first line contains two integers N and M (1 \leq N, M \leq 100), where N is the dimension of each vector, and M is the number of the vectors. Each of the following M lines contains N floating point values v_{i,j} (-1.0 \leq v_{i,j} \leq 1.0) which represents the j-th element value of the i-th vector.

Output

Output the minimum sum of the squared length of the recorded vectors. The output should not contain an absolute error greater than 10^{-6}.

Examples

Input

2 3
1.0 1.0
-1.0 0.0
0.5 0.5


Output

1.0


Input

1 1
1.0


Output

1.0


Input

4 3
1.0 1.0 0.0 0.0
-1.0 0.0 -1.0 0.0
0.5 0.5 0.5 0.5


Output

3.0

Solution (in CPP):

Solution:
#include<iostream>
#include<vector>

using namespace std;

typedef double weight_t;
const weight_t INF=1<<29;

struct edge{
  int v;
  weight_t w;
};

vector<int> minimum_spanning_arborescence(const vector<vector<edge> > &G,int root){
  const int N=G.size();
  vector<int> prev(N,-1);
  vector<weight_t> weight(N,INF);
  for(int i=0;i<N;i++){
    for(auto e:G[i]){
      if(e.v!=root&&e.w<weight[e.v]){
	weight[e.v]=e.w;
	prev[e.v]=i;
      }
    }
  }
  vector<int> check(N,N);
  int cycle_len=0;
  vector<char> in_cycle(N);
  for(int i=0;i<N;i++){
    for(int j=i;j>=0;){
      if(check[j]<i)break;
      if(check[j]==i){
	for(int k=j;;){
	  cycle_len++;
	  in_cycle[k]=true;
	  k=prev[k];
	  if(k==j)goto found;
	}
      }
      check[j]=i;
      j=prev[j];
    }
  }
  return prev;
 found:
  int nN=N-cycle_len+1;
  vector<int> newv(N),oldv(N);
  for(int i=0,idx=0;i<N;i++){
    if(!in_cycle[i]){
      oldv[idx]=i;
      newv[i]=idx++;
    }else{
      newv[i]=nN-1;
    }
  }
  vector<vector<edge> > nG(nN);
  vector<int> in(N,-1),out(N,-1);
  vector<weight_t> in_val(N,INF),out_val(N,INF);
  for(int i=0;i<N;i++){
    for(auto e:G[i]){
      weight_t nc=e.w-weight[e.v];
      if(!in_cycle[i]){
	if(!in_cycle[e.v]){
	  nG[newv[i]].push_back({newv[e.v],nc});
	}else{
	  if(nc<in_val[i]){
	    in_val[i]=nc;
	    in[i]=e.v;
	  }
	}
      }else{
	if(!in_cycle[e.v]){
	  if(nc<out_val[e.v]){
	    out_val[e.v]=nc;
	    out[e.v]=i;
	  }
	}
      }
    }
  }
  for(int i=0;i<N;i++){
    if(in_val[i]<INF){
      nG[newv[i]].push_back({nN-1,in_val[i]});
    }
    if(out_val[i]<INF){
      nG[nN-1].push_back({newv[i],out_val[i]});
    }
  }
  auto r=minimum_spanning_arborescence(nG,newv[root]);
  vector<int> rv(N);
  for(int i=0;i<nN-1;i++){
    if(r[i]<0){
      rv[oldv[i]]=r[i];
    }else if(r[i]<nN-1){
      rv[oldv[i]]=oldv[r[i]];
    }else{
      rv[oldv[i]]=out[oldv[i]];
    }
  }
  int v_into_cycle=oldv[r[nN-1]];
  int v_in_cycle=in[v_into_cycle];
  rv[v_in_cycle]=v_into_cycle;
  int x=prev[v_in_cycle];
  for(int i=0;i<cycle_len-1;i++){
    rv[x]=prev[x];
    x=prev[x];
  }
  return rv;
}

double sqr(double d){
  return d*d;
}

int main(){
  int N,M;
  cin>>M>>N;
  double v[123][123];
  double d[123][123]={};
  vector<vector<edge> > G(N+1);
  for(int i=0;i<N;i++){
    for(int j=0;j<M;j++){
      cin>>v[i][j];
      d[N][i]+=sqr(v[i][j]);
    }
    G[N].push_back({i,d[N][i]});
  }
  for(int i=0;i<N;i++){
    for(int j=0;j<N;j++){
      if(i==j)continue;
      double num=0,den=0;
      for(int k=0;k<M;k++){
	num+=v[j][k]*v[i][k];
	den+=sqr(v[i][k]);
      }
      if(den==0){
	d[i][j]=d[N][j];
      }else{
	for(int k=0;k<M;k++){
	  d[i][j]+=sqr(v[j][k]-num/den*v[i][k]);
	}
      }
      G[i].push_back({j,d[i][j]});
    }
  }
  auto r=minimum_spanning_arborescence(G,N);
  double ans=0;
  for(int i=0;i<N;i++){
    ans+=d[r[i]][i];
  }
  cout<<fixed<<ans<<endl;
}