import numpy as np import sys sys.setrecursionlimit(10 ** 6) int1 = lambda x: int(x) - 1 p2D = lambda x: print(*x, sep="\n") def II(): return int(sys.stdin.readline()) def MI(): return map(int, sys.stdin.readline().split()) def LI(): return list(map(int, sys.stdin.readline().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] def SI(): return sys.stdin.readline()[:-1] def inmax(aa,bb): h=len(aa) w=len(bb[0]) res=np.zeros((h,w),dtype="i8") for i in range(h): for j in range(w): res[i][j]=np.max(aa[i]+bb[:,j]) return res def main(): n=II()-1 m=II() base=np.array(LLI(m),dtype="i8") aa=np.zeros((m,m),dtype="i8") while n: if n&1:aa=inmax(aa,base) base=inmax(base,base) n>>=1 print(np.max(aa)) main()