Summer is coming! It's time for Iahub and Iahubina to work out, as they both want to look hot at the beach. The gym where they go is a matrix a with n lines and m columns. Let number a[i][j] represents the calories burned by performing workout at the cell of gym in the i-th line and the j-th column.

Iahub starts with workout located at line 1 and column 1. He needs to finish with workout a[n][m]. After finishing workout a[i][j], he can go to workout a[i + 1][j] or a[i][j + 1]. Similarly, Iahubina starts with workout a[n][1] and she needs to finish with workout a[1][m]. After finishing workout from cell a[i][j], she goes to either a[i][j + 1] or a[i - 1][j]. 

There is one additional condition for their training. They have to meet in exactly one cell of gym. At that cell, none of them will work out. They will talk about fast exponentiation (pretty odd small talk) and then both of them will move to the next workout.

If a workout was done by either Iahub or Iahubina, it counts as total gain. Please plan a workout for Iahub and Iahubina such as total gain to be as big as possible. Note, that Iahub and Iahubina can perform workouts with different speed, so the number of cells that they use to reach meet cell may differs.


-----Input-----

The first line of the input contains two integers n and m (3 ≤ n, m ≤ 1000). Each of the next n lines contains m integers: j-th number from i-th line denotes element a[i][j] (0 ≤ a[i][j] ≤ 10^5).


-----Output-----

The output contains a single number — the maximum total gain possible. 


-----Examples-----
Input
3 3
100 100 100
100 1 100
100 100 100

Output
800


-----Note-----

Iahub will choose exercises a[1][1] → a[1][2] → a[2][2] → a[3][2] → a[3][3]. Iahubina will choose exercises a[3][1] → a[2][1] → a[2][2] → a[2][3] → a[1][3].

Difficulty: competition

Starter code:

Solutions:
def main():
    n, m = list(map(int, input().split()))
    aa = []
    for _ in range(n):
        row = list(map(int, input().split()))
        row.append(0)
        aa.append(row)
    aa.append([0] * (m + 1))
    d1, d2, d3, d4 = ([[0] * (m + 1) for _ in range(n + 1)] for _ in (1, 2, 3, 4))
    for i in range(n):
        for j in range(m):
            d1[i][j] = max(d1[i - 1][j], d1[i][j - 1]) + aa[i][j]
    for i in range(n):
        for j in range(m - 1, -1, -1):
            d2[i][j] = max(d2[i - 1][j], d2[i][j + 1]) + aa[i][j]
    for i in range(n - 1, -1, -1):
        for j in range(m):
            d3[i][j] = max(d3[i + 1][j], d3[i][j - 1]) + aa[i][j]
    for i in range(n - 1, -1, -1):
        for j in range(m - 1, -1, -1):
            d4[i][j] = max(d4[i + 1][j], d4[i][j + 1]) + aa[i][j]
    print((max(
        max(d1[i][j - 1] + d2[i - 1][j] + d3[i + 1][j] + d4[i][j + 1] for i in range(1, n - 1) for j in range(1, m - 1)),
        max(d1[i - 1][j] + d2[i][j + 1] + d3[i][j - 1] + d4[i + 1][j] for i in range(1, n - 1) for j in range(1, m - 1)))))


def __starting_point():
    main()

__starting_point()

'''input
3 3
100 100 100
100 1 100
100 100 100
'''
# again a coding delight
from sys import stdin


def create_dp1(matrix, n, m):
	dp = [[0 for i in range(m)] for j in range(n)]
	for i in range(n):
		for j in range(m):
			if i - 1 >= 0:
				dp[i][j] = max(dp[i][j], matrix[i][j] + dp[i - 1][j])
			if j - 1 >= 0:
				dp[i][j] = max(dp[i][j], matrix[i][j] + dp[i][j - 1])
			elif i - 1 < 0 and j - 1 < 0:
				dp[i][j] = matrix[i][j]
	return dp


def create_dp2(matrix, n, m):
	dp = [[0 for i in range(m)] for j in range(n)]
	for i in range(n - 1, -1, -1):
		for j in range(m - 1, -1, -1):
			if i + 1 < n:
				dp[i][j] = max(dp[i][j], matrix[i][j] + dp[i + 1][j])
			if j + 1 < m:
				dp[i][j] = max(dp[i][j], matrix[i][j] + dp[i][j + 1])
			if i + 1 >= n and j + 1 >= m:
				dp[i][j] = matrix[i][j]
	return dp 


