#include #include #include #include using namespace std; using R = long long; using Vector = std::vector; using Matrix = std::vector; Matrix mul(Matrix a, Matrix b) { const int n = a.size(); Matrix c(n, Vector(n)); for (int i = 0; i < n; i++) { for (int k = 0; k < n; k++) { for (int j = 0; j < n; j++) { c[i][j] = max(c[i][j], a[i][k] + b[k][j]); } } } return c; } Matrix pow(Matrix a, long long b) { const int n = a.size(); Matrix ret(n, Vector(n)); for (int i = 0; i < n; i++) { ret[i][i] = 0; } for (; b > 0; b >>= 1) { if (b & 1) ret = mul(ret, a); a = mul(a, a); } return ret; } int main() { int n, m; cin >> n >> m; Matrix A(m, Vector(m)); for (int i = 0; i < m; i++) { for (int j = 0; j < m; j++) { cin >> A[i][j]; } } A = pow(A, n - 1); cout << *max_element(begin(A[0]), end(A[0])) << endl; }