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

package main

import (
	"bufio"
	"os"
	"strings"
	"strconv"
	"log"
	"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 <iomanip>
#include <cmath>
#include <cfloat>
using namespace std;

const int NMAX = 100;

int main() {
  int n, m;
  double dp[2][NMAX];
  double g[NMAX][NMAX];
  double lg[NMAX][NMAX];

  while (1) {
    cin >> n >> m;
    if (!n && !m) {
      break;
    }

    for (int i=0; i<n; ++i) {
      for (int j=0; j<n; ++j) {
        cin >> g[i][j];
        lg[i][j] = log(g[i][j]);
      }
    }
    for (int i=0; i<n; ++i) {
      dp[0][i] = 0.0;
    }

    for (int i=0; i<m-1; ++i) {
      for (int j=0; j<n; ++j) {
        dp[!(i&1)][j] = -DBL_MAX;
        for (int k=0; k<n; ++k) {
          dp[!(i&1)][j] = max(dp[!(i&1)][j], dp[i&1][k] + lg[k][j]);
        }
      }
    }

    double lans = 0.0;
    for (int i=0; i<n; ++i) {
      lans = max(lans, dp[!(m&1)][i]);
    }
    cout << fixed << setprecision(2) << round(exp(lans) * 100) / 100. << endl;
  }
  return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<cstdio>
#include<iomanip>

using namespace std;

int n, m;
double grow[100][100];
double dp[100][100];//

int main() {
	while (cin >> n >> m, n * m) {
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				cin >> grow[i][j];
			}
		}
		for (int i = 0; i < n; i++)dp[0][i] = 1;
		for (int i = 1; i < m; i++) {
			for (int j = 0; j < n; j++) {
				double M = 0;
				for (int k = 0; k < n; k++) {
					M = max(M, dp[i - 1][k] * grow[k][j]);
				}
				dp[i][j] = M;
			}
		}
		double ans = 0;
		for (int i = 0; i < n; i++) {
			ans = max(ans, dp[m - 1][i]);
		}
		int a = (ans + 0.005) * 100;
		ans = a;
		ans /= 100;
		cout << fixed << setprecision(2) << ans << endl;
		for (int i = 0; i < n; i++)for (int j = 0; j < n; j++)grow[i][j] = 0;
		for (int i = 0; i < m; i++)for (int j = 0; j < n; j++)dp[i][j] = 0;
	}
	return 0;
}

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

int main(){
  int n,m;
  double In;
 
  while(cin >> n >> m && n && m){
    vector<double> matrix[n+2];
  
    for(int i=0;i<n;i++){
      for(int j=0;j<n;j++){
	cin >> In;
	matrix[i].push_back(In);
      }
    }
    double dp[n+2][m+2];//選んだもの、回数
    for(int i=0;i<n;i++){
      dp[i][0] = dp[i][1] = 1.0; 
      for(int j=2;j<=m;j++){
	dp[i][j] = -1.0;
      }
    }

    for(int i=0;i<n;i++){
      for(int j=2;j<=m;j++){
	for(int k=0;k<n;k++){
	  dp[i][j] = max(dp[i][j],dp[i][j-1]*matrix[i][k]);
	}
      }
    }
    double ans = -1;
    for(int I=0;I<n;I++)ans = max(ans,dp[I][m]);
    printf("%.2f\n",round(ans));

  }
  return 0;
}

C++: //パソコン甲子園2008本選 問題09

#include<stdio.h>

int main(void){

	double *nl, **L, *ml, max, tmp;
	int n, m, f1, f2, f3;
	
	while(scanf("%d %d",&n,&m) && n > 0){
	
		L = new double *[n];
		nl = new double [n];
		 
		for(f1 = 0; f1 < n; f1++){
			nl[f1] = 1.0;//１回目は1.0なので1.0で初期化
			L[f1] = new double [n];
			for(f2 = 0; f2 < n; f2++)
				scanf("%lf",&L[f1][f2]);
		}
		
		for(f1 = 1; f1 < m; f1++){
		
			ml = new double [n];
			
			for(f2 = 0; f2 < n; f2++)//初期化
				ml[f2] = 0.0;
			//計算
			for(f2 = 0; f2 < n; f2++){
				for(f3 = 0; f3 < n; f3++){
					tmp = nl[f2] * L[f2][f3];
					if(tmp > ml[f3])
						ml[f3] = tmp;
				}
			}
			nl = ml;
		}
		//最大値を求める
		max = nl[0];
		for(f1 = 0; f1 < n; f1++){
			if(max < nl[f1])
				max = nl[f1];
		}
		//出力
		printf("%.2lf\n",max);
			
	}
	return 0;
}

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

