#include using namespace std; using ll = long long; typedef vector vec; typedef vector mat; mat mul(mat& A, mat& B) { mat C(A.size(), vec(B[0].size(), 0)); for (int i = 0; i < A.size(); i++) { for (int k = 0; k < B.size(); k++) { for (int j = 0; j < B[0].size(); j++) { C[i][j] = max(C[i][j], A[i][k] + B[k][j]); } } } return C; } mat pow(mat A, ll n) { mat B(A.size(), vec(A.size(), 0)); for (int i = 0; i < A.size(); i++) { B[i][i] = 0; } while (n > 0) { if (n & 1) B = mul(B, A); A = mul(A, A); n >>= 1; } return B; } int main() { cin.tie(0); ios::sync_with_stdio(false); ll n; int m; cin >> n >> m; mat A(m, vec(m, 0)); for (int i = 0; i < m; i++) { for (int j = 0; j < m; j++) { cin >> A[i][j]; } } mat B = pow(A, n - 1); ll ans = 0; for (int i = 0; i < m; i++) { for (int j = 0; j < m; j++) { ans = max(ans, B[i][j]); } } cout << ans << endl; return 0; }