from collections import deque N = int(input()) M = int(input()) S = [] for i in range(M): scores = list(map(int, input().split(' '))) que = deque() for score_index in sorted([(score,i) for i,score in enumerate(scores)]): que.append(score_index) S.append(que) U = [] for i in range(M): U.append(deque()) def total(start, number): visited = [False for _ in range(M)] total = 0 n = 0 i = start while n < number: score,next = S[i].pop() if visited[next]: U[i].append((score,next)) continue visited[next] = True U[i].append((score,next)) total = total + score i = next n = n + 1 for j in range(M): for _ in range(len(U[j])): S[j].append(U[j].pop()) return total,i if N < M: print(0) exit(0) division = N // M residual = N % M answer = 0 for i in range(M): total1,start = total(i,M) total2,last = total(start,residual-1) answer = max(answer, total1 * division + total2) print(answer)