#include #include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long ll; typedef vector vi; typedef vector vvi; #define rep(i,n) for(ll i=0;i<(n);i++) #define tii tuple #define tiii tuple #define mt make_tuple #define pb push_back #define ALL(a) (a).begin(),(a).end() #define FST first #define SEC second const int INF = (INT_MAX/2); const ll LLINF = (LLONG_MAX/2); const double eps = 1e-5; const double PI = M_PI; #define DEB cerr<<"!"<0){if((n&1)==1)r=r*x%m;x=x*x%m;n>>=1;}return r%m;} typedef vector Array; typedef vector Matrix; int n,m; // O(n) Matrix identity(int n) { Matrix A(n, Array(n)); rep(i,n) A[i][i] = 0; return A; } // O(n^2) Array mul(const Matrix &A, const Array &x){ Array y(A.size()); //rep(i,A.size())rep(j,A[0].size()) y[i] = A[i][j] * x[j]; return y; } // O(n^3) Matrix mul(const Matrix &A, const Matrix &B) { Matrix C(A.size(), Array(B[0].size())); rep(i,C.size())rep(j,C[i].size())rep(k,A[i].size()) C[i][j] = max(C[i][j],A[i][k] + B[k][j]); return C; } // O(n^3 log e) Matrix pow(const Matrix &A, int e) { return e == 0 ? identity(A.size()) : e % 2 == 0 ? pow(mul(A, A), e/2) : mul(A, pow(A, e-1)); } int main(){ cin >> n >> m; Matrix mat(m,Array(m)); rep(i,m)rep(j,m) cin >> mat[i][j]; mat = pow(mat,n-1); ll ans = 0; rep(i,m){rep(j,m)cout << " " << mat[i][j]; cout << endl;} rep(i,m)rep(j,m) ans = max(ans,mat[i][j]); cout << ans << endl; }