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] ≤ 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 <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <map>
#define Max 1001
using namespace std; int mp[][];
int dp1[][],dp2[][],dp3[][],dp4[][];
int ma = ;
int main()
{
int n,m;
cin>>n>>m;
for(int i = ;i <= n;i ++)for(int j = ;j <= m;j ++)cin>>mp[i][j];
for(int i = ;i <= n;i ++)for(int j = ;j <= m;j ++)dp1[i][j] = max(dp1[i-][j],dp1[i][j-])+mp[i][j];
for(int i = n;i >= ;i --)for(int j = m;j >= ;j --)dp2[i][j] = max(dp2[i+][j],dp2[i][j+])+mp[i][j];
for(int i = ;i <= n;i ++)for(int j = m;j >= ;j --)dp3[i][j] = max(dp3[i-][j],dp3[i][j+])+mp[i][j];
for(int i = n;i >= ;i --)for(int j = ;j <= m;j ++)dp4[i][j] = max(dp4[i+][j],dp4[i][j-])+mp[i][j];
for(int i = ;i <= n;i ++)
{
for(int j = ;j <= m;j ++)
ma = max(ma,max(dp1[i][j-]+dp2[i][j+]+dp3[i+][j]+dp4[i-][j],dp1[i-][j]+dp2[i+][j]+dp3[i][j+]+dp4[i][j-]));
}
cout<<ma;
}

随机推荐

  1. 如何将本地的项目上传到git

    如何将本地的项目上传到git 1 进入项目文件夹,把目录变为git仓库 git init 2 把文件添加到版本库中 git add . 3 把版本提交到版本库 git commit -m 'first ...

  2. 搞懂分布式技术2:分布式一致性协议与Paxos,Raft算法

    搞懂分布式技术2:分布式一致性协议与Paxos,Raft算法 2PC 由于BASE理论需要在一致性和可用性方面做出权衡,因此涌现了很多关于一致性的算法和协议.其中比较著名的有二阶提交协议(2 Phas ...

  3. 使用IScroll5实现滚动

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. 十、dbms_shared_pool(提供了对共享池的一些过程和函数访问)

    1.概述 作用:提供了对共享池的一些过程和函数访问,它使用户可以显示共享池中的对象尺寸,绑定对象到共享池,清除绑定到共享池的对象.为了使用该包,必须运行dbmspool.sql脚本来建立该包. 2.包 ...

  5. Winform 导航菜单的方法

    http://blog.163.com/kunkun0921@126/blog/static/169204332201171610619611/ 第一种:使用OutlookBar第三方控件 第二种:使 ...

  6. 高射炮打蚊子丨用Visual Studio 2017写最初级的C语言程序

    众所周知,Visual Studio号称全宇宙最强的IDE(集成开发环境),简直可以“秒天秒地秒空气”.我们看着各种技术大会上,大神们在台上用VS演示Demo溜得飞起,然而对于一些非技术专业同学或者是 ...

  7. eureka-7-多网卡下的ip选择

    目前没有需求,后面需要的话,再补充 只是简单使用的话,只需要指定ip即可 eureka.instance.ip-address:127.0.0.1

  8. 随手写的一个检测php连接mysql的小脚本

    最近偶然接触到一点点的php开发,要用到mysql数据库,由于mysql和php版本的关系,php5里面连接函数有mysql_connect(),mysqli_connect()两种,php7中又使用 ...

  9. GreenPlum的Primary和Mirro切换恢复

    gp节点出现了acting as primary change tracking错误,判断是节点primary和mirror发生了切换 1.没有配置gp的日志,无法获取为什么切换了,待会儿看看默认日志 ...

  10. cpu的用户态和内核态和内存的用户空间内核空间

    谈到CPU的这两个工作状态,也就是处理器的这两个工作状态,那我们有必要说一下为什么搞出这两个鬼玩意出来.       用过电脑的娃娃们肯定知道在一个系统中既有操作系统的程序,也由普通用户的程序.但那么 ...