The geologist further hypothesizes that cracks will occur from the northernmost to
the southernmost of the seabed area. In addition, exactly one sub-region in each row will
contain a crack and cracks in consecutive rows must be adjacent to each other, vertically or
diagonally. This means that two adjacent cracks are either in the same column or in the
adjacent column. The last part of the geologist’s hypothesis is that the most likely cracks
will occur in a way that the total amount of energy required to cause the cracks is minimal
among all possible cracks. In the above example, the most likely cracks occur in the
highlighted sub-regions and the total energy to cause the cracks is 2+5+1+5+1+2+2+2 units.
Your task is to write an efficient program to calculate the total energy amount
required to cause the most likely cracks.
Input
The first line contains an integer T denoting the number of test cases (1 ≤ T ≤ 10).
For each test case, the input is in the following format.
The first line contains integers M and N denoting the number of rows and columns of
sub-regions in a seabed area, where 1 ≤ M ≤ 500 and 1 ≤ N ≤ 500.
north
west