A - Same Sum Grid Path

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 300

問題文

N \times N のマス目があります。上から i 行目、左から j 列目のマスを (i,j) で表します。

N \times N 個の文字 S_{1,1}, \dots , S_{N,N} が与えられます。各文字は数字(09)または ? です。? をそれぞれ数字に置き換えることで以下の条件を満たせるか判定し、可能ならばそのような置き換え方を 1 つ出力して下さい。

1 \le i,j \le N についてマス (i,j)S_{i,j} を整数として書き込む。
右または下に隣接するマスへの移動のみを繰り返してマス (1,1) からマス (N,N) まで到達する経路について、経路上に含まれるマス(始点・終点を含む)に書かれた整数の総和を経路のスコアとする。
このとき、\binom{2N-2}{N-1} 通りある経路について、スコアは全て等しい。

制約

  • 2 \le N \le 100
  • S_{i,j} は数字(09)または ?

入力

入力は以下の形式で標準入力から与えられる。

N
S_{1,1}S_{1,2}\ldotsS_{1,N}
S_{2,1}S_{2,2}\ldotsS_{2,N}
\vdots
S_{N,1}S_{N,2}\ldotsS_{N,N}

出力

条件を満たす置き換えが不可能な場合、-1 を出力せよ。可能な場合、置き換え後の S_{1,1}, \dots , S_{N,N} を以下の形式で出力せよ。

S_{1,1}S_{1,2}\ldotsS_{1,N}
S_{2,1}S_{2,2}\ldotsS_{2,N}
\vdots
S_{N,1}S_{N,2}\ldotsS_{N,N}

解が複数存在する場合は、どれを出力しても正解とみなされる。


入力例 1

3
101
0??
1??

出力例 1

101
010
101

全ての経路のスコアが 3 になるように ? を数字に置き換えることが出来ます。


入力例 2

4
2026
?2?8
????
?214

出力例 2

-1

どのような置き換え方をしても条件を満たすことが出来ません。


入力例 3

2
99
99

出力例 3

99
99

Score : 300 points

Problem Statement

There is an N \times N grid. Let (i,j) denote the cell at the i-th row from the top and j-th column from the left.

You are given N \times N characters S_{1,1}, \dots , S_{N,N}. Each character is a digit (0 to 9) or ?. Determine if it is possible to replace each ? with a digit to satisfy the following condition, and if possible, output one such way of replacement.

For each 1 \le i,j \le N, write S_{i,j} as an integer in cell (i,j).
For a path from cell (1,1) to cell (N,N) by repeating moves only to adjacent cells to the right or down, let the score of the path be the sum of the integers written in the cells on the path (including the start and end points).
Then, for all \binom{2N-2}{N-1} possible paths, the scores are all equal.

Constraints

  • 2 \le N \le 100
  • S_{i,j} is a digit (0 to 9) or ?.

Input

The input is given from Standard Input in the following format:

N
S_{1,1}S_{1,2}\ldotsS_{1,N}
S_{2,1}S_{2,2}\ldotsS_{2,N}
\vdots
S_{N,1}S_{N,2}\ldotsS_{N,N}

Output

If it is impossible to make a replacement that satisfies the condition, output -1. If possible, output S_{1,1}, \dots , S_{N,N} after the replacement in the following format:

S_{1,1}S_{1,2}\ldotsS_{1,N}
S_{2,1}S_{2,2}\ldotsS_{2,N}
\vdots
S_{N,1}S_{N,2}\ldotsS_{N,N}

If there are multiple solutions, any of them will be accepted.


Sample Input 1

3
101
0??
1??

Sample Output 1

101
010
101

It is possible to replace ? with digits so that the scores of all paths are 3.


Sample Input 2

4
2026
?2?8
????
?214

Sample Output 2

-1

No replacement can satisfy the condition.


Sample Input 3

2
99
99

Sample Output 3

99
99
B - Missing Number in Graph

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 500

問題文

N 頂点の M 辺の連結な単純無向グラフがあり、頂点には 1, \dots ,N の番号、辺には 1, \dots ,M の番号がついています。 辺 i は頂点 A_i と頂点 B_i を結んでいます。 また、0 から N までの整数が書かれたカードがそれぞれ 1 枚ずつ、合計 N+1 枚あります。

