import java.util.Arrays; import java.util.LinkedList; import java.util.Scanner; public class Main { public static long[][] mat_mul(int m, long[][] mat, long p){ if(p == 0){ return new long[m][m]; }else if(p == 1){ return mat; }else if(p % 2 == 1){ return mat_max(m, mat, mat_mul(m, mat, p - 1)); }else{ long[][] ret = mat_mul(m, mat, p / 2); return mat_max(m, ret, ret); } } public static long[][] mat_max(int m, long[][] mat1, long[][] mat2){ long[][] ret = new long[m][m]; for(int from = 0; from < m; from++){ for(int to = 0; to < m; to++){ for(int i = 0; i < m; i++){ ret[from][to] = Math.max(ret[from][to], mat1[from][i] + mat2[i][to]); } } } return ret; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); final int n = sc.nextInt(); final int m = sc.nextInt(); long[][] mat = new long[m][m]; for(int i = 0; i < m; i++){ for(int j = 0; j < m; j++){ mat[i][j] = sc.nextLong(); } } long[][] ret = mat_mul(m, mat, n - 1); long max = 0; for(int i = 0; i < m; i++){ for(int j = 0; j < m; j++){ //System.out.print(ret[i][j] + " "); max = Math.max(max, ret[i][j]); } //System.out.println(); } System.out.println(max); } }