14888 - Let The Water Flow   

Description

You are given an N * M grid representing a terrain, where each cell contains an integer between 1 and 15 representing its height.

A water source is placed at the top-left corner (0, 0). Water can flow from a cell to any of its four adjacent neighbors (up, down, left, and right), but only if the neighbor's height is less than or equal to the current cell's height.

Your task is to calculate the total number of cells that will be covered by water (including water source).

 

Constraints

1 <= N, M <= 100

 

Input

  • The first line contains two integers N and M, representing the number of rows and columns.

  • The following N lines each contain M integers, representing the heights of the cells in the grid.

Output

Output a single integer with '\n': the total number of cells reachable by the water from the top-left corner.

Sample Input  Download

Sample Output  Download




Discuss