int main() {
  double f[101][101];
  double dp[101][101];
  int n,m;
  while (cin >> n >> m,n||m) {
    
    for ( int i = 0; i < n; i++ ) {
      for ( int j = 0; j < n; j++ ) {
	cin >> f[i][j];
      }
    }

    for ( int i = 0; i < m; i++ ) {
      for ( int j = 0; j < n; j++ ) {
	dp[i][j] = 0.0;
      }
    }

    for ( int i = 0; i < n; i++ ) dp[0][i] = 1.0;

    for ( int i = 1; i < m; i++ ) {
      for ( int k = 0; k < n; k++ ) {
	for ( int j = 0; j < n; j++ ) {
	  dp[i][j] = max( dp[i][j], dp[i-1][j] * f[j][k] );
	}
      }
    }

    double ans = 0.0;
    for ( int i = 0; i < n; i++ ) {
      ans = max ( ans, dp[m-1][i] );
    }
      
    printf ("%.2f\n", ans );
  }
  return 0;
}

C++: #define _USE_MATH_DEFINES
#include<cstdio>
#include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
#include<stack>
#include<cmath>
#include<climits>
using namespace std;

int main(){
	int N, M;
	while(cin >> N >> M, N || M){
		double List[100][100], dp[100][100] = {};
		for(int i = 0; i < N; i++){
			dp[0][i] = 1;
			for(int j = 0; j < N; j++)
				cin >> List[i][j];
		}
	
		for(int i = 0; i < M-1; i++){
			for(int j = 0; j < N; j++){
				for(int k = 0; k < N; k++){
					dp[i+1][k] = max(dp[i+1][k], dp[i][j]*List[j][k]);
				}
			}
		}
		double ans = 0;
		for(int i = 0; i < N; i++) ans = max(ans, dp[M-1][i]);
		printf("%.2lf\n", ans);
	}
	return 0;
}

C++: #include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <list>
#include <iostream>
#include <sstream>
#include <climits>
#include <cfloat>
#include <complex>

using namespace std;

const double Eps = 1e-8;

int main()
{
	int n, m;
	while (scanf("%d %d", &n, &m), n | m)
	{
		double g[100][100];
		for (int i = 0; i < n; ++i)
			for (int j = 0; j < n; ++j)
				scanf("%lf", &g[i][j]);
		double rates[2][100];
		for (int i = 0; i < n; ++i)
			rates[0][i] = 1;

		for (int c = 1; c < m; ++c)
		{
			for (int i = 0; i < n; ++i)
			{
				rates[c & 1][i] = rates[(c+1) & 1][0] * g[0][i];
				for (int j = 1; j < n; ++j)
					rates[c & 1][i] = max(rates[c & 1][i], rates[(c+1) & 1][j] * g[j][i]);
			}
		}
		double ans = -1;
		for (int i = 0; i < n; ++i)
			ans = max(ans, rates[(m+1) & 1][i]);
		printf("%.2f\n", floor(ans*1e2+0.5+Eps)/100);
	}

	return 0;
}

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

int main()
{
	double memo1[100] = {};
	double memo2[100] = {};
	double data[100][100];
	int n, m;

	while (true)
	{
		cin >> n >> m;
		if (n + m == 0) return 0;

		for (int i = 0; i < n; i++)
		{
			for (int j = 0; j < n; j++)
			{
				cin >> data[i][j];
			}
			memo1[i] = 1.0;
		}

		for (int i = 0; i < m - 1; i++)
		{
			for (int j = 0; j < n; j++)
			{
				for (int k = 0; k < n; k++)
				{
					memo2[k] = max(memo2[k], memo1[j] * data[j][k]);
				}
				memo1[j] = 0;
			}
			swap(memo1, memo2);
		}

		double Max = 0;
		for (int i = 0; i < n; i++)
		{
			Max = max(Max, memo1[i]);
		}
		printf("%.2f\n", Max);
	}

	return 0;
}