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. Linux安装keepalived

    1.下载安装ipvs安装包,进行解压 http://www.keepalived.org/software/ 2.创建安装路径连接 安装环境: yum -y install openssl-devel ...

  2. TIME_WAIT和CLOSE_WAIT

    先看下三次握手四次挥手的状态变化: 通常会遇到下面两种情况: 服务器保持了大量TIME_WAIT状态 服务器保持了大量CLOSE_WAIT状态 因为linux分配给一个用户的文件句柄是有限的,而TIM ...

  3. JS书籍推荐

    JS书籍推荐 一.总结 一句话总结: 二.JS进阶书籍 第一阶段:<JavaScript DOM编程艺术> 看这本书之前,请先确认您对Javascript有个基本的了解,应该知道if el ...

  4. C语言实现顺序表

    C语言实现顺序表代码 文件SeqList.cpp #pragma warning(disable: 4715) #include"SeqList.h" void ShowSeqLi ...

  5. 12.详解Condition的await和signal等待通知机制

    1.Condition简介 任何一个java对象都天然继承于Object类,在线程间实现通信的往往会应用到Object的几个方法,比如wait(),wait(long timeout),wait(lo ...

  6. HDU 5289 尺取

    Assignment Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...

  7. LA-4255 Guess (拓扑排序+构造)

    题目大意:一个未知的整数序列,给出其任意一个区间和的正负,还原这个序列.任意一个满足条件的序列即可. 题目分析:将连续区间和转化为前缀和之差,sumx-1与sumy的大小关系已知,以此建立一条有向边, ...

  8. [转载]Java操作Excel文件的两种方案

    微软在桌面系统上的成功,令我们不得不大量使用它的办公产品,如:Word,Excel.时至今日,它的源代码仍然不公开已封锁了我们的进一步应用和开发.在我们实际开发企业办公系统的过程中,常常有客户这样子要 ...

  9. Qt 元对象系统(Meta-Object System)

    (转自:http://blog.csdn.net/aladdina/article/details/5496891) Qt的元对象系统基于如下三件事情: 类:QObject,为所有需要利用原对象系统的 ...

  10. ( 转)Sqlserver中tinyint, smallint, int, bigint的区别 及 10进制转换16进制的方法

    一.类型比较 bigint:从-2^63(-9223372036854775808)到2^63-1(9223372036854775807)的整型数据,存储大小为 8 个字节.一个字节就是8位,那么b ...