すぬけ君は以下の操作を順番に行いました。

  1. 各頂点に 1 枚ずつカードを置き、残った 1 枚のカードを食べる。
  2. i\ (1 \le i \le M) について、頂点 A_i,B_i に置かれた 2 枚のカードに書かれた整数のビット単位 \mathrm{XOR} を辺 i に書く。この整数を X_i とする。
  3. 頂点に置かれたカードを全て捨てる。

グラフの情報(N,M,A_1,\dots ,A_M,B_1,\dots ,B_M,X_1,\dots ,X_M)が与えられるので、すぬけ君が食べたカードに書かれた整数を特定して下さい。ただし、一意に定まらない場合は -1 を出力して下さい。 なお、与えられる X が上記の操作で得られるものであることは保証されます。

T 個のテストケースが与えられるので、それぞれについて答えを求めて下さい。

ビット単位 \mathrm{XOR} 演算とは

非負整数 A, B のビット単位 \mathrm{XOR}A \oplus B は、以下のように定義されます。

  • A \oplus B を二進表記した際の 2^k (k \geq 0) の位の数は、A, B を二進表記した際の 2^k の位の数のうち一方のみが 1 であれば 1、そうでなければ 0 である。
例えば、3 \oplus 5 = 6 となります (二進表記すると: 011 \oplus 101 = 110)。
一般に k 個の非負整数 p_1, p_2, p_3, \dots, p_k のビット単位 \mathrm{XOR}(\dots ((p_1 \oplus p_2) \oplus p_3) \oplus \dots \oplus p_k) と定義され、これは p_1, p_2, p_3, \dots, p_k の順番によらないことが証明できます。

制約

  • 1 \le T \le 10^4
  • 1 \le N \le 2 \times 10^5
  • 0 \le M \le 2 \times 10^5
  • 1 \le A_i,B_i \le N
  • 与えられるグラフは連結な単純無向グラフである
  • X_1,\dots ,X_M は問題文中の操作で得られるものである
  • 全てのテストケースにおける N の総和は 2 \times 10^5 以下
  • 全てのテストケースにおける M の総和は 2 \times 10^5 以下
  • 入力される値は全て整数

入力

入力は以下の形式で標準入力から与えられる。

T
\text{case}_1
\text{case}_2
\vdots
\text{case}_T

各テストケースは以下の形式で与えられる。

N M
A_1 B_1 X_1
A_2 B_2 X_2
\vdots
A_M B_M X_M

出力

T 行出力せよ。

i 行目には i 番目のテストケースについて、すぬけ君が食べたカードに書かれた整数が一意に定まる場合はその整数を、そうでない場合は -1 を出力せよ。


入力例 1

3
2 1
1 2 3
1 0
4 4
2 1 4
1 3 5
1 4 7
2 3 1

出力例 1

0
-1
2

1 つ目のテストケースについて、グラフは下図の通りです。

頂点 1,2 に置かれたカードに書かれた整数の組は (1,2)(2,1) がありえますが、いずれの場合もすぬけ君が食べたカードに書かれた整数は 0 です。

2 つ目のテストケースについて、すぬけ君が食べたカードに書かれた整数は 0,12 通りがありえます。

3 つ目のテストケースについて、グラフは下図の通りです。

Score : 500 points

Problem Statement

There is a connected simple undirected graph with N vertices and M edges, where the vertices are numbered 1, \dots ,N and the edges are numbered 1, \dots ,M. Edge i connects vertices A_i and B_i. Also, there are a total of N+1 cards, one each with the integers from 0 to N written on them.

Snuke performed the following operations in order:

  1. Place one card on each vertex, and eat the remaining one card.
  2. For each i\ (1 \le i \le M), write on edge i the bitwise \mathrm{XOR} of the integers written on the two cards placed on vertices A_i and B_i. Let this integer be X_i.
  3. Discard all cards placed on the vertices.

Given the information about the graph (N,M,A_1,\dots ,A_M,B_1,\dots ,B_M,X_1,\dots ,X_M), determine the integer written on the card that Snuke ate. If it cannot be uniquely determined, output -1. It is guaranteed that the given X can be obtained by the above operations.

You are given T test cases; solve each of them.

What is bitwise \mathrm{XOR}?