def create_dp3(matrix, n, m):
	dp = [[0 for i in range(m)] for j in range(n)]
	for i in range(n - 1, -1, -1):
		for j in range(m):
			if i + 1 < n:
				dp[i][j] = max(dp[i][j], matrix[i][j] + dp[i + 1][j])
			if j - 1 >= 0:
				dp[i][j] = max(dp[i][j], matrix[i][j] + dp[i][j - 1])
			if i + 1 >= n and j - 1 < 0:
				dp[i][j] = matrix[i][j]
	return dp 


def create_dp4(matrix, n, m):
	dp = [[0 for i in range(m)] for j in range(n)]
	for i in range(n):
		for j in range(m - 1, -1, -1):
			if i - 1 >= 0:
				dp[i][j] = max(dp[i][j], matrix[i][j] + dp[i - 1][j])
			if j + 1 < m:
				dp[i][j] = max(dp[i][j], matrix[i][j] + dp[i][j + 1])
			if i - 1 < 0 and j + 1 >= m:
				dp[i][j] = matrix[i][j]
	return dp 


# main starts
n, m = list(map(int, stdin.readline().split()))
matrix = []
for _ in range(n):
	matrix.append(list(map(int, stdin.readline().split())))

dp1 = create_dp1(matrix, n, m) # from 0, 0 to i, j
dp2 = create_dp2(matrix, n, m) # from i, j to n, m
dp3 = create_dp3(matrix, n, m) # from n, 1 to i, j
dp4 = create_dp4(matrix, n, m) # from i, j to 1, m

total = -float('inf')
for i in range(1, n - 1):
	for j in range(1, m - 1):
		first = dp1[i - 1][j] + dp2[i + 1][j] + dp3[i][j - 1] + dp4[i][j + 1]
		second = dp1[i][j - 1] + dp2[i][j + 1] + dp3[i + 1][j] + dp4[i - 1][j]
		total = max(total, first, second)
print(total)

R = lambda: map(int, input().split())
n, m = R()
g = [list() for i in range(n)]
for i in range(n):
    g[i] = list(R())
dp1, dp2, dp3, dp4 = ([[0] * (m + 1) for j in range(n + 1)] for i in range(4))
for i in range(n):
    for j in range(m):
        dp1[i][j] = g[i][j] + max(dp1[i - 1][j], dp1[i][j - 1])
    for j in range(m - 1, -1, -1):
        dp2[i][j] = g[i][j] + max(dp2[i - 1][j], dp2[i][j + 1])
for i in range(n - 1, -1, -1):
    for j in range(m):
        dp3[i][j] = g[i][j] + max(dp3[i + 1][j], dp3[i][j - 1])
    for j in range(m - 1, -1, -1):
        dp4[i][j] = g[i][j] + max(dp4[i + 1][j], dp4[i][j + 1])
print(max(max(dp1[i][j - 1] + dp2[i - 1][j] + dp3[i + 1][j] + dp4[i][j + 1], dp1[i - 1][j] + dp2[i][j + 1] + dp3[i][j - 1] + dp4[i + 1][j]) for j in range(1, m - 1) for i in range(1, n - 1)))

import sys
from math import *

def minp():
	return sys.stdin.readline().strip()

def mint():
	return int(minp())

def mints():
	return list(map(int, minp().split()))

n, m = mints()
a = [0]*n
dp1 = [0]*n
dp2 = [0]*n
dp3 = [0]*n
dp4 = [0]*n
for i in range(n):
	a[i] = list(mints())
	dp1[i] = [0]*m
	dp2[i] = [0]*m
	dp3[i] = [0]*m
	dp4[i] = [0]*m

