#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <functional>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
#include <cassert>
#include <tuple>
using namespace std;
typedef long long LL;
typedef complex<double> CMP;

class BuildingRoutes { public:
	int build(vector <string> dist, int T)
	{
		const int N = dist.size();
		vector<vector<int>> use(N, vector<int>(N, 0));

		vector<vector<int>> d(N, vector<int>(N));
		for(int s=0; s<N; ++s)
		for(int t=0; t<N; ++t)
			d[s][t] = dist[s][t] - '0';
		for(int k=0; k<N; ++k)
		for(int i=0; i<N; ++i)
		for(int j=0; j<N; ++j)
			d[i][j] = min(d[i][j], d[i][k]+d[k][j]);

		for(int s=0; s<N; ++s)
		for(int t=0; t<N; ++t)
			for(int a=0; a<N; ++a)
			for(int b=0; b<N; ++b) if(a!=b)
				if(d[s][a]+(dist[a][b]-'0')+d[b][t] == d[s][t])
					use[a][b] ++;

		int total = 0;
		for(int s=0; s<N; ++s)
		for(int t=0; t<N; ++t)
			if(use[s][t] >= T)
				total += dist[s][t] - '0';
		return total;
	}
};

// BEGIN CUT HERE
#include <ctime>
double start_time; string timer()
 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); }
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v)
 { os << "{ ";
   for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it)
   os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; }
void verify_case(const int& Expected, const int& Received) {
 bool ok = (Expected == Received);
 if(ok) cerr << "PASSED" << timer() << endl;  else { cerr << "FAILED" << timer() << endl;
 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } }
#define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock();
#define END	 verify_case(_, BuildingRoutes().build(dist, T));}
int main(){

CASE(0)
	string dist_[] = {"011",
 "101",
 "110"};
	  vector <string> dist(dist_, dist_+sizeof(dist_)/sizeof(*dist_)); 
	int T = 1; 
	int _ = 6; 
END
CASE(1)
	string dist_[] = {"033",
 "309",
 "390"};
	  vector <string> dist(dist_, dist_+sizeof(dist_)/sizeof(*dist_)); 
	int T = 1; 
	int _ = 12; 
END
CASE(2)
	string dist_[] = {"0123",
 "1023",
 "1203",
 "1230"};
	  vector <string> dist(dist_, dist_+sizeof(dist_)/sizeof(*dist_)); 
	int T = 2; 
	int _ = 5; 
END
CASE(3)
	string dist_[] = {"05789654",
 "10347583",
 "65085479",
 "55602398",
 "76590934",
 "57939045",
 "12345608",
 "68647640"};
	  vector <string> dist(dist_, dist_+sizeof(dist_)/sizeof(*dist_)); 
	int T = 3; 
	int _ = 40; 
END
/*
CASE(4)
	string dist_[] = ;
	  vector <string> dist(dist_, dist_+sizeof(dist_)/sizeof(*dist_)); 
	int T = ; 
	int _ = ; 
END
CASE(5)
	string dist_[] = ;
	  vector <string> dist(dist_, dist_+sizeof(dist_)/sizeof(*dist_)); 
	int T = ; 
	int _ = ; 
END
*/
}
// END CUT HERE
