#[allow(unused_imports)] use std::cmp::{max, min}; #[allow(unused_imports)] use std::collections::{HashMap, HashSet}; mod util { use std::io::stdin; use std::str::FromStr; use std::fmt::Debug; #[allow(dead_code)] pub fn line() -> String { let mut line: String = String::new(); stdin().read_line(&mut line).unwrap(); line.trim().to_string() } #[allow(dead_code)] pub fn get() -> T where ::Err: Debug, { let mut line: String = String::new(); stdin().read_line(&mut line).unwrap(); line.trim().parse().unwrap() } #[allow(dead_code)] pub fn gets() -> Vec where ::Err: Debug, { let mut line: String = String::new(); stdin().read_line(&mut line).unwrap(); line.split_whitespace() .map(|t| t.parse().unwrap()) .collect() } #[allow(dead_code)] pub fn get2() -> (T, U) where ::Err: Debug, ::Err: Debug, { let mut line: String = String::new(); stdin().read_line(&mut line).unwrap(); let mut iter = line.split_whitespace(); ( iter.next().unwrap().parse().unwrap(), iter.next().unwrap().parse().unwrap(), ) } #[allow(dead_code)] pub fn get3() -> (S, T, U) where ::Err: Debug, ::Err: Debug, ::Err: Debug, { let mut line: String = String::new(); stdin().read_line(&mut line).unwrap(); let mut iter = line.split_whitespace(); ( iter.next().unwrap().parse().unwrap(), iter.next().unwrap().parse().unwrap(), iter.next().unwrap().parse().unwrap(), ) } } #[allow(unused_macros)] macro_rules! debug { ($x: expr) => { println!("{}: {:?}", stringify!($x), $x) } } fn main() { let n: usize = util::get(); let m: usize = util::get(); let a: Vec> = (0..m).map(|_| util::gets()).collect(); let mut dp = vec![vec![vec![0; m]; m]; 40]; for i in 0..40 { for k in 0..m { for j in 0..m { if i == 0 { dp[i][k][j] = a[k][j]; } else { for h in 0..m { dp[i][k][j] = max(dp[i][k][j], dp[i - 1][k][h] + dp[i - 1][h][j]); } } } } } let mut dp2 = vec![0; m]; for i in 0..40 { if ((n - 1) >> i) & 1 != 1 { continue; } let mut next = vec![0; m]; for k in 0..m { for j in 0..m { next[k] = max(next[k], dp2[j] + dp[i][j][k]); } } dp2 = next; } println!("{}", dp2.iter().cloned().max().unwrap()) }