dp1[0][0] = a[0][0]
dp2[n-1][m-1] = a[n-1][m-1]
dp3[n-1][0] = a[n-1][0]
dp4[0][m-1] = a[0][m-1]
'''
for i in range(1,n):
	dp1[i][0] = dp1[i-1][0] + a[i][0] # >>>>
for i in range(n-2,-1,-1):
	dp2[i][m-1] = dp2[i+1][m-1] + a[i][m-1] # <<<<
for i in range(n-2,-1,-1):
	dp3[i][0] = dp3[i+1][0] + a[i][0] # <<<<
for i in range(1,n):
	dp4[i][m-1] = dp4[i-1][m-1] + a[i][m-1] # >>>>
for i in range(1,m):
	dp1[0][i] = dp1[0][i-1] + a[0][i] # >>>>
for i in range(m-2,-1,-1):
	dp2[n-1][i] = dp2[n-1][i+1] + a[n-1][i] # <<<<
for i in range(1,m):
	dp3[n-1][i] = dp3[n-1][i-1] + a[n-1][i] # >>>>
for i in range(m-2,-1,-1):
	dp4[0][i] = dp4[0][i+1] + a[0][i] # >>>>
'''
for i in range(0,n):
	for j in range(0,m):
		z = 0
		if i-1 >= 0:
			z = dp1[i-1][j]
		if j-1 >= 0:
			z = max(z, dp1[i][j-1])
		dp1[i][j] = z + a[i][j]
		#dp1[i][j] = max(dp1[i-1][j], dp1[i][j-1]) + a[i][j]
for i in range(n-1,-1,-1):
	for j in range(m-1,-1,-1):
		z = 0
		if i+1 < n:
			z = dp2[i+1][j]
		if j+1 < m:
			z = max(z, dp2[i][j+1])
		dp2[i][j] = z + a[i][j]
		#dp2[i][j] = max(dp2[i+1][j], dp2[i][j+1]) + a[i][j]
for i in range(n-1,-1,-1):
	for j in range(0,m):
		z = 0
		if i+1 < n:
			z = dp3[i+1][j]
		if j-1 >= 0:
			z = max(z, dp3[i][j-1])
		dp3[i][j] = z + a[i][j]
		#dp3[i][j] = max(dp3[i+1][j], dp3[i][j-1]) + a[i][j]
for i in range(0,n):
	for j in range(m-1,-1,-1):
		z = 0
		if i-1 >= 0:
			z = dp4[i-1][j]
		if j+1 < m:
			z = max(z, dp4[i][j+1])
		dp4[i][j] = z + a[i][j]
		#dp4[i][j] = max(dp4[i-1][j], dp4[i][j+1]) + a[i][j]
'''for i in dp1:
	print(i)
print()
for i in dp2:
	print(i)
print()
for i in dp3:
	print(i)
print()
for i in dp4:
	print(i)
print()
'''
r = 0
for i in range(1,n-1):
	for j in range(1,m-1):
		r = max(r, dp1[i-1][j] + dp2[i+1][j] + dp3[i][j-1] + dp4[i][j+1])
		r = max(r, dp1[i][j-1] + dp2[i][j+1] + dp3[i+1][j] + dp4[i-1][j])
		#print(r, i, j)
print(r)



import sys
from math import *

def minp():
	return sys.stdin.readline().strip()

def mint():
	return int(minp())

def mints():
	return list(map(int, minp().split()))

n, m = mints()
a = [0]*n
dp1 = [0]*n
dp2 = [0]*n
dp3 = [0]*n
dp4 = [0]*n
for i in range(n):
	a[i] = list(mints())
	dp1[i] = [0]*m
	dp2[i] = [0]*m
	dp3[i] = [0]*m
	dp4[i] = [0]*m

dp1[0][0] = a[0][0]
dp2[n-1][m-1] = a[n-1][m-1]
dp3[n-1][0] = a[n-1][0]
dp4[0][m-1] = a[0][m-1]
for i in range(1,n):
	dp1[i][0] = dp1[i-1][0] + a[i][0] # >>>>
for i in range(n-2,-1,-1):
	dp2[i][m-1] = dp2[i+1][m-1] + a[i][m-1] # <<<<
for i in range(n-2,-1,-1):
	dp3[i][0] = dp3[i+1][0] + a[i][0] # <<<<
for i in range(1,n):
	dp4[i][m-1] = dp4[i-1][m-1] + a[i][m-1] # >>>>
for i in range(1,m):
	dp1[0][i] = dp1[0][i-1] + a[0][i] # >>>>
for i in range(m-2,-1,-1):
	dp2[n-1][i] = dp2[n-1][i+1] + a[n-1][i] # <<<<
