Go: // n×nの成長度表に現れる数字を重複を許してm-1個選んで掛け合わせる時の最大値を求める
// ->掛け算型のDP

package main

import (
	"bufio"
	"os"
	"strings"
	"strconv"
		"fmt"
)

var scanner = bufio.NewScanner(os.Stdin)

func main() {
	for scanner.Scan(){
		buf := strings.Split(scanner.Text(), " ")
		n, _ := strconv.Atoi(buf[0])
		m, _ := strconv.Atoi(buf[1])
		if n == 0 && m == 0 {
			break
		}
		growth_table := make([][]float64, n)
		for i := range growth_table {
			scanner.Scan()
			buf_ := strings.Split(scanner.Text(), " ")
			tmp := make([]float64, n)
			for j := range tmp {
				tmp[j], _ = strconv.ParseFloat(buf_[j], 64)
			}
			growth_table[i] = tmp
		}
		dp := make([][]float64, m)
		for i := range dp {
			dp[i] = make([]float64, n)
		}
		for i := range dp {
			dp[0][i] = 1
		}
		for k := 1; k < m; k++ {
			// k個使うとき...
			for i := 0; i < n; i++ {
				for j := 0; j < n; j++ {
					// 新たにgrowth_table[i][jを使うかどうか
					dp[k][j] = max(dp[k][j], dp[k-1][i]*growth_table[i][j])
					// check(dp)
				}
			}
		}
		ans := 0.0
		for i := 0; i < n; i++ {
			if dp[m-1][i] > ans {
				ans = dp[m-1][i]
			}
		}
		fmt.Printf("%.2f\n", ans)
	}
}

func max(a, b float64) float64 {
	if a < b {
		return b
	}
	return a
}

// func check(dp [][]float64) {
// 	log.Println()
// 	for i := range dp {
// 		log.Println(dp[i])
// 	}
// 	log.Println()
// }

C++: #include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<cassert>
#include<cmath>
#define EPS 1e-10
using namespace std;
typedef pair<double,int> P;
int m,n;
double total;
double dp[110][110];
int Just;
vector<double> matrix[110];
double rec(int next,int counter){
  if(dp[next][counter] > 0)return dp[next][counter];
  if(counter  == 0){
    return 1.0;
  }
  double HANMA = 0.0;
 
 
  for(int i=0;i<n;i++){   
    
      HANMA = max(HANMA,rec(i,counter-1)*matrix[next][i]);
    
  }  
 
  dp[Just][counter] = HANMA;
  return HANMA;
}


int main(){
  
  double In;
 
  while(cin >> n >> m && n && m){
    for(int i=0;i<=n;i++)matrix[i].clear();
    
    for(int i=m;i>=0;i--)for(int j=0;j<n;j++)dp[j][i] = -10;
    for(int i=0;i<n;i++)dp[i][0] = dp[i][1] = 1;
    double MaX[n+1];
    int Pos[n+1];
    for(int i=0;i<n;i++)MaX[i] = 0;
    for(int i=0;i<n;i++){
      for(int j=0;j<n;j++){
	cin >> In;
	matrix[i].push_back(In);
      }
    }
 

    double ans = -1;
 
  
      for(int I=0;I<n;I++){
	Just = I;
	double tes = rec(I,m);
	ans = max(ans,dp[Just][m]);
      }
    

      //for(int i=1;i<=m;i++)cout << dp[0][i] << " ";
      //cout << endl;

    printf("%.2f\n",ans);

  }
  return 0;
}

C++: #include <iostream>
#include <vector>
#include <cmath>
#include <cstdio>

using namespace std;

int main() {
	int n, m;
	while (cin >> n >> m) {
		if (n == 0 && m == 0) {
			break;
		}
		vector< vector<long double> > hyou(n, vector<long double>(n));
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				cin >> hyou[i][j];
			}
		}
		vector< vector<long double> > nae(m, vector<long double>(n, 1.0));
		for (int i = 1; i < m; i++) {
			for (int j = 0; j < n; j++) {
				nae[i][j] = nae[i-1][0]*hyou[0][j];
				for (int k = 1; k < n; k++) {
					nae[i][j] = max(nae[i][j], nae[i-1][k]*hyou[k][j]);
				}
			}
		}
		long double MAX = 0.0;
		for (int i = 0; i < n; i++) {
			MAX = max(MAX, nae[m-1][i]);
		}
		MAX *= 100.0;
		if (MAX-(long double)((int)MAX) >= 0.5) {
			MAX += 1.0;
		} 
		MAX /= 100.0;
		printf("%.2Lf\n", MAX);
	}
	return 0;
}

C++: #include <iostream>
#include <string>
#include <string.h>
#include <algorithm>
#include <iomanip>
using namespace std;

const int MAX_N = 100;
const int MAX_M = 100;

double g[ MAX_M + 1 ][ MAX_N + 1 ][ MAX_N + 1 ];

