class Max2: def __init__(self, fst_key=-1, fst_val=-9*10**18, snd_key=-2, snd_val=-9*10**18): self.fst_key, self.fst_val = fst_key, fst_val self.snd_key, self.snd_val = snd_key, snd_val def chmax2(self, key, val): if key == self.fst_key: self.fst_val = max(self.fst_val, val) elif val > self.fst_val: self.snd_key, self.snd_val = self.fst_key, self.fst_val self.fst_key, self.fst_val = key, val elif val > self.snd_val: self.snd_key, self.snd_val = key, val from itertools import product H, W = map(int, input().split()) A = [[0]*(2*H+W) for _ in range(2*W+H)] for i in range(H): A[W+i][H:H+W] = list(map(int, input().split())) row, col = [0]*(2*W+H), [0]*(2*H+W) diag, skew = [0]*(3*H+3*W), [0]*(3*H+3*W) for i, j in product(range(2*W+H), range(2*H+W)): row[i] += A[i][j]; col[j] += A[i][j] diag[i-j] += A[i][j]; skew[i+j] += A[i][j] row_max2, col_max2 = Max2(), Max2() diag_max2, skew_max2 = Max2(), Max2() for i in range(2*W+H): row_max2.chmax2(i, row[i]) for j in range(2*H+W): col_max2.chmax2(j, col[j]) for k in range(3*H+3*W): diag_max2.chmax2(k, diag[k]) for l in range(3*H+3*W): skew_max2.chmax2(l, skew[l]) ans = max( row_max2.fst_val + row_max2.snd_val, col_max2.fst_val + col_max2.snd_val, diag_max2.fst_val + diag_max2.snd_val, skew_max2.fst_val + skew_max2.snd_val ) for i, j in product(range(2*W+H), range(2*H+W)): max2 = Max2(0, row[i]); max2.chmax2(1, col[j]) max2.chmax2(2, diag[i-j]); max2.chmax2(3, skew[i+j]) ans = max(ans, max2.fst_val + max2.snd_val - A[i][j]) even_diag_max = odd_diag_max = 0 even_skew_max = odd_skew_max = 0 for k in range(3*H+3*W): if k%2: odd_diag_max = max(odd_diag_max, diag[k]) else: even_diag_max = max(even_diag_max, diag[k]) for l in range(3*H+3*W): if l%2: odd_skew_max = max(odd_skew_max, skew[l]) else: even_skew_max = max(even_skew_max, skew[l]) ans = max(ans, even_diag_max + odd_skew_max, even_skew_max + odd_diag_max ) print(ans)