for i in range(1,m):
	dp3[n-1][i] = dp3[n-1][i-1] + a[n-1][i] # >>>>
for i in range(m-2,-1,-1):
	dp4[0][i] = dp4[0][i+1] + a[0][i] # >>>>
for i in range(1,n):
	for j in range(1,m):
		dp1[i][j] = max(dp1[i-1][j], dp1[i][j-1]) + a[i][j]
for i in range(n-2,-1,-1):
	for j in range(m-2,-1,-1):
		dp2[i][j] = max(dp2[i+1][j], dp2[i][j+1]) + a[i][j]
for i in range(n-2,-1,-1):
	for j in range(1,m):
		dp3[i][j] = max(dp3[i+1][j], dp3[i][j-1]) + a[i][j]
for i in range(1,n):
	for j in range(m-2,-1,-1):
		dp4[i][j] = max(dp4[i-1][j], dp4[i][j+1]) + a[i][j]
r = 0
for i in range(1,n-1):
	for j in range(1,m-1):
		r = max(r, dp1[i-1][j] + dp2[i+1][j] + dp3[i][j-1] + dp4[i][j+1])
		r = max(r, dp1[i][j-1] + dp2[i][j+1] + dp3[i+1][j] + dp4[i-1][j])
print(r)



n, m = list(map(int, input().split()))
gym = [[0 for i in range(m+1)]]
for row in range(n):
    gym.append([0] + list(map(int, input().split())))

bToMid = [[0 for i in range(m+2)] for j in range(n+2)]
for i in range(1, n+1):
    for j in range(1, m+1):
        bToMid[i][j] = gym[i][j] + max(bToMid[i-1][j], bToMid[i][j-1])
bToEnd = [[0 for i in range(m+2)] for j in range(n+2)]
for i in range(n, 0, -1):
    for j in range(m, 0, -1):
        bToEnd[i][j] = gym[i][j] + max(bToEnd[i+1][j], bToEnd[i][j+1])
aToMid = [[0 for i in range(m+2)] for j in range(n+2)]
for i in range(n, 0, -1):
    for j in range(1, m+1):
        aToMid[i][j] = gym[i][j] + max(aToMid[i+1][j], aToMid[i][j-1])
aToEnd = [[0 for i in range(m+2)] for j in range(n+2)]
for i in range(1, n+1):
    for j in range(m, 0, -1):
        aToEnd[i][j] = gym[i][j] + max(aToEnd[i-1][j], aToEnd[i][j+1])
#print(bToMid[1][2], bToEnd[3][2], aToMid[2][1], aToEnd[2][3])
best = 0
bestIJ = ()
for i in range(2, n):
    for j in range(2, m):
        best = max(best, bToMid[i][j-1]+bToEnd[i][j+1]+aToMid[i+1][j]+aToEnd[i-1][j])
        best = max(best, bToMid[i-1][j]+bToEnd[i+1][j]+aToMid[i][j-1]+aToEnd[i][j+1])
        bestIJ = (i, j)
print(best)
#print(bestIJ)


# -*- coding:utf-8 -*-

"""

created by shuangquan.huang at 1/7/20

"""

import collections
import time
import os
import sys
import bisect
import heapq
from typing import List


def solve(N, M, A):
    dpa = [[0 for _ in range(M+2)] for _ in range(N+2)]
    dpb = [[0 for _ in range(M+2)] for _ in range(N+2)]
    dpc = [[0 for _ in range(M+2)] for _ in range(N+2)]
    dpd = [[0 for _ in range(M+2)] for _ in range(N+2)]
    
    for r in range(1, N+1):
        for c in range(1, M + 1):
            dpa[r][c] = max(dpa[r-1][c], dpa[r][c-1]) + A[r][c]
    
    for r in range(N, 0, -1):
        for c in range(M, 0, -1):
            dpb[r][c] = max(dpb[r+1][c], dpb[r][c+1]) + A[r][c]
    
    for r in range(N, 0, -1):
        for c in range(1, M+1):
            dpc[r][c] = max(dpc[r+1][c], dpc[r][c-1]) + A[r][c]
    
    for r in range(1, N+1):
        for c in range(M, 0, -1):
            dpd[r][c] = max(dpd[r-1][c], dpd[r][c+1]) + A[r][c]

    ans = 0
    for r in range(2, N):
        for c in range(2, M):
            a = dpa[r][c-1] + dpb[r][c+1] + dpc[r+1][c] + dpd[r-1][c]
            b = dpc[r][c-1] + dpd[r][c+1] + dpa[r-1][c] + dpb[r+1][c]
            ans = max(ans, a, b)
            
    return ans


