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].

Source: codeforces | Difficulty: MEDIUM_HARD
Tags: Dynamic programming


Solutions:
(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])
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)


(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)


(n, m) = list(map(int, input().split()))
a = [list(map(int, input().split())) for i in range(n)]
b = [[0 for j in range(m)] for i in range(n)]
b[0][0] = a[0][0]
c = [[0 for j in range(m)] for i in range(n)]
c[n - 1][0] = a[n - 1][0]
d = [[0 for j in range(m)] for i in range(n)]
d[0][m - 1] = a[0][m - 1]
e = [[0 for j in range(m)] for i in range(n)]
e[n - 1][m - 1] = a[n - 1][m - 1]
for i in range(n):
	for j in range(m):
		if i == 0:
			if j != 0:
				b[i][j] = b[i][j - 1] + a[i][j]
		elif j == 0:
			b[i][j] = b[i - 1][j] + a[i][j]
		else:
			b[i][j] = max(b[i - 1][j], b[i][j - 1]) + a[i][j]
for i in range(n - 1, -1, -1):
	for j in range(m):
		if i == n - 1:
			if j != 0:
				c[i][j] = c[i][j - 1] + a[i][j]
		elif j == 0:
			c[i][j] = c[i + 1][j] + a[i][j]
		else:
			c[i][j] = max(c[i + 1][j], c[i][j - 1]) + a[i][j]
for i in range(n):
	for j in range(m - 1, -1, -1):
		if i == 0:
			if j != m - 1:
				d[i][j] = d[i][j + 1] + a[i][j]
		elif j == m - 1:
			d[i][j] = d[i - 1][j] + a[i][j]
		else:
			d[i][j] = max(d[i - 1][j], d[i][j + 1]) + a[i][j]
for i in range(n - 1, -1, -1):
	for j in range(m - 1, -1, -1):
		if i == n - 1:
			if j != m - 1:
				e[i][j] = e[i][j + 1] + a[i][j]
		elif j == m - 1:
			e[i][j] = e[i + 1][j] + a[i][j]
		else:
			e[i][j] = max(e[i + 1][j], e[i][j + 1]) + a[i][j]
f = 0
for i in range(n):
	for j in range(m):
		g1 = int(b[i][j - 1] if j > 0 else -1000000000000) + int(c[i + 1][j] if i < n - 1 else -1000000000000) + int(d[i - 1][j] if i > 0 else -1000000000000) + int(e[i][j + 1] if j < m - 1 else -1000000000000)
		g2 = int(b[i - 1][j] if i > 0 else -1000000000000) + int(c[i][j - 1] if j > 0 else -1000000000000) + int(d[i][j + 1] if j < m - 1 else -1000000000000) + int(e[i + 1][j] if i < n - 1 else -1000000000000)
		f = max(f, g1, g2)
print(f)


(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)]
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])
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(ans)


from __future__ import division, print_function
from fractions import Fraction
import sys
import os
from io import BytesIO, IOBase
from functools import cmp_to_key
from heapq import *
from math import gcd, factorial, floor, ceil, sqrt
from copy import deepcopy
from collections import deque
from bisect import bisect_left as bl
from bisect import bisect_right as br
from bisect import bisect
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
from types import GeneratorType

def iterative(f, stack=[]):

	def wrapped_func(*args, **kwargs):
		if stack:
			return f(*args, **kwargs)
		to = f(*args, **kwargs)
		while True:
			if type(to) is GeneratorType:
				stack.append(to)
				to = next(to)
				continue
			stack.pop()
			if not stack:
				break
			to = stack[-1].send(to)
		return to
	return wrapped_func

def inp():
	return sys.stdin.readline().rstrip('\r\n')

def out(var):
	sys.stdout.write(str(var))

def lis():
	return list(map(int, inp().split()))

def stringlis():
	return list(map(str, inp().split()))

def sep():
	return map(int, inp().split())

def strsep():
	return map(str, inp().split())

def testcase(t):
	for pp in range(t):
		solve(pp)

def google(p):
	print('Case #' + str(p) + ': ', end='')

def lcm(a, b):
	return a * b // gcd(a, b)

def power(x, y, p):
	y %= p - 1
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def ncr(n, r):
	return factorial(n) // (factorial(r) * factorial(max(n - r, 1)))

def isPrime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	i = 5
	while i * i <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return False
		i = i + 6
	return True
inf = pow(10, 20)
mod = 10 ** 9 + 7

def safe(x, y):
	global n, m
	return 0 <= x < n and 0 <= y < m

def solve(case):
	global n, m
	(n, m) = sep()
	g = []
	for i in range(n):
		tmep = lis()
		g.append(tmep)
	dp1 = [[0] * m for i in range(n)]
	dp2 = [[0] * m for i in range(n)]
	dp3 = [[0] * m for i in range(n)]
	dp4 = [[0] * m for i in range(n)]
	dp1[0][0] = g[0][0]
	for i in range(n):
		for j in range(m):
			if i == 0 and j == 0:
				continue
			dp1[i][j] = g[i][j]
			if safe(i - 1, j) and safe(i, j - 1):
				dp1[i][j] += max(dp1[i - 1][j], dp1[i][j - 1])
			elif safe(i - 1, j):
				dp1[i][j] += dp1[i - 1][j]
			else:
				dp1[i][j] += dp1[i][j - 1]
	for i in range(n - 1, -1, -1):
		for j in range(m - 1, -1, -1):
			if i == n - 1 and j == m - 1:
				dp2[i][j] = g[i][j]
				continue
			dp2[i][j] = g[i][j]
			if safe(i, j + 1) and safe(i + 1, j):
				dp2[i][j] += max(dp2[i][j + 1], dp2[i + 1][j])
			elif safe(i + 1, j):
				dp2[i][j] += dp2[i + 1][j]
			else:
				dp2[i][j] += dp2[i][j + 1]
	for i in range(n - 1, -1, -1):
		for j in range(m):
			if i == n - 1 and j == 0:
				dp3[i][j] = g[i][j]
				continue
			dp3[i][j] = g[i][j]
			if safe(i, j - 1) and safe(i + 1, j):
				dp3[i][j] += max(dp3[i][j - 1], dp3[i + 1][j])
			elif safe(i + 1, j):
				dp3[i][j] += dp3[i + 1][j]
			else:
				dp3[i][j] += dp3[i][j - 1]
	for i in range(n):
		for j in range(m - 1, -1, -1):
			if i == 0 and j == m - 1:
				dp4[i][j] = g[i][j]
				continue
			dp4[i][j] = g[i][j]
			if safe(i - 1, j) and safe(i, j + 1):
				dp4[i][j] += max(dp4[i - 1][j], dp4[i][j + 1])
			elif safe(i - 1, j):
				dp4[i][j] += dp4[i - 1][j]
			else:
				dp4[i][j] += dp4[i][j + 1]
	ans = -inf
	for i in range(1, n - 1):
		for j in range(1, m - 1):
			temp = dp1[i - 1][j] + dp2[i + 1][j] + dp3[i][j - 1] + dp4[i][j + 1]
			ans = max(ans, temp)
			temp = dp1[i][j - 1] + dp2[i][j + 1] + dp3[i + 1][j] + dp4[i - 1][j]
			ans = max(ans, temp)
	print(ans)
(n, m) = (0, 0)
testcase(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(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)


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
	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)
solve()


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
(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)
dp2 = create_dp2(matrix, n, m)
dp3 = create_dp3(matrix, n, m)
dp4 = create_dp4(matrix, n, 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)


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))))


import sys
from functools import lru_cache, cmp_to_key
from heapq import merge, heapify, heappop, heappush
from collections import defaultdict as dd, deque, Counter as C
from itertools import combinations as comb, permutations as perm
from bisect import bisect_left as bl, bisect_right as br, bisect
from time import perf_counter
from fractions import Fraction
import copy
import time
starttime = time.time()
mod = int(pow(10, 9) + 7)
mod2 = 998244353
from sys import stdin
input = stdin.readline

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

def out(*var, end='\n'):
	sys.stdout.write(' '.join(map(str, var)) + end)

def L():
	return list(sp())

def sl():
	return list(ssp())

def sp():
	return map(int, data().split())

def ssp():
	return map(str, data().split())

def l1d(n, val=0):
	return [val for i in range(n)]

def l2d(n, m, val=0):
	return [l1d(n, val) for j in range(m)]
try:
	sys.stdin = open('input.txt', 'r')
except:
	pass
(n, m) = L()
A = [L() for i in range(n)]
A1 = [[0 for i in range(m + 2)] for j in range(n + 2)]
A2 = [[0 for i in range(m + 2)] for j in range(n + 2)]
A3 = [[0 for i in range(m + 2)] for j in range(n + 2)]
A4 = [[0 for i in range(m + 2)] for j in range(n + 2)]
for i in range(1, 1 + n):
	for j in range(1, 1 + m):
		A1[i][j] = max(A1[i][j - 1], A1[i - 1][j]) + A[i - 1][j - 1]
for i in range(n, 0, -1):
	for j in range(m, 0, -1):
		A2[i][j] = max(A2[i][j + 1], A2[i + 1][j]) + A[i - 1][j - 1]
for i in range(n, 0, -1):
	for j in range(1, 1 + m):
		A3[i][j] = max(A3[i][j - 1], A3[i + 1][j]) + A[i - 1][j - 1]
for i in range(1, 1 + n):
	for j in range(m, 0, -1):
		A4[i][j] = max(A4[i][j + 1], A4[i - 1][j]) + A[i - 1][j - 1]
ans = 0
for i in range(2, n):
	for j in range(2, m):
		ans = max(ans, A1[i][j - 1] + A2[i][j + 1] + A3[i + 1][j] + A4[i - 1][j])
		ans = max(ans, A1[i - 1][j] + A2[i + 1][j] + A3[i][j - 1] + A4[i][j + 1])
print(ans)
endtime = time.time()


from sys import maxsize, stdout, stdin, stderr
mod = int(1000000000.0 + 7)
import sys

def I():
	return int(stdin.readline())

def lint():
	return [int(x) for x in stdin.readline().split()]

def S():
	return input().strip()

def grid(r, c):
	return [lint() for i in range(r)]
from collections import defaultdict, Counter, deque
import math
import heapq
from heapq import heappop, heappush
import bisect
from itertools import groupby

def gcd(a, b):
	while b:
		a %= b
		tmp = a
		a = b
		b = tmp
	return a

def lcm(a, b):
	return a // gcd(a, b) * b

def check_prime(n):
	for i in range(2, int(n ** (1 / 2)) + 1):
		if not n % i:
			return False
	return True

def Bs(a, x):
	i = 0
	j = 0
	left = 1
	right = x
	flag = False
	while left < right:
		mi = (left + right) // 2
		if a[mi] <= x:
			left = mi + 1
			i += 1
		else:
			right = mi
			j += 1
	if left > 0 and a[left - 1] == x:
		return (i - 1, j)
	else:
		return (-1, -1)

def nCr(n, r):
	return fact(n) // (fact(r) * fact(n - r))

def fact(n):
	res = 1
	for i in range(2, n + 1):
		res = res * i
	return res

def primefactors(n):
	num = 0
	while n % 2 == 0:
		num += 1
		n = n / 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			num += 1
			n = n // i
	if n > 2:
		num += 1
	return num

def ask(a):
	print('? {}'.format(a), flush=True)
	n = I()
	return n

def dfs(i, p):
	(a, tmp) = (0, 0)
	for j in d[i]:
		if j != p:
			a += 1
			tmp += dfs(j, i)
	if a == 0:
		return 0
	return tmp / a + 1

def primeFactors(n):
	l = []
	while n % 2 == 0:
		l.append(2)
		n = n // 2
	if n > 2:
		l.append(n)
	return l
(n, m) = lint()
a = []
dp1 = [[0] * (m + 2) for _ in range(n + 2)]
dp2 = [[0] * (m + 2) for _ in range(n + 2)]
dp3 = [[0] * (m + 2) for _ in range(n + 2)]
dp4 = [[0] * (m + 2) for _ in range(n + 2)]
ans = 0
for i in range(n):
	a.append(lint())
for i in range(1, n + 1):
	for j in range(1, m + 1):
		dp1[i][j] = a[i - 1][j - 1] + max(dp1[i - 1][j], dp1[i][j - 1])
for i in range(n, 0, -1):
	for j in range(1, m + 1):
		dp2[i][j] = a[i - 1][j - 1] + max(dp2[i + 1][j], dp2[i][j - 1])
for i in range(1, n + 1):
	for j in range(m, 0, -1):
		dp3[i][j] = a[i - 1][j - 1] + max(dp3[i - 1][j], dp3[i][j + 1])
for i in range(n, 0, -1):
	for j in range(m, 0, -1):
		dp4[i][j] = a[i - 1][j - 1] + max(dp4[i + 1][j], dp4[i][j + 1])
for i in range(2, n):
	for j in range(2, m):
		tm = dp1[i - 1][j] + dp2[i][j - 1] + dp3[i][j + 1] + dp4[i + 1][j]
		pm = dp1[i][j - 1] + dp2[i + 1][j] + dp3[i - 1][j] + dp4[i][j + 1]
		ans = max(ans, max(tm, pm))
print(ans)


import os, sys
from io import BytesIO, IOBase