The bitwise \mathrm{XOR} of non-negative integers A and B, A \oplus B, is defined as follows:

  • In the binary representation of A \oplus B, the digit in the 2^k (k \geq 0) place is 1 if exactly one of the digits in the 2^k place in the binary representations of A and B is 1, and 0 otherwise.
For example, 3 \oplus 5 = 6 (in binary: 011 \oplus 101 = 110).
In general, the bitwise \mathrm{XOR} of k non-negative integers p_1, p_2, p_3, \dots, p_k is defined as (\dots ((p_1 \oplus p_2) \oplus p_3) \oplus \dots \oplus p_k), and it can be proved that this does not depend on the order of p_1, p_2, p_3, \dots, p_k.

Constraints

  • 1 \le T \le 10^4
  • 1 \le N \le 2 \times 10^5
  • 0 \le M \le 2 \times 10^5
  • 1 \le A_i,B_i \le N
  • The given graph is a connected simple undirected graph.
  • X_1,\dots ,X_M can be obtained by the operations in the problem statement.
  • The sum of N over all test cases is at most 2 \times 10^5.
  • The sum of M over all test cases is at most 2 \times 10^5.
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

T
\text{case}_1
\text{case}_2
\vdots
\text{case}_T

Each test case is given in the following format:

N M
A_1 B_1 X_1
A_2 B_2 X_2
\vdots
A_M B_M X_M

Output

Output T lines.

The i-th line should contain the integer written on the card Snuke ate for the i-th test case if it can be uniquely determined, and -1 otherwise.


Sample Input 1

3
2 1
1 2 3
1 0
4 4
2 1 4
1 3 5
1 4 7
2 3 1

Sample Output 1

0
-1
2

For the first test case, the graph is as shown below.

The possible pairs of integers written on the cards placed on vertices 1 and 2 are (1,2) and (2,1); in either case, the integer written on the card Snuke ate is 0.

For the second test case, there are two possible integers written on the card Snuke ate: 0 and 1.

For the third test case, the graph is as shown below.

C - Divide into 4 Teams

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 600

問題文

1, \dots ,N の番号がついた N 人の人がいます。人 i強さP_i です。

各人を A,B,C,D のいずれかのチームに割り当てることで、4 つのチームにチーム分けをします。チーム分けの方法は 4^N 通りありますが、このうち以下の条件を全て満たすチーム分けの個数を 998244353 で割った余りを求めて下さい。

  • A,B,C,D のどのチームにも 1 人以上所属する。
  • チーム A に所属する人の強さの和とチーム B に所属する人の強さの和が等しい。
  • チーム C に所属する人の強さの和とチーム D に所属する人の強さの和が等しい。

制約

  • 4 \le N \le 500
  • 1 \le P_i
  • \sum_{1 \le i \le N} P_i \le 10^5
  • 入力される値は全て整数

入力

入力は以下の形式で標準入力から与えられる。

N
P_1 P_2 \ldots P_N

出力

問題文中の条件を満たすチーム分けの個数を 998244353 で割った余りを出力せよ。


入力例 1

4
1 1 2 2

出力例 1

8

以下の 8 通りのチーム分けが条件を満たします。

1 2 3 4
チーム分け 1 A B C D
チーム分け 2 B A C D
チーム分け 3 A B D C
チーム分け 4 B A D C
チーム分け 5 C D A B
チーム分け 6 C D B A
チーム分け 7 D C A B
チーム分け 8 D C B A

入力例 2

9
1 2 3 4 5 6 7 8 9

出力例 2

0

条件を満たすチーム分けが存在しません。


入力例 3

24
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24

出力例 3

289359125

Score : 600 points

Problem Statement

There are N people numbered 1, \dots ,N. The strength of person i is P_i.

Each person is assigned to one of the teams A,B,C,D to form four teams. There are 4^N ways to form teams; among these, find the number, modulo 998244353, of formations that satisfy all of the following conditions.

  • Each of the teams A,B,C,D has at least one person.
  • The sum of the strengths of the people in team A equals the sum of the strengths of the people in team B.
  • The sum of the strengths of the people in team C equals the sum of the strengths of the people in team D.

Constraints

  • 4 \le N \le 500
  • 1 \le P_i
  • \sum_{1 \le i \le N} P_i \le 10^5
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

N
P_1 P_2 \ldots P_N

Output

Output the number, modulo 998244353, of formations that satisfy the conditions in the problem statement.


