#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair P; const double PI = 3.14159265358979323846; const double EPS = 1e-12; const ll INF = 1LL<<29; const ll mod = 1e9+7; #define rep(i,n) for(int (i)=0;(i)<(ll)(n);++(i)) #define repd(i,n,d) for(ll (i)=0;(i)<(ll)(n);(i)+=(d)) #define all(v) (v).begin(), (v).end() #define pb(x) push_back(x) #define mp(x,y) make_pair((x),(y)) #define mset(m,v) memset((m),(v),sizeof(m)) #define chmin(X,Y) ((X)>(Y)?X=(Y),true:false) #define chmax(X,Y) ((X)<(Y)?X=(Y),true:false) #define fst first #define snd second #define UNIQUE(x) (x).erase(unique(all(x)),(x).end()) template ostream &operator<<(ostream &os, const vector &v){int n=v.size();rep(i,n)os<> &A, vector> &B){ vector> C(A), D(B); int n = A.size(); rep(i, n) rep(j, n) rep(k, n) chmax(A[i][j], C[i][k]+D[k][j]); } int main(){ int n, m; cin>>n>>m; vector> A(m, vector(m)), X(m, vector(m, 0)); rep(i, m) rep(j, m) cin>>A[i][j]; n--; while(n){ if(n&1) mul(X, A); mul(A, A); n >>= 1; } ll res = 0; rep(i, m) rep(j, m) chmax(res, X[i][j]); cout<