eolymp
bolt
Try our new interface for solving problems
Problems

Route in the matrix

Route in the matrix

Given matrix of size n × m that contains positive integers, not greater than 10000.

The cells are adjacent if their column or row numbers differ by 1. The path from one cell to another passes only through adjacent cells of the matrix.

Find the path with minimum cost between the upper left and lower right corners of the matrix. The cost of the path equals to the sum of matrix elements through the way. It is allowed to move to neighboring cells to the left, right, up and down.

Input

First line contains the numbers n and m (1n, m10). Then n lines are given, each line contains m numbers.

Output

Print the cost of minimal path.

Time limit 1 second
Memory limit 122.17 MiB
Input example #1
4 5
1 9 4 5 9
1 8 1 1 1
1 1 1 9 1
6 5 9 8 1
Output example #1
10