Sample Input 1

4
1 1 2 2

Sample Output 1

8

The following eight team formations satisfy the conditions.

Person 1 Person 2 Person 3 Person 4
Formation 1 A B C D
Formation 2 B A C D
Formation 3 A B D C
Formation 4 B A D C
Formation 5 C D A B
Formation 6 C D B A
Formation 7 D C A B
Formation 8 D C B A

Sample Input 2

9
1 2 3 4 5 6 7 8 9

Sample Output 2

0

No formations satisfy the conditions.


Sample Input 3

24
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24

Sample Output 3

289359125
D - Distinct Sum Grid Path

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 700

問題文

N \times N のマス目があります。上から i 行目、左から j 列目のマスを (i,j) で表します。

以下の条件を満たすように各マスに非負整数を 1 つずつ書き込む方法を 1 つ出力して下さい。

右または下に隣接するマスへの移動のみを繰り返してマス (1,1) からマス (N,N) まで到達する経路について、経路上に含まれるマス(始点・終点を含む)に書かれた整数の総和を経路のスコアとする。
このとき、\binom{2N-2}{N-1} 通りある経路について、スコアは互いに異なり、全て 6 \times 10^6 以下である。

制約

  • 2 \le N \le 13

入力

入力は以下の形式で標準入力から与えられる。

N

出力

問題文中の条件を満たすような書き込み方について、マス (i,j) に書き込む非負整数を A_{i,j} とし、これを以下の形式で出力せよ。

A_{1,1} A_{1,2} \ldots A_{1,N}
A_{2,1} A_{2,2} \ldots A_{2,N}
\vdots
A_{N,1} A_{N,2} \ldots A_{N,N}

解が複数存在する場合は、どれを出力しても正解とみなされる。


入力例 1

2

出力例 1

1 2
3 4

右・下の順で移動する経路のスコアは 7、下・右の順で移動する経路のスコアは 8 であり、問題文中の条件を満たします。


入力例 2

5

出力例 2

10443 257776 131299 258759 153153
103533 63158 120370 132605 22808
206213 155047 237224 121264 96487
213665 51789 237311 239387 27085
229301 125534 154776 40898 210765

Score : 700 points

Problem Statement

There is an N \times N grid. Let (i,j) denote the cell at the i-th row from the top and j-th column from the left.

Output one way to write a non-negative integer in each cell so that the following condition is satisfied.

For a path from cell (1,1) to cell (N,N) by repeating moves only to adjacent cells to the right or down, let the score of the path be the sum of the integers written in the cells on the path (including the start and end points).
Then, for all \binom{2N-2}{N-1} possible paths, the scores are all distinct and all at most 6 \times 10^6.

Constraints

  • 2 \le N \le 13

Input

The input is given from Standard Input in the following format:

N

Output

For a way of writing that satisfies the condition in the problem statement, let A_{i,j} be the non-negative integer written in cell (i,j), and output it in the following format:

A_{1,1} A_{1,2} \ldots A_{1,N}
A_{2,1} A_{2,2} \ldots A_{2,N}
\vdots
A_{N,1} A_{N,2} \ldots A_{N,N}

If there are multiple solutions, any of them will be accepted.


Sample Input 1

2

Sample Output 1

1 2
3 4

The score of the path that moves right then down is 7, and the score of the path that moves down then right is 8, satisfying the condition in the problem statement.


Sample Input 2

5

Sample Output 2

10443 257776 131299 258759 153153
103533 63158 120370 132605 22808
206213 155047 237224 121264 96487
213665 51789 237311 239387 27085
229301 125534 154776 40898 210765
E - Swap K times

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 700

問題文

長さ N の整数列 A = (A_1, \dots ,A_N), B = (B_1, \dots ,B_N) と正整数 K が与えられます。

コストを 1 払うたびに、隣接 swap をちょうど K行うことが出来ます。隣接 swap とは、以下の操作を指します。

  • 1 以上 N-1 以下の整数 i1 つ選び、A_iA_{i+1} を入れ替える。

AB に一致させられるかを判定して下さい。可能な場合は、一致させるために必要なコストの最小値を求めて下さい。

T 個のテストケースが与えられるので、それぞれについて答えを求めて下さい。

