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. ZooKeeper的API操作(二)(通俗易懂)

    所需要6个jar包,都是解压zookeeper的tar包后里面的. zookeeper-3.4.10.jar    jline-0.094.jar    log4j-1.2.16.jar netty- ...

  2. Java BigInteger 与C# BigInteger之间的问题

    最近接到一个Java代码转C#代码的项目.本来就两个函数看起来很简单的,后来折腾了一天,终于完美收官. 碰到的第一个问题是:java的BigInteger构造函数里面BigInteger(string ...

  3. CentOS 7添加应用快捷方式到桌面

    以eclipse为例,编辑下面文件,复制到桌面即可. vi client.desktop [Desktop Entry]Encoding=UTF-8Name=eclipseExec=/home/clo ...

  4. linux入门总结

    linux的核心概念知识:     linux软件是开源免费的,而linux是由Unix演变而成,Unix是由MINIX演变而成. 2000年以后,linux系统日趋成熟,涌现大量基于linux服务平 ...

  5. ionic+cordova 学习开发App(一)

    一.项目所需环境 (一)jdk 1.jdk的安装,必须同时包含Java 和javac [一般安装包中都包含有,可以确定下] (二)node.js 和NPM 1.大多插件和辅助工具都运行在NPm平台上. ...

  6. 设置了width和height的a元素在IE11与IE11以下浏览器中的不同渲染方式

    #welcomeMiddleBtn { display: block; width: 73px; height: 120px; margin: 0px auto; } <a id="w ...

  7. 拒绝了对对象 'sp_OACreate' (数据库 'mssqlsystemresource',架构 'sys')的 EXECUTE 权限。

    执行一个存储过程, 由于里面使用到了一些 --创建对象  EXEC sp_OACreate 'VBScript.RegExp', @objRegex OUT  --设置属性  EXEC sp_OASe ...

  8. Prism 4 文档 ---第2章:初始化Prism应用程序

     这一章节介绍Prism应用程序启动和运行时发生的内容.Prism应用程序在启动时需要有注册和配置的过程,这就是所谓的自自启动程序. 什么是自启动引导程序?     引导程序是一个类,它负责使用Pri ...

  9. MVC和EF中的 Model First 和 Code First

    准备:先引入MVC和EF的dll包 *命令方法:打开工具——库程序包管理器——程序包管理器控制台,选择自己的项目 a)     Install-Package EntityFramework -Ver ...

  10. 【LeetCode 111_二叉树_遍历】Minimum Depth of Binary Tree

    解法一:递归 int minDepth(TreeNode* root) { if (root == NULL) ; if (root->left == NULL) { ; } else if ( ...