N, M = map(int, input().split())
A = [[0 for _ in range(M+2)]]
for i in range(N):
    row = [0] + [int(x) for x in input().split()] + [0]
    A.append(row)
A.append([0 for _ in range(M+2)])

print(solve(N, M, A))

n, m = list(map(int, input().strip().split()))
dp1, dp2, dp3, dp4 = [[[0 for i in range(m+1)] for i in range(n+1)] for i in range(4)]

# print(dp1)
# print(dp2)
# print(dp3)
# print(dp4)

a = []
for i in range(n):
    a.append(list(map(int, input().strip().split())))

for i in range(n):
    for j in range(m):
        dp1[i][j] = a[i][j] + max(dp1[i-1][j], dp1[i][j-1])

for i in range(n-1, -1, -1):
    for j in range(m-1, -1, -1):
        dp2[i][j] = a[i][j] + max(dp2[i+1][j], dp2[i][j+1])

for i in range(n-1, -1, -1):
    for j in range(m):
        dp3[i][j] = a[i][j] + max(dp3[i+1][j], dp3[i][j-1])

for i in range(n):
    for j in range(m-1, -1, -1):
        dp4[i][j] = a[i][j] + max(dp4[i-1][j], dp4[i][j+1])

# print("#############")

# for i in dp1:
#     print(i)
# print("-----------")

# for i in dp2:
#     print(i)
# print("-----------")

# for i in dp3:
#     print(i)
# print("-----------")

# for i in dp4:
#     print(i)

# print("#############")

ans = 0
for i in range(1,n-1):
    for j in range(1, m-1):
        ans = max(ans, dp1[i][j-1] + dp2[i][j+1] + dp3[i+1][j] + dp4[i-1][j], dp3[i][j-1] + dp4[i][j+1] + dp1[i-1][j] + dp2[i+1][j])
        # print(dp1[i][j-1],dp2[i][j+1], dp3[i+1][j], dp4[i-1][j], dp3[i][j-1], dp4[i][j+1], dp1[i+1][j], dp2[i-1][j])
print(ans)












from sys import stdin,stdout
import sys
from bisect import bisect_left,bisect_right
import heapq
sys.setrecursionlimit(2*(10**5))

# stdin = open("input.txt", "r");
# stdout = open("output.txt", "w");

n,m=stdin.readline().strip().split(' ')
n,m=int(n),int(m)

costarr=[]
for i in range(n):
	costarr.append(list(map(int,stdin.readline().strip().split(' '))))




dp_tl_br=[[0 for i in range(m)] for j in range(n)]
dp_br_tl=[[0 for i in range(m)] for j in range(n)]
dp_bl_tr=[[0 for i in range(m)] for j in range(n)]
dp_tr_bl=[[0 for i in range(m)] for j in range(n)]

#	TOP LEFT TO BOTTOM RIGHT COST
dp_tl_br[0][0]=costarr[0][0]
for i in range(1,m):
	dp_tl_br[0][i]=dp_tl_br[0][i-1]+costarr[0][i]
for i in range(1,n):
	dp_tl_br[i][0]=dp_tl_br[i-1][0]+costarr[i][0]
for i in range(1,n):
	for j in range(1,m):
		dp_tl_br[i][j]=max(dp_tl_br[i][j-1],dp_tl_br[i-1][j])+costarr[i][j]


#	BOTTOM RIGHT TO TOP LEFT COST
dp_br_tl[n-1][m-1]=costarr[n-1][m-1]
for i in range(m-2,-1,-1):
	dp_br_tl[n-1][i]=dp_br_tl[n-1][i+1]+costarr[n-1][i]
for i in range(n-2,-1,-1):
	dp_br_tl[i][m-1]=dp_br_tl[i+1][m-1]+costarr[i][m-1]
