#K206. Maximum Height Difference in a Grid

    ID: 24652 Type: Default 1000ms 256MiB

Maximum Height Difference in a Grid

Maximum Height Difference in a Grid

You are given a grid of size H×WH \times W where each cell contains an integer representing its height. Your task is to find the maximum absolute difference between the heights of any two adjacent cells. Two cells are considered adjacent if they share a side (i.e., the cells directly above, below, left, or right of a given cell).

Formally, let grid[i][j]grid[i][j] denote the height of the cell in the ii-th row and jj-th column. You need to compute [ \max_{(i,j) \sim (k,l)} \ |grid[i][j] - grid[k][l]| ] where the maximum is taken over all pairs of adjacent cells (i,j)(i,j) and (k,l)(k,l).

It is guaranteed that the grid will have at least one cell.

inputFormat

The first line of input contains two integers HH and WW, the number of rows and columns of the grid respectively. This is followed by HH lines, each containing WW space-separated integers representing the grid.

outputFormat

Output a single integer — the maximum absolute difference between the heights of any two adjacent cells.## sample

3 3
1 2 2
3 8 2
5 3 5
6

</p>