制約

  • 1 \le T \le 10^5
  • 2 \le N \le 3 \times 10^5
  • 1 \le K \le 10^9
  • 1 \le A_i,B_i \le N
  • \{A_1,\dots ,A_N\}\{B_1,\dots ,B_N\} は多重集合として一致する
  • 全てのテストケースにおける N の総和は 3 \times 10^5 以下
  • 入力される値は全て整数

入力

入力は以下の形式で標準入力から与えられる。

T
\text{case}_1
\text{case}_2
\vdots
\text{case}_T

各テストケースは以下の形式で与えられる。

N K
A_1 A_2 \ldots A_N
B_1 B_2 \ldots B_N

出力

T 行出力せよ。

i 行目には i 番目のテストケースについて、AB に一致させられる場合は最小コストを、そうでない場合は -1 を出力せよ。


入力例 1

3
5 4
3 1 4 1 5
5 1 3 1 4
2 1000000000
1 2
2 1
10 13
1 2 3 4 5 6 7 8 9 10
10 9 8 7 6 5 4 3 2 1

出力例 1

2
-1
5

1 つ目のテストケースについて、以下のような操作を行うことで、2 のコストで AB を一致させることが出来ます。

  1. コストを 1 払い、各隣接 swap における i3,1,4,1 の順で選ぶことで A = (3,1,1,5,4) となる。
  2. コストを 1 払い、各隣接 swap における i3,1,2,1 の順で選ぶことで A = (5,1,3,1,4) となる。

2 つ目のテストケースについて、コストによらず、AB を一致させることは出来ません。

Score : 700 points

Problem Statement

You are given integer sequences of length N, A = (A_1, \dots ,A_N) and B = (B_1, \dots ,B_N), and a positive integer K.

Each time you pay a cost of 1, you can perform adjacent swaps exactly K times. An adjacent swap refers to the following operation:

  • Choose an integer i between 1 and N-1, inclusive, and swap A_i and A_{i+1}.

Determine if it is possible to make A match B. If possible, find the minimum cost required to make them match.

You are given T test cases; find the answer for each of them.

Constraints

  • 1 \le T \le 10^5
  • 2 \le N \le 3 \times 10^5
  • 1 \le K \le 10^9
  • 1 \le A_i,B_i \le N
  • \{A_1,\dots ,A_N\} and \{B_1,\dots ,B_N\} match as multisets.
  • The sum of N over all test cases is at most 3 \times 10^5.
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

T
\text{case}_1
\text{case}_2
\vdots
\text{case}_T

Each test case is given in the following format:

N K
A_1 A_2 \ldots A_N
B_1 B_2 \ldots B_N

Output

Output T lines.

The i-th line should contain the minimum cost if it is possible to make A match B for the i-th test case, and -1 otherwise.


Sample Input 1

3
5 4
3 1 4 1 5
5 1 3 1 4
2 1000000000
1 2
2 1
10 13
1 2 3 4 5 6 7 8 9 10
10 9 8 7 6 5 4 3 2 1

Sample Output 1

2
-1
5

For the first test case, it is possible to make A and B match with a cost of 2 by performing the following operations:

  1. Pay a cost of 1, and choose i as 3,1,4,1 in order for each adjacent swap, making A = (3,1,1,5,4).
  2. Pay a cost of 1, and choose i as 3,1,2,1 in order for each adjacent swap, making A = (5,1,3,1,4).

For the second test case, it is impossible to make A and B match regardless of the cost.

F - Unpredictable Moves

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 900

問題文

H \times W のマス目があります。上から i 行目、左から j 列目のマスを (i,j) で表します。

各マスは空きマス・壁マスのいずれかであり、その情報は H \times W 個の文字 S_{1,1},\dots ,S_{H,W} によって表されます。 マス (i,j) は、S_{i,j}. のとき空きマス、# のとき壁マスです。

はじめ、高橋君がマス (1,1) にいます。 高橋君は、今いるマスと上下左右のいずれかの方向に隣接するマスに移動することを好きな回数行うことが出来ます。 ただし、2 回続けて同じ方向に移動することは出来ません。 また、壁マスやマス目の外に移動することも出来ません。

高橋君は移動を開始する前に 0 個以上の壁マスを破壊して空きマスに変化させることが出来ます。 高橋君がマス (H,W) に辿り着くためには、最小でいくつの壁マスを破壊する必要があるでしょうか? この問題の制約下において、マス (H,W) に辿り着くことが出来るように壁マスを破壊する方法が存在することは証明出来ます。