for i in range(n-2,-1,-1):
	for j in range(m-2,-1,-1):
		dp_br_tl[i][j]=max(dp_br_tl[i][j+1],dp_br_tl[i+1][j])+costarr[i][j]


#	BOTTOM LEFT TO TOP RIGHT COST
dp_bl_tr[n-1][0]=costarr[n-1][0]
for i in range(1,m):
	dp_bl_tr[n-1][i]=dp_bl_tr[n-1][i-1]+costarr[n-1][i]
for i in range(n-2,-1,-1):
	dp_bl_tr[i][0]=dp_bl_tr[i+1][0]+costarr[i][0]
for i in range(n-2,-1,-1):
	for j in range(1,m):
		dp_bl_tr[i][j]=max(dp_bl_tr[i][j-1],dp_bl_tr[i+1][j])+costarr[i][j]


#	TOP RIGHT TO BOTTOM LEFT COST
dp_tr_bl[0][m-1]=costarr[0][m-1]
for i in range(m-2,-1,-1):
	dp_tr_bl[0][i]=dp_tr_bl[0][i+1]+costarr[0][i]
for i in range(1,n):
	dp_tr_bl[i][m-1]=dp_tr_bl[i-1][m-1]+costarr[i][m-1]
for i in range(1,n):
	for j in range(m-2,-1,-1):
		dp_tr_bl[i][j]=max(dp_tr_bl[i][j+1],dp_tr_bl[i-1][j])+costarr[i][j]



def sh(arr):
	for i in arr:
		print(i)

# sh(dp_tr_bl)
# print()

# sh(dp_tl_br)
# print()

# sh(dp_bl_tr)
# print()

# sh(dp_br_tl)
# print()

ans=0

for i in range(1,n-1):
	for j in range(1,m-1):
		ans=max(ans,dp_bl_tr[i][j-1]+dp_tr_bl[i][j+1]+dp_tl_br[i-1][j]+dp_br_tl[i+1][j])#	LEFT TO RIGHT | DOWN TO UP
		ans=max(ans,dp_bl_tr[i+1][j]+dp_tr_bl[i-1][j]+dp_tl_br[i][j-1]+dp_br_tl[i][j+1])# DOWN TO UP | LEFT TO RIGHT
# for i in range(1,n-2):
# 	for j in range(m):
# 		ans+=max(ans,dp_tl_br[i-1][j]+costarr[i][j]+dp_br_tl[i+1][j]+dp_bl_tr[i+1][j]+costarr[i][j]+dp_tr_bl[i-1][j])

stdout.write(str(ans)+"\n")






















n,m=map(int,input().split())
a=[]
for i in range(n):a.append(list(map(int,input().split())))
dpa=[[[0,0] for i in range(m+2)] for i in range(n+2)]
dpb=[[[0,0] for i in range(m+2)] for i in range(n+2)]
ans=0
for i in range(1,n+1):
	for j in range(1,m+1):
		dpa[i][j][0]=max(dpa[i-1][j][0],dpa[i][j-1][0])+a[i-1][j-1]
		dpa[n+1-i][m+1-j][1]=max(dpa[n+2-i][m+1-j][1],dpa[n+1-i][m+2-j][1])+a[n-i][m-j]
for i in range(n,0,-1):
	for j in range(1,m+1):
		dpb[i][j][0]=max(dpb[i+1][j][0],dpb[i][j-1][0])+a[i-1][j-1]
		dpb[n+1-i][m+1-j][1]=max(dpb[n-i][m+1-j][1],dpb[n+1-i][m+2-j][1])+a[n-i][m-j]
for i in range(2,n):
	for j in range(2,m):
		x=dpa[i-1][j][0]+dpa[i+1][j][1]+dpb[i][j-1][0]+dpb[i][j+1][1]
		y=dpb[i+1][j][0]+dpb[i-1][j][1]+dpa[i][j-1][0]+dpa[i][j+1][1]
		ans=max(ans,x,y)
print(ans)

R = lambda: map(int, input().split())
n, m = R()
g = [list() for i in range(n)]
for i in range(n):
    g[i] = list(R())
dp1, dp2, dp3, dp4 = ([[0] * (m + 1) for j in range(n + 1)] for i in range(4))
for i in range(n):
    for j in range(m):
        dp1[i][j] = g[i][j] + max(dp1[i - 1][j], dp1[i][j - 1])
    for j in range(m - 1, -1, -1):
        dp2[i][j] = g[i][j] + max(dp2[i - 1][j], dp2[i][j + 1])
