Maximum Energy
题号:NC252112
时间限制:C/C++/Rust/Pascal 1秒,其他语言2秒
空间限制:C/C++/Rust/Pascal 256 M,其他语言512 M
64bit IO Format: %lld

题目描述

Christina finds herself on a grid with n rows and m columns. Each cell on the grid has a height with the height of cell (i,j) being h_{i,j}. Christina does not know which cell she started in, but she does know one thing: she is very small, especially when it comes to climbing mountains. Therefore, she can only move to a cell that is no higher than the one she is currently on and that is adjacent to her current cell. When Christina moves between two cells of the same height, she expends no energy. Otherwise, each move she makes expends 1 unit of energy.

Now Christina wants to know, for all possible starting positions, what is the maximum energy she would expend? Two cells are considered adjacent if they share an edge.

Note: Christina must never be allowed to move off the grid at any time.

输入描述:

The input contains multiple test cases. The first line consists of an integer T(1 \leq T \leq 10), the number of test cases. Then there will be T test cases.

For each test case:

- The first line consists of two integers, n and m(1 \leq n,m \leq 500), separated by a space, indicating the dimensions of the grid.
- n lines follow, each of which contains m integers, with the j-th integer on line i denoting the height h_{i,j}(1 \leq h_{i,j} \leq 10^6) of the cell at row i and column j.

输出描述:

For each test case, output a single integer, representing the maximum energy Christina would expend.
示例1

输入

复制
1
3 3
3 4 3
2 3 2
2 1 9

输出

复制
3