In a video game Little Hi is trapped in a maze. The maze can be considered as an
N
×
M
grid. There are monsters on some cells. Each cell has one monster at most. Below is an example of a 4x5 maze. '.' represents an empty cell. 'D' represents the entrance, Little Hi's starting point. 'M' represents a normal monster. 'S' represents a special monster.
..S..
M...M
..D..
.M...
At the beginning, each cell is covered by a slate except that the slate on the entrance cell has been already removed. Each round Little Hi may either remove a slate as long as
1. each monster has either been killed or still covered by a slate, and
2. the cell covered by the slate is adjacent to some cell whose slate has been already removed. (Two cells are adjacent if they share a common side.)
or attack a monster as long as the slate covering it has been removed.
At the beginning Little Hi has Hp hit points and Ap attack points. Each monster also has its hit points Hi and attack points Ai. When Little Hi attacks a monster, the hit points of both sides should subtract the attack points of the other side.
For example, if Little Hi's hit points are 50 and attack points are 30. When he attacks a monster whose hit points are 25 and attack points are 10, the remaining hit points for Little Hi are 40 and the remaining hit points for the monster are -5.
When hit points are less than or equal to 0 the monster is killed.
At the beginning Little Hi has a buff called "Destruction Blade" which lasts for 5 rounds. With such buff Little Hi does not take damage when he attacks a monster. The buff vanishes after 5 rounds but can be refreshed or regained for the following 5 rounds after killing a special monster. (Note that the buff always lasts for 5 rounds after killing a special monster no matter how many rounds left before killing the monster.)
Now given the map of the maze. Can you tell whether Little Hi can kill all the monsters? If he can what is the maximum remaining hit points?
输入
Line 1: two integers N and M. (2 ≤ N, M ≤ 6, N × M ≤ 20)
Line 2 .. N+1: M characters per line, representing the maze map.
Line N+2 .. N+K+1: two integers Hi and Ai per line, representing the hit points and attack points for each monster, from top to bottom and left to right. (3 ≤ K ≤ 7)
Line N+K+2: two integers Hp and Ap, the hit points and attack points for Little Hi.
输出
If Little Hi can kill all the monsters and stay alive output the maximum remaining hit points. Otherwise output DEAD.
样例解释
Let's assume the upper left cell is (1, 1).
Round 1: remove slate (2, 3), buff remains 4 rounds
Round 2: remove slate (2, 2), buff remains 3 rounds
Round 3: remove slate (2, 1), buff remains 2 rounds
Round 4: attack monster (2, 1), take no damage, buff remains 1 round
Round 5: attack monster (2, 1), take no damage, monster killed, buff vanishes
Round 6: remove slate (2, 4)
Round 7: remove slate (4, 3)
Round 8: remove slate (1, 3)
Round 9: attack monster (1, 3), take 5 damage, HP=55
Round 10: attack monster (1, 3), take 5 damage, HP=50, monster killed, buff remains 5 rounds
Round 11: remove slate (2, 5), buff remains 4 rounds
Round 12: attack monster (2, 5) take no damage, buff remains 3 rounds
Round 13: attack monster (2, 5) take no damage, buff remains 2 rounds
Round 14: remove slate (4, 2), buff remains 1 round
Round 15: attack monster (4, 2), take no damage, buff vanishes
Round 16: attack monster (4, 2), take 5 damage, HP=45, monster killed
样例输入:
4 5 ..S.. M...M ..D.. .M... 20 5 20 5 20 5 20 5 60 10样例输出
45