T 個のテストケースが与えられるので、それぞれについて答えを求めて下さい。

制約

  • 1 \le T \le 1000
  • 2 \le H, W \le 100
  • S_{i,j}. または #
  • S_{1,1} および S_{H,W}.
  • 全てのテストケースにおける HW の総和は 3 \times 10^4 以下

入力

入力は以下の形式で標準入力から与えられる。

T
\text{case}_1
\text{case}_2
\vdots
\text{case}_T

各テストケースは以下の形式で与えられる。

H W
S_{1,1}S_{1,2}\ldotsS_{1,W}
S_{2,1}S_{2,2}\ldotsS_{2,W}
\vdots
S_{H,1}S_{H,2}\ldotsS_{H,W}

出力

T 行出力せよ。

i 行目には i 番目のテストケースについて、高橋君がマス (H,W) に辿り着くために破壊する壁マスの個数としてありうる最小値を出力せよ。


入力例 1

3
3 5
..##.
.#...
##.#.
2 3
..#
#..
13 13
.############
##.##..###..#
#.#.#.#.#.###
#...#..##.###
#.#.#.#.#.###
#.#.#.#.##..#
#############
#...##.##.#.#
###.#..##.#.#
#...##.##...#
#.####.####.#
#...#...###.#
############.

出力例 1

2
0
13

1 つ目のテストケースについて、下図のように 2 個の壁マスを破壊することでマス (H,W) に辿り着く事が出来ます。

左側は破壊する壁マスの例を表しており、右側は移動経路の例を表しています。 必ずしも移動回数を最小化する必要がない点や、同じマスに 2 回以上訪れても良い点に注意して下さい。

2 つ目のテストケースについて、壁マスを 1 つも破壊せずにマス (H,W) に辿り着くことが出来ます。

Score : 900 points

Problem Statement

There is an H \times W grid. Let (i,j) denote the cell at the i-th row from the top and j-th column from the left.

Each cell is an empty cell or a wall cell, and this information is represented by H \times W characters S_{1,1},\dots ,S_{H,W}. Cell (i,j) is an empty cell if S_{i,j} is ., and a wall cell if it is #.

Initially, Takahashi is at cell (1,1). He can move to a cell adjacent to his current cell in one of the four directions (up, down, left, right) any number of times. However, he cannot move in the same direction twice in a row. Also, he cannot move to a wall cell or outside the grid.

Before starting to move, he can destroy zero or more wall cells to change them into empty cells. What is the minimum number of wall cells he needs to destroy to reach cell (H,W)? Under the constraints of this problem, it can be proved that there exists a way to destroy wall cells so that he can reach cell (H,W).

You are given T test cases; solve each of them.

Constraints

  • 1 \le T \le 1000
  • 2 \le H, W \le 100
  • S_{i,j} is . or #.
  • S_{1,1} and S_{H,W} are ..
  • The sum of HW over all test cases is at most 3 \times 10^4.

Input

The input is given from Standard Input in the following format:

T
\text{case}_1
\text{case}_2
\vdots
\text{case}_T

Each test case is given in the following format:

H W
S_{1,1}S_{1,2}\ldotsS_{1,W}
S_{2,1}S_{2,2}\ldotsS_{2,W}
\vdots
S_{H,1}S_{H,2}\ldotsS_{H,W}

Output

Output T lines.

The i-th line should contain the minimum possible number of wall cells to destroy for Takahashi to reach cell (H,W) for the i-th test case.


Sample Input 1

3
3 5
..##.
.#...
##.#.
2 3
..#
#..
13 13
.############
##.##..###..#
#.#.#.#.#.###
#...#..##.###
#.#.#.#.#.###
#.#.#.#.##..#
#############
#...##.##.#.#
###.#..##.#.#
#...##.##...#
#.####.####.#
#...#...###.#
############.

Sample Output 1

2
0
13

For the first test case, Takahashi can reach cell (H,W) by destroying two wall cells as shown below.

The left side shows an example of wall cells to destroy, and the right side shows an example of a movement path. Note that it is not necessary to minimize the number of moves, and it is allowed to visit the same cell more than once.

For the second test case, he can reach cell (H,W) without destroying any wall cells.