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 mcolumns. 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] ≤ 105).

Output

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

Example

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

#include<iostream>
#include<cstring>
#include<cstdio> using namespace std;
const int N = + ;
int mat[N][N], a[N][N], b[N][N], c[N][N], d[N][N]; int main(){
int n, m;
scanf("%d %d", &n, &m);
for(int i = ; i <= n; i++)
for(int j = ; j <= m; j++) scanf("%d", &mat[i][j]); for(int i = ; i <= n; i++)
for(int j = ; j <= m; j++) a[i][j] = max(a[i-][j], a[i][j-]) + mat[i][j]; for(int i = n; i > ; i--)
for(int j = m; j > ; j--) b[i][j] = max(b[i+][j], b[i][j+]) + mat[i][j]; for(int i = n; i > ; i--)
for(int j = ; j <= m; j++) c[i][j] = max(c[i+][j], c[i][j-]) + mat[i][j]; for(int i = ; i <= n; i++)
for(int j = m; j > ; j--) d[i][j] = max(d[i-][j], d[i][j+]) + mat[i][j];
int ans = ;
for(int i = ; i < n; i++)
for(int j = ; j < m; j++)
ans = max(ans, a[i-][j] + b[i+][j] + c[i][j-] + d[i][j+]),
ans = max(ans, a[i][j-] + b[i][j+] + c[i+][j] + d[i-][j]);
printf("%d\n", ans);
}

最新文章

  1. mac office 激活
  2. python操作mysql数据库
  3. jquery学习——遍历
  4. Android系列之网络(三)----使用HttpClient发送HTTP请求(分别通过GET和POST方法发送数据)
  5. Gym 100827G Number Game (博弈)
  6. python使用PIL压缩图片
  7. Win7系统安装MySQL5.5.21图解教程
  8. Leetcode题解(28)
  9. Smarty基础用法
  10. oracle pl/sql中的循环及if语句
  11. 【Luogu1876】开灯(数论)
  12. python爬虫动态html selenium.webdriver
  13. 使用node.js 脚手架搭建Vue项目
  14. FUTABA 13-ST-84GINK + DS3231 时钟
  15. 【Mysql】常用命令
  16. c# async和await 用法(阻塞与不阻塞)
  17. vue router history模式开发ngnix配置
  18. 关于CSS3的filter(滤镜) 属性
  19. SVN的Branch和Tag管理
  20. java static方法不能被重写@Override

热门文章

  1. Java面试框架篇(8)
  2. XML 简介 – 什么是 XML?
  3. dell笔记本 win10 下安装 ubuntu16.04 踩坑记录
  4. 【转】django rest framework ModelSerializer 、serializers小结
  5. windows 安装 Mongodb 数据库及操作图形化软件 Robo 3T
  6. selenium+键盘鼠标
  7. 基于数组阻塞队列 ArrayBlockingQueue 的一个队列工具类
  8. oracle调整链接数
  9. vue 自己写的一个日历
  10. 2018-2019-2 网络对抗技术 20165220 Exp 9 Web安全基础