import java.util.ArrayList; import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { new Main().run(); } void run() { Scanner sc = new Scanner(System.in); long n = sc.nextLong(); int m = sc.nextInt(); long[][] a = new long[m][m]; for (int i = 0; i < m; ++i) { for (int j = 0; j < m; ++j) a[i][j] = sc.nextLong(); } long[][] v = new long[m][1]; v = mul(pow(a, n - 1), v); long ans = 0; for (int i = 0; i < v.length; ++i) ans = Math.max(ans, v[i][0]); System.out.println(ans); } long[][] pow(long[][] a, long n) { if (a.length != a[0].length) throw new AssertionError(); long[][] ret = new long[a.length][a.length]; for (; n > 0; n >>= 1, a = mul(a, a)) { if (n % 2 == 1) { ret = mul(ret, a); } } return ret; } long[][] mul(long[][] a, long[][] b) { long[][] ret = new long[a.length][b[0].length]; for (int i = 0; i < a.length; ++i) { for (int j = 0; j < b[i].length; ++j) { for (int m = 0; m < a[i].length; ++m) { ret[i][j] = Math.max(ret[i][j], a[i][m] + b[m][j]); } } } return ret; } void tr(Object... objects) { System.out.println(Arrays.deepToString(objects)); } }