def main():
	(n, m) = map(int, input().split())
	arr = [list(map(int, input().split())) for _ in range(n)]
	dp1 = [[0] * (m + 2) for _ in range(n + 2)]
	for i in range(1, n + 1):
		for j in range(1, m + 1):
			dp1[i][j] = max(dp1[i - 1][j], dp1[i][j - 1]) + arr[i - 1][j - 1]
	dp2 = [[0] * (m + 2) for _ in range(n + 2)]
	for i in range(1, n + 1):
		for j in range(m, 0, -1):
			dp2[i][j] = max(dp2[i - 1][j], dp2[i][j + 1]) + arr[i - 1][j - 1]
	dp3 = [[0] * (m + 2) for _ in range(n + 2)]
	for i in range(n, 0, -1):
		for j in range(m, 0, -1):
			dp3[i][j] = max(dp3[i + 1][j], dp3[i][j + 1]) + arr[i - 1][j - 1]
	dp4 = [[0] * (m + 2) for _ in range(n + 2)]
	for i in range(n, 0, -1):
		for j in range(1, m + 1):
			dp4[i][j] = max(dp4[i + 1][j], dp4[i][j - 1]) + arr[i - 1][j - 1]
	maxi = 0
	for i in range(2, n):
		for j in range(2, m):
			maxi = max(maxi, dp1[i][j - 1] + dp4[i + 1][j] + dp2[i - 1][j] + dp3[i][j + 1], dp1[i - 1][j] + dp4[i][j - 1] + dp2[i][j + 1] + dp3[i + 1][j])
	print(maxi)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


from collections import deque, defaultdict
from math import sqrt, ceil, factorial, floor, inf, log2, sqrt
import bisect
import copy
from itertools import combinations
import sys

def get_array():
	return list(map(int, sys.stdin.readline().strip().split()))

def get_ints():
	return map(int, sys.stdin.readline().strip().split())

def input():
	return sys.stdin.readline().strip()
arr = []
(n, m) = get_ints()
for i in range(n):
	a = get_array()
	arr.append(a)
dp = [[0 for i in range(m)] for j in range(n)]
dp1 = [[0 for i in range(m)] for j in range(n)]
dp2 = [[0 for i in range(m)] for j in range(n)]
dp3 = [[0 for i in range(m)] for j in range(n)]
dp[0][0] = arr[0][0]
for i in range(1, m):
	dp[0][i] += dp[0][i - 1] + arr[0][i]
for j in range(1, n):
	dp[j][0] += dp[j - 1][0] + arr[j][0]
for i in range(1, n):
	for j in range(1, m):
		dp[i][j] += max(dp[i - 1][j], dp[i][j - 1]) + arr[i][j]
dp1[n - 1][0] = arr[n - 1][0]
for i in range(1, m):
	dp1[n - 1][i] += dp1[n - 1][i - 1] + arr[n - 1][i]
for i in range(n - 2, -1, -1):
	dp1[i][0] += dp1[i + 1][0] + arr[i][0]
for i in range(n - 2, -1, -1):
	for j in range(1, m):
		dp1[i][j] += max(dp1[i + 1][j], dp1[i][j - 1]) + arr[i][j]
dp2[n - 1][m - 1] = arr[n - 1][m - 1]
for i in range(m - 2, -1, -1):
	dp2[n - 1][i] += dp2[n - 1][i + 1] + arr[n - 1][i]
for i in range(n - 2, -1, -1):
	dp2[i][m - 1] += dp2[i + 1][m - 1] + arr[i][m - 1]
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]) + arr[i][j]
dp3[0][m - 1] = arr[0][m - 1]
for i in range(m - 2, -1, -1):
	dp3[0][i] += dp3[0][i + 1] + arr[0][i]
for i in range(1, n):
	dp3[i][m - 1] += dp3[i - 1][m - 1] + arr[i][m - 1]
for i in range(1, n):
	for j in range(m - 2, -1, -1):
		dp3[i][j] = max(dp3[i - 1][j], dp3[i][j + 1]) + arr[i][j]
ans = -1
for i in range(n):
	for j in range(m):
		(maxi, maxi1) = (-1, -1)
		if i - 1 >= 0 and j - 1 >= 0 and (i + 1 < n) and (j + 1 < m):
			(maxi, maxi1) = (-1, -1)
			maxi = max(maxi, dp[i - 1][j] + dp1[i][j - 1])
			maxi1 = max(maxi1, dp2[i + 1][j] + dp3[i][j + 1])
			ans = max(ans, maxi + maxi1)
		if i + 1 < n and j - 1 >= 0 and (j + 1 < m) and (i - 1 >= 0):
			(maxi, maxi1) = (-1, -1)
			maxi = max(maxi, dp[i][j - 1] + dp1[i + 1][j])
			maxi1 = max(maxi1, dp2[i][j + 1] + dp3[i - 1][j])
			ans = max(ans, maxi + maxi1)
print(ans)


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()


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))


from sys import stdin, stdout
import sys
from bisect import bisect_left, bisect_right
import heapq
sys.setrecursionlimit(2 * 10 ** 5)
(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)]
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]
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]
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]
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)
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])
		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])
stdout.write(str(ans) + '\n')