//
// main.cpp
// poj1088
//
// Created by Jasmine wang on 05/09/2017.
// Copyright © 2017 Jasmine wang. All rights reserved.
//
这道题是典型的递归 事实上我用的递推。郭老师给出了两种方法,但首先都是确定长度函数l[][]
1、人人为我:即从某一点开始,若小于临近数值,则临近点的的l加一 若非,则临近点l是1 上下左右都做判断
2、我为人人:从一点开始,若大于临近数值,则该点是临近点+1,然后移到该临近点;若非,则该点为1
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
#define MAXN 105
int R, C;
int h[MAXN][MAXN], l[MAXN][MAXN];
int longestpath(int x, int y){
//int left = 0,right = 0, up = 0, down = 0, m;
if(l[x][y] > 0){
return l[x][y];
}
if(x + 1 <= R){
if(h[x][y] < h[x + 1][y]){
h[x + 1][y] = max(h[x + 1][y], h[x][y] + 1);
}
else h[x + 1][y] = 1;
}
else h[x + 1][y] = 1;
if(x - 1 >= 1){
if(h[x][y] < h[x - 1][y]){
h[x - 1][y] = max(h[x - 1][y], h[x][y] + 1);
}
else h[x - 1][y] = 1;
}
else h[x - 1][y] = 1;
if(y + 1 <= C){
if(h[x][y] < h[x][y + 1]){
h[x][y + 1] = max(h[x][y + 1], h[x][y] + 1);
}
else h[x][y + 1] = 1;
}
else h[x][y + 1] = 1;
if(y - 1 >= 1){
if(h[x][y] < h[x][y - 1]){
h[x][y - 1] = max(h[x][y - 1], h[x][y] + 1);
}
else h[x][y - 1] = 1;
}
else h[x][y - 1] = 1;
//m = max(left, max(right, max(up, down)));
/*
if(x+1<=R){
if(h[x+1][y]<h[x][y])
up=1+longestpath(x+1,y);
else up=1;
}
else up=1;
if(x-1>=1){
if(h[x-1][y]<h[x][y])
down=1+longestpath(x-1,y);
else down=1;
}
else down=1;
if(y+1<=C){
if(h[x][y+1]<h[x][y])
right=1+longestpath(x,y+1);
else right=1;
}
else right=1;
if(y-1>=1){
if(h[x][y-1]<h[x][y])left=1+longestpath(x,y-1);
else left=1;
}
else left=1;
m = max(left, max(right, max(up, down)));*/
return h[x][y];
}
int main(int argc, const char * argv[]) {
int i, j, ans = 0;
scanf("%d%d", &R, &C);
for( i = 1; i <= R; i ++){
for(j = 1; j <= C; j ++){
scanf("%d", &h[i][j]);
}
}
memset(l,0,sizeof(l));
for(i = 1; i <= R; i ++){
for( j = 1; j <= C; j ++){
l[i][j] = longestpath(i, j);
if(ans < l[i][j]){
ans = l[i][j];
}
}
}
printf("%d", ans);
return 0;
}