from collections import defaultdict H, W = map(int, input().split()) A = [list(map(int, input().split())) for _ in range(H)] SUM = sum(sum(a) for a in A) if H == 1 or W == 1: exit(print(SUM)) sumH = [0]*H sumW = [0]*W sumHW1 = [0]*(H+W-1) sumHW2 = [0]*(H+W-1) for i in range(H): for j in range(W): sumH[i] += A[i][j] sumW[j] += A[i][j] sumHW1[i-j] += A[i][j] sumHW2[i+j] += A[i][j] sumHS = sorted(sumH) sumWS = sorted(sumW) sumHW1S = sorted(sumHW1) sumHW2S = sorted(sumHW2) ans = max(sum(sumHS[-2:]), sum(sumWS[-2:]), sum(sumHW1S[-2:]), sum(sumHW2S[-2:])) D = defaultdict(int) for i in range(H): for j in range(W): T = (0, i, i-j) D[T] = A[i][j] T = (1, i, j) D[T] = A[i][j] T = (2, i, i+j) D[T] = A[i][j] T = (3, i-j, j) D[T] = A[i][j] T = (4, i-j, i+j) D[T] = A[i][j] T = (5, j, i+j) D[T] = A[i][j] for i in range(H): for j in range(-W+1, H): ans = max(ans, sumH[i]+sumHW1[j]-D[(0, i, j)]) for i in range(H): for j in range(W): ans = max(ans, sumH[i]+sumW[j]-D[(1, i, j)]) for i in range(H): for j in range(H+W-1): ans = max(ans, sumH[i]+sumHW2[j]-D[(2, i, j)]) for i in range(-W+1, H): for j in range(W): ans = max(ans, sumHW1[i]+sumW[j]-D[(3, i, j)]) for i in range(-W+1, H): for j in range(H+W-1): ans = max(ans, sumHW1[i]+sumHW2[j]-D[(4, i, j)]) for i in range(W): for j in range(H+W-1): ans = max(ans, sumW[i]+sumHW2[j]-D[(5, i, j)]) print(ans)