int main()
{
	int n, m;

	while( cin >> n >> m && n != 0 && m != 0 ){
		memset( g, 0, sizeof(g) );
		for( int i = 0; i < n; ++i ){
			for( int j = 0; j < n; ++j ){
				cin >> g[ 2 ][ i ][ j ];
			}
		}
		for( int i = 0; i < n; ++i ){
			double beginMax = 0.0;
			double endMax = 0.0;
			for( int j = 0; j < n; ++j ){
				beginMax = max( beginMax, g[ 2 ][ i ][ j ] );	//最初に撒く肥料を固定したときの最大成長値
				endMax = max( endMax, g[ 2 ][ j ][ i ] );		//最後に撒く肥料を固定したときの最大成長値
				//1回目撒いたときは1.0倍とみなして初期化
				g[ 1 ][ i ][ j ] = 1.0;
			}
			g[ 1 ][ i ][ MAX_N ] = 1.0;
			g[ 1 ][ MAX_N ][ i ] = 1.0;
			g[ 2 ][ i ][ MAX_N ] = beginMax;
			g[ 2 ][ MAX_N ][ i ] = endMax;
		}

		for( int t = 3; t <= m; ++t ){	//t回目の肥料撒き
			for( int k = 0; k < n; ++k ){	//ワーシャルフロイド、肥料kを経由
				for( int i = 0; i < n; ++i ){	//一番最初に撒いた肥料i
					for( int j = 0; j < n; ++j ){	//一番最後に撒いた肥料j
						for( int s = 2; s < t ; ++s ){	//肥料巻きs回目の時点で肥料kを経由する
							double bv = 0.0;	//経由直前までの成長値
							double av = 0.0;	//経由直後からの成長値
							int bg = 0;	//経由直前の肥料の種類
							int ag = 0;	//経由直後の肥料の種類
							for( int u = 0; u < n; ++u ){	//経由肥料kの前後にあたる肥料u
								double bbv, aav;
								if( s - 1 > 2 ){
									bbv = g[ s - 1 ][ MAX_N ][ u ] * g[ 2 ][ u ][ k ];
								}else{
									bbv = g[ 2 ][ i ][ k ];
								}
								if( bv < bbv ){
									bv = bbv;
									bg = u;
								}
								if( t - s > 2 ){
									aav = g[ 2 ][ k ][ u ] * g[ t - s ][ u ][ MAX_N ];
								}else{
									aav = g[ 2 ][ k ][ j ];
								}
								if( av < aav ){
									av = aav;
									ag = u;
								}
							}
							double total = bv * av;
							g[ t ][ i ][ j ] = max( g[ t ][ i ][ j ],  total );
							g[ t ][ i ][ MAX_N ] = max( g[ t ][ i ][ MAX_N ], g[ t ][ i ][ j ] );
							g[ t ][ MAX_N ][ j ] = max( g[ t ][ MAX_N ][ j ], g[ t ][ i ][ j ] );
						}
					}
				}
			}
		}

		double ans = 0.0;
		for( int i = 0; i < n; ++i ){
			ans = max( ans, g[ m ][ i ][ MAX_N ] );
		}

		cout << fixed << setprecision( 2 ) << ans << endl;
	}
	return 0;
}

C++: #include <iostream>
#include<iomanip>
#include<algorithm>
#define shosu(x) fixed<<setprecision(x)
using namespace std;

int n,m;
double ef[105][105];

double func(){
	double ans=0,dp[105][105]={0};
	for(int i=0;i<m;i++){
		dp[0][i]=1;
	}
	for(int i=1;i<n;i++){
		for(int j=0;j<m;j++){
			for(int k=0;k<m;k++){
				int tmp=0;
				tmp=max(tmp,dp[i-1][k]*ef[k][j]);
			}
			dp[i][j]=tmp;
		}
	}
	for(int i=0;i<m;i++){
		ans=max(ans,dp[n-1][i]);
	}
	return ans;
}

int main(){
	cout<<shosu(2);
	while(1){
		cin>>n>>m;
		if(n==0&&m==0) break;
		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++){
				cin>>ef[i][j];
			}
		}
		cout<<func()<<endl;
	}
}

C++: #include <iostream>
#include <vector>
#include <cstdio>
using namespace std;

int n, m;
double dp[101][101];

double solve(int prev, int cnt, const vector<vector<double> >& manure)
{
  if (cnt >= m)
    return 1.0;

  if (dp[prev][cnt] != -1.0)
    return dp[prev][cnt];

  double res = 0.0;
  for (int i = 0; i < n; ++i)
    res = max(res, solve(i, cnt+1, manure) * manure[prev][i]);

  return dp[prev][cnt] = res;
}

int main()
{

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

    vector<vector<double> > manure(n+1, vector<double>(n+1, 1.0));
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < n; ++j)
	cin >> manure[i][j];
    }

    for (int i = 0; i < 101; ++i) {
      for (int j = 0; j < 101; ++j)
	dp[i][j] = -1.0;
    }

    printf("%.2f\n", static_cast<int>(solve(n, 0, manure) * 100 + 0.5) / 100.0);
  }
  return 0;
}