410812: GYM104120 B Business Stamps

Memory Limit:256 MB Time Limit:1 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

B. Business Stampstime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard output

Jaime transportation company started operations in Quadradonia this year, the business was growing and Jaime was transporting packages between any destination in Quadradonia until new law enforced rules to transit in the city. Quadronia has been divided in sectors and is represented by a rectangular shaped matrix of $$$N$$$ rows and $$$M$$$ columns, forming $$$N$$$x$$$M$$$ sectors. The new law enforces that a vehicle can transit on the sector located at the $$$i$$$-th row and $$$j$$$-th column of Quadradonia, if and only if, the truck has been approved with the $$$M_{i,j}$$$ stamp, and can move only to those sectors that share a side in the sectors map if the vehicle has the required stamp to its destination.

The new law has brought problems to Jaime, but not much of them, Jaime currently can operate all his business moving trucks in one sector each, making sure he needs only one stamp per truck, however, the main warehouse where Jaime operates his business is not in the same sector as his home, therefore, Jaime will need to get stamps for his car so he can transit to work each morning. As each required stamp approval needs to be requested independently, and only one request per vehicle is processed at a time, Jaime wants to get the minimum number of stamps for his car.

Given the map of sectors of Quadradonia, the coordinates of the sector where Jaime's home and the main warehouse are located, help Jaime find the minimum number of stamps he must get for him to be able to travel from his home to the main warehouse.

Input

The first line contains two integer numbers separated by a space $$$N$$$ and $$$M$$$ ($$$1 \leq N,M \leq 100$$$) representing the number of rows and columns in Quadradonia sectors map. The next line contains two integer numbers $$$r_h$$$ ($$$1 \leq r_h \leq N$$$), and $$$c_h$$$ ($$$1 \leq c_h, leq M$$$), representing the row and column of the sector where Jaime's home is located. The next line contains two integer numbers $$$r_w$$$ ($$$1 \leq r_w \leq N$$$), and $$$c_w$$$ ($$$1 \leq c_w \leq M$$$), representing the row and column of the sector where the main warehouse is located. Each of the next $$$N$$$ lines contains $$$M$$$ integer numbers separated by a space, where the $$$j$$$-th number of the $$$i$$$-th line represents the stamp $$$M_{i,j}$$$ required to transit in the sector of the $$$i$$$-th row and $$$j$$$-th column ($$$1 \leq M_{i,j} \leq 10$$$).

Output

Output a line containing an integer number, the minimum number of stamps Jaime must get so he can transit from his home sector to the main warehouse sector.

ExampleInput
5 5
1 1
5 3
1 2 3 2 3
6 2 5 7 3
4 7 3 4 5
3 2 1 3 5
5 2 3 7 9
Output
4

加入题单

上一题 下一题 算法标签: