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. js原码工具集

    /* 原生js工具集 */ arr是否包含obj function contains1(arr, obj) { var i = arr.length; while (i--) { if (arr[i] ...

  2. angularjs控制器之间的数据共享与通信

    1.可以写一个service服务,从而达到数据和代码的共享; var app=angular.module('app',[]); app.service('ObjectService', [Objec ...

  3. ADC和RTC的寄存器的读取

    ADC的寄存器读取,int adc_read(void){ int result; #if ADSTART==0 result = ADC.ADCDAT0&0x3ff; while(!(ADC ...

  4. Calendar时间获取天,周,月,季度,年度时间段

    Date类 Date类的大多数构造函数和方法都已经抛弃,只留下和毫秒有关的操作,目前常用的构造函数 常用方法 设置毫秒数 读取毫秒数 toString()打印结果 //创建日期对象,把当前的毫秒值转成 ...

  5. Java subList的使用

    1. 在看<阿里巴巴java编程手册的时候>有如下强制约束 顺便学了一下subList. java.util.List中有一个subList方法,用来返回一个list的一部分的视图. Li ...

  6. UVA-11324 The Largest Clique (强连通+DP)

    题目大意:在一张无向图中,最大的节点集使得集合内任意两个节点都能到达对方. 题目分析:找出所有的强连通分量,将每一个分量视作大节点,则原图变成了一张DAG.将每个分量中的节点个数作为节点权值,题目便转 ...

  7. JavaScript---循环与闭包

    循环与闭包 先看一个demo <!DOCTYPE html> <html lang="en"> <head> <meta charset= ...

  8. 转载:【Oracle 集群】RAC知识图文详细教程(一)--集群概念介绍

    文章导航 集群概念介绍(一) ORACLE集群概念和原理(二) RAC 工作原理和相关组件(三) 缓存融合技术(四) RAC 特殊问题和实战经验(五) ORACLE 11 G版本2 RAC在LINUX ...

  9. 如何让VS2013编写的程序在xp下运行

    总体分c++程序和c#程序 1.c++程序 这个用C++编写的程序可以经过设置后在XP下运行,主要的“平台工具集”里修改就可以. 额外说明:(1)程序必须为Dotnet 4.0及以下版本.(XP只支持 ...

  10. vue.js 源代码学习笔记 ----- core lifecycle

    /* @flow */ import config from '../config' import Watcher from '../observer/watcher' import { mark, ...