for i in range(n - 1, -1, -1):
    for j in range(m):
        dp3[i][j] = g[i][j] + max(dp3[i + 1][j], dp3[i][j - 1])
    for j in range(m - 1, -1, -1):
        dp4[i][j] = g[i][j] + max(dp4[i + 1][j], dp4[i][j + 1])
print(max(max(dp1[i][j - 1] + dp2[i - 1][j] + dp3[i + 1][j] + dp4[i][j + 1], dp1[i - 1][j] + dp2[i][j + 1] + dp3[i][j - 1] + dp4[i + 1][j]) for j in range(1, m - 1) for i in range(1, n - 1)))

import sys
input=sys.stdin.readline
R = lambda: map(int, input().split())
n, m = R()
g = [list() for i in range(n)]
for i in range(n):
    g[i] = list(R())
dp1, dp2, dp3, dp4 = ([[0] * (m + 1) for j in range(n + 1)] for i in range(4))
for i in range(n):
    for j in range(m):
        dp1[i][j] = g[i][j] + max(dp1[i - 1][j], dp1[i][j - 1])
    for j in range(m - 1, -1, -1):
        dp2[i][j] = g[i][j] + max(dp2[i - 1][j], dp2[i][j + 1])
for i in range(n - 1, -1, -1):
    for j in range(m):
        dp3[i][j] = g[i][j] + max(dp3[i + 1][j], dp3[i][j - 1])
    for j in range(m - 1, -1, -1):
        dp4[i][j] = g[i][j] + max(dp4[i + 1][j], dp4[i][j + 1])
print(max(max(dp1[i][j - 1] + dp2[i - 1][j] + dp3[i + 1][j] + dp4[i][j + 1], dp1[i - 1][j] + dp2[i][j + 1] + dp3[i][j - 1] + dp4[i + 1][j]) for j in range(1, m - 1) for i in range(1, n - 1)))


from collections import defaultdict
def solve():

    n,m = list(map(int,input().split()))
    la = []
    for i in range(n):
        z = list(map(int,input().split()))
        la.append(z)

    dp1, dp2, dp3, dp4 = [[[0 for i in range(m+1)] for i in range(n+1)] for i in range(4)]

    for i in range(n):
        for j in range(m):
            dp1[i][j] = la[i][j]
            z1,z2 = 0,0
            if i-1>=0:
                z1 = dp1[i-1][j]
            if j-1>=0:
                z2 = dp1[i][j-1]

            dp1[i][j]+=max(z1,z2)

    for i in range(n-1,-1,-1):
        for j in range(m):
            dp2[i][j] = la[i][j]
            z1,z2 = 0,0
            if i+1<n:
                z1 = dp2[i+1][j]
            if j-1>=0:
                z2 = dp2[i][j-1]

            dp2[i][j]+=max(z1,z2)

    for i in range(n-1,-1,-1):
        for j in range(m-1,-1,-1):
            dp3[i][j] = la[i][j]
            z1,z2 = 0,0
            if i+1<n:
                z1 = dp3[i+1][j]
            if j+1<m:
                z2 = dp3[i][j+1]

            dp3[i][j]+=max(z1,z2)

    for i in range(n):
        for j in range(m-1,-1,-1):
            dp4[i][j] = la[i][j]
            z1,z2 = 0,0
            if i-1>=0:
                z1 = dp4[i-1][j]
            if j+1<m:
                z2 = dp4[i][j+1]

            dp4[i][j]+=max(z1,z2)

    ans = 0
    # print(dp1)
    # print(dp2)
    for i in range(1,n-1):
        for j in range(1,m-1):
            z1,z2,z3,z4 = dp1[i][j-1],dp2[i+1][j],dp3[i][j+1],dp4[i-1][j]

            ans = max(z1+z2+z3+z4,ans)
            z1,z2,z3,z4 = dp1[i-1][j],dp2[i][j-1],dp3[i+1][j],dp4[i][j+1]
            ans = max(z1+z2+z3+z4,ans)



            # print(ans)

    print(ans)




# t = int(stdin.readline())
# for _ in range(t):
solve()