A - Alternately

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 100

問題文

N 人が一列に並んでいます。列の状態は長さ N の文字列 S で与えられ、前から i 番目の人は Si 文字目が M のとき男性、F のとき女性です。

男女が交互に並んでいるかどうか判定してください。

男性同士が隣り合う箇所も女性同士が隣り合う箇所も存在しないとき、かつ、そのときに限り、男女が交互に並んでいるといいます。

制約

  • 1 \leq N \leq 100
  • N は整数である
  • SM および F のみからなる長さ N の文字列である

入力

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

N
S

出力

男女が交互に並んでいるとき Yes、そうでないとき No と出力せよ。


入力例 1

6
MFMFMF

出力例 1

Yes

男性同士が隣り合う箇所も女性同士が隣り合う箇所も存在せず、男女が交互に並んでいます。


入力例 2

9
FMFMMFMFM

出力例 2

No

入力例 3

1
F

出力例 3

Yes

Score : 100 points

Problem Statement

There is a row of N people. They are described by a string S of length N. The i-th person from the front is male if the i-th character of S is M, and female if it is F.

Determine whether the men and women are alternating.

It is said that the men and women are alternating if and only if there is no position where two men or two women are adjacent.

Constraints

  • 1 \leq N \leq 100
  • N is an integer.
  • S is a string of length N consisting of M and F.

Input

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

N
S

Output

Print Yes if the men and women are alternating, and No otherwise.


Sample Input 1

6
MFMFMF

Sample Output 1

Yes

There is no position where two men or two women are adjacent, so the men and women are alternating.


Sample Input 2

9
FMFMMFMFM

Sample Output 2

No

Sample Input 3

1
F

Sample Output 3

Yes
B - Chessboard

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 200

問題文

チェス盤のどこにコマが置かれているか答えてください。

8 マス、横 8 マスのグリッドがあります。グリッドの各マスには、次のルールで定められる長さ 2 の文字列の名前がついています。

  • 左から 1 列目にあるマスの名前の 1 文字目は a である。同様に、左から 2,3,\ldots,8 列目にあるマスの名前の 1 文字目は b, c, d, e, f, g, h である。
  • 下から 1 行目にあるマスの名前の 2 文字目は 1 である。同様に、下から 2,3,\ldots,8 行目にあるマスの名前の 2 文字目は 2, 3, 4, 5, 6, 7, 8 である。

例えば、グリッドの左下のマスの名前は a1、右下のマスの名前は h1、右上のマスの名前は h8 です。

グリッドの状態を表す長さ 88 つの文字列 S_1,\ldots,S_8 が与えられます。
S_ij 文字目は、グリッドの上から i 行目 左から j 列目のマスにコマが置かれているとき *、置かれていないとき . であり、S_1,\ldots,S_8 の中に文字 * はちょうど 1 つ存在します。
コマが置かれているマスの名前を求めてください。

制約

  • S_i. および * のみからなる長さ 8 の文字列である
  • S_1,\ldots,S_8 の中に文字 * はちょうど 1 つ存在する。

入力

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

S_1
S_2
S_3
S_4
S_5
S_6
S_7
S_8

出力

答えを出力せよ。


入力例 1

........
........
........
........
........
........
........
*.......

出力例 1

a1

問題文中で説明したとおり、グリッドの左下のマスの名前は a1 です。


入力例 2

........
........
........
........
........
.*......
........
........

出力例 2

b3

Score : 200 points

Problem Statement

Locate a piece on a chessboard.

We have a grid with 8 rows and 8 columns of squares. Each of the squares has a 2-character name determined as follows.

  • The first character of the name of a square in the 1-st column from the left is a. Similarly, the first character of the name of a square in the 2-nd, 3-rd, \ldots, 8-th column from the left is b, c, d, e, f, g, h, respectively.
  • The second character of the name of a square in the 1-st row from the bottom is 1. Similarly, the second character of the name of a square in the 2-nd, 3-rd, \ldots, 8-th row from the bottom is 2, 3, 4, 5, 6, 7, 8, respectively.

For instance, the bottom-left square is named a1, the bottom-right square is named h1, and the top-right square is named h8.

You are given 8 strings S_1,\ldots,S_8, each of length 8, representing the state of the grid.
The j-th character of S_i is * if the square at the i-th row from the top and j-th column from the left has a piece on it, and . otherwise. The character * occurs exactly once among S_1,\ldots,S_8. Find the name of the square that has a piece on it.

Constraints

  • S_i is a string of length 8 consisting of . and*.
  • The character * occurs exactly once among S_1,\ldots,S_8.

Input

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

S_1
S_2
S_3
S_4
S_5
S_6
S_7
S_8

Output

Print the answer.


Sample Input 1

........
........
........
........
........
........
........
*.......

Sample Output 1

a1

As explained in the problem statement, the bottom-left square is named a1.


Sample Input 2

........
........
........
........
........
.*......
........
........

Sample Output 2

b3
C - Gap Existence

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 300

問題文

長さ N の数列 A=(A_1,\ldots,A_N) が与えられます。

1\leq i,j \leq N である組 (i,j) であって、A_i-A_j=X となるものが存在するかどうか判定してください。

制約

  • 2 \leq N \leq 2\times 10^5
  • -10^9 \leq A_i \leq 10^9
  • -10^9 \leq X \leq 10^9
  • 入力は全て整数である

入力

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

N X
A_1 \ldots A_N

出力

1\leq i,j \leq N である組 (i,j) であって、A_i-A_j=X となるものが存在するとき Yes、存在しないとき No と出力せよ。


入力例 1

6 5
3 1 4 1 5 9

出力例 1

Yes

A_6-A_3=9-4=5 です。


入力例 2

6 -4
-2 -7 -1 -8 -2 -8

出力例 2

No

A_i-A_j=-4 となる組 (i,j) は存在しません。


入力例 3

2 0
141421356 17320508

出力例 3

Yes

A_1-A_1=0 です。

Score : 300 points

Problem Statement

You are given a sequence of N numbers: A=(A_1,\ldots,A_N).

Determine whether there is a pair (i,j) with 1\leq i,j \leq N such that A_i-A_j=X.

Constraints

  • 2 \leq N \leq 2\times 10^5
  • -10^9 \leq A_i \leq 10^9
  • -10^9 \leq X \leq 10^9
  • All values in the input are integers.

Input

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

N X
A_1 \ldots A_N

Output

Print Yes if there is a pair (i,j) with 1\leq i,j \leq N such that A_i-A_j=X, and No otherwise.


Sample Input 1

6 5
3 1 4 1 5 9

Sample Output 1

Yes

We have A_6-A_3=9-4=5.


Sample Input 2

6 -4
-2 -7 -1 -8 -2 -8

Sample Output 2

No

There is no pair (i,j) such that A_i-A_j=-4.


Sample Input 3

2 0
141421356 17320508

Sample Output 3

Yes

We have A_1-A_1=0.

D - M<=ab

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 400

問題文

正整数 N,M が与えられます。
次の条件をともにみたす最小の正整数 X を求めてください。 ただし、そのようなものが存在しない場合は -1 を出力してください。

  • X1 以上 N 以下の整数 a,b の積として表される。ここで、ab は同じ整数でも良い。
  • XM 以上である。

制約

  • 1\leq N\leq 10^{12}
  • 1\leq M\leq 10^{12}
  • N,M は整数

入力

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

N M

出力

問題文の条件をともにみたす最小の正整数 X を出力せよ。そのようなものが存在しない場合は -1 を出力せよ。


入力例 1

5 7

出力例 1

8

まず、71 以上 5 以下の整数 2 つの積として表すことはできません。
次に、88=2\times 4 などとして、1 以上 5 以下の整数 2 つの積として表すことができます。

よって、8 を出力します。


入力例 2

2 5

出力例 2

-1

1\times 1=11\times 2=22\times 1=22\times 2=4 より、 1 以上 2 以下の整数 2 つの積として表す事ができるのは 1,2,4 のみであるため、 5 以上の数はそのような整数 2 つの積として表すことができません。
よって、-1 を出力します。


入力例 3

100000 10000000000

出力例 3

10000000000

a=b=100000 (=10^5)とした時、a,b の積は 10000000000 (=10^{10}) となり、これが答えとなります。
答えが 32 bit 整数型に収まらない場合があることに注意してください。

Score : 400 points

Problem Statement

You are given positive integers N and M.
Find the smallest positive integer X that satisfies both of the conditions below, or print -1 if there is no such integer.

  • X can be represented as the product of two integers a and b between 1 and N, inclusive. Here, a and b may be the same.
  • X is at least M.

Constraints

  • 1\leq N\leq 10^{12}
  • 1\leq M\leq 10^{12}
  • N and M are integers.

Input

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

N M

Output

Print the smallest positive integer X that satisfies both of the conditions, or -1 if there is no such integer.


Sample Input 1

5 7

Sample Output 1

8

First, 7 cannot be represented as the product of two integers between 1 and 5.
Second, 8 can be represented as the product of two integers between 1 and 5, such as 8=2\times 4.

Thus, you should print 8.


Sample Input 2

2 5

Sample Output 2

-1

Since 1\times 1=1, 1\times 2=2, 2\times 1=2, and 2\times 2=4, only 1, 2, and 4 can be represented as the product of two integers between 1 and 2, so no number greater than or equal to 5 can be represented as the product of two such integers.
Thus, you should print -1.


Sample Input 3

100000 10000000000

Sample Output 3

10000000000

For a=b=100000 (=10^5), the product of a and b is 10000000000 (=10^{10}), which is the answer.
Note that the answer may not fit into a 32-bit integer type.

E - Transition Game

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500

問題文

長さ N の数列 A=(A_1,A_2,\ldots,A_N) が与えられます。ここで、各 A_i (1\leq i\leq N)1\leq A_i \leq N をみたします。

高橋君と青木君が N 回ゲームを行います。i=1,2,\ldots,N 回目のゲームは次のようにして行われます。

  1. 青木君が正整数 K_i を指定する。
  2. 青木君の指定した K_i を聞いた後、高橋君は 1 以上 N 以下の整数 S_i を選び、黒板に書く。
  3. その後、 K_i 回次の操作を繰り返す。

    • 黒板に x が書かれているとき、それを消し、A_x を新しく書く。

K_i 回の操作の後で黒板に書かれている整数が i ならば i 回目のゲームは高橋君の勝ち、そうでないならば青木君の勝ちとなります。
ここで、K_i,S_ii=1,2,\ldots,N に対して独立に選ぶ事ができます。

両者が、自身が勝つために最善の行動をとったとき、N 回のゲームのうち高橋君が勝つ回数を求めてください。

制約

  • 1\leq N\leq 2\times 10^5
  • 1\leq A_i\leq N
  • 入力はすべて整数

入力

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

N
A_1 A_2 \ldots A_N

出力

両者が最善の行動をとったとき、N 回のゲームのうち高橋君が勝つ回数を出力せよ。


入力例 1

3
2 2 3

出力例 1

2

1 回目のゲームにおいて、青木君が K_1=2 を指定したとき、高橋君は S_1 として、1,2,3 のいずれを選んでも勝つことはできません。

例えば、高橋君が最初に黒板に S_1=1 と書いたとすると、2 回の操作で黒板に書かれている数は順に 1\to 2(=A_1)2\to 2(=A_2) と変化し、
最終的に黒板に書かれている数は 2(\neq 1) であるため、青木君の勝ちとなります。

一方、2,3 回目のゲームにおいては、青木君の指定した K_i の値によらず、高橋君はそれぞれ 2,3 を最初に黒板に書くことで勝つ事ができます。

よって、両者が、自分が勝つために最善の行動をとったとき、高橋君が勝つゲームは 2,3 回目の 2 回であるため、2 を出力します。


入力例 2

2
2 1

出力例 2

2

1 回目のゲームにおいて、高橋君は、青木君が指定した K_1 が奇数のときは 2、偶数のときは 1 を最初に黒板に書く事で勝つ事ができます。

同様に 2 回目のゲームにおいても勝つ方法が存在するため、1,2 回目のゲームのいずれでも高橋君は勝利する事ができ、2 が答えとなります。

Score : 500 points

Problem Statement

You are given a sequence of N numbers: A=(A_1,A_2,\ldots,A_N). Here, each A_i (1\leq i\leq N) satisfies 1\leq A_i \leq N.

Takahashi and Aoki will play N rounds of a game. For each i=1,2,\ldots,N, the i-th game will be played as follows.

  1. Aoki specifies a positive integer K_i.
  2. After knowing K_i Aoki has specified, Takahashi chooses an integer S_i between 1 and N, inclusive, and writes it on a blackboard.
  3. Repeat the following K_i times.

    • Replace the integer x written on the blackboard with A_x.

If i is written on the blackboard after the K_i iterations, Takahashi wins the i-th round; otherwise, Aoki wins.
Here, K_i and S_i can be chosen independently for each i=1,2,\ldots,N.

Find the number of rounds Takahashi wins if both players play optimally to win.

Constraints

  • 1\leq N\leq 2\times 10^5
  • 1\leq A_i\leq N
  • All values in the input are integers.

Input

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

N
A_1 A_2 \ldots A_N

Output

Find the number of rounds Takahashi wins if both players play optimally to win.


Sample Input 1

3
2 2 3

Sample Output 1

2

In the first round, if Aoki specifies K_1=2, Takahashi cannot win whichever option he chooses for S_1: 1, 2, or 3.

For example, if Takahashi writes S_1=1 on the initial blackboard, the two operations will change this number as follows: 1\to 2(=A_1), 2\to 2(=A_2). The final number on the blackboard will be 2(\neq 1), so Aoki wins.

On the other hand, in the second and third rounds, Takahashi can win by writing 2 and 3, respectively, on the initial blackboard, whatever value Aoki specifies as K_i.

Therefore, if both players play optimally to win, Takashi wins two rounds: the second and the third. Thus, you should print 2.


Sample Input 2

2
2 1

Sample Output 2

2

In the first round, Takahashi can win by writing 2 on the initial blackboard if K_1 specified by Aoki is odd, and 1 if it is even.

Similarly, there is a way for Takahashi to win the second round. Thus, Takahashi can win both rounds: the answer is 2.

F - Simultaneous Swap

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500

問題文

長さ N の数列 A=(A_1,A_2,\ldots,A_N), B=(B_1,B_2,\ldots,B_N) が与えられます。

高橋君は次の操作を好きなだけ (0 回でも良い) 繰り返す事ができます。

1 以上 N 以下の、どの 2 つも互いに相異なる 3 つの整数 i,j,k を選ぶ。
Ai 番目の要素と j 番目の要素を交換し、Bi 番目の要素と k 番目の要素を交換する。

高橋君がうまく操作を繰り返すことによって、 AB を一致させる事が可能ならば Yes を、不可能ならば No を出力してください。
ただし、AB が一致しているとは、任意の 1\leq i\leq N について Ai 番目の要素と Bi 番目の要素が等しいことを言います。

制約

  • 3 \leq N \leq 2\times 10^5
  • 1\leq A_i,B_i\leq N
  • 入力はすべて整数

入力

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

N
A_1 A_2 \ldots A_N
B_1 B_2 \ldots B_N

出力

操作を繰り返すことによって、高橋君が AB を一致させる事が可能ならば Yes を、不可能ならば No を出力せよ。


入力例 1

3
1 2 1
1 1 2

出力例 1

Yes

(i,j,k)=(1,2,3) として 1 回操作を行うことで、A_1A_2B_1B_3 がそれぞれ交換され、
A,B はともに (2,1,1) となって一致します。よって、Yes を出力します。


入力例 2

3
1 2 2
1 1 2

出力例 2

No

どのように操作を行っても AB を一致させることはできません。よって、No を出力します。


入力例 3

5
1 2 3 2 1
3 2 2 1 1

出力例 3

Yes

入力例 4

8
1 2 3 4 5 6 7 8
7 8 5 6 4 3 1 2

出力例 4

No

Score : 500 points

Problem Statement

You are given two sequences of N numbers: A=(A_1,A_2,\ldots,A_N) and B=(B_1,B_2,\ldots,B_N).

Takahashi can repeat the following operation any number of times (possibly zero).

Choose three pairwise distinct integers i, j, and k between 1 and N.
Swap the i-th and j-th elements of A, and swap the i-th and k-th elements of B.

If there is a way for Takahashi to repeat the operation to make A and B equal, print Yes; otherwise, print No.
Here, A and B are said to be equal when, for every 1\leq i\leq N, the i-th element of A and that of B are equal.

Constraints

  • 3 \leq N \leq 2\times 10^5
  • 1\leq A_i,B_i\leq N
  • All values in the input are integers.

Input

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

N
A_1 A_2 \ldots A_N
B_1 B_2 \ldots B_N

Output

Print Yes if there is a way for Takahashi to repeat the operation to make A and B equal, and print No otherwise.


Sample Input 1

3
1 2 1
1 1 2

Sample Output 1

Yes

Performing the operation once with (i,j,k)=(1,2,3) swaps A_1 and A_2, and swaps B_1 and B_3,
making both A and B equal to (2,1,1). Thus, you should print Yes.


Sample Input 2

3
1 2 2
1 1 2

Sample Output 2

No

There is no way to perform the operation to make A and B equal, so you should print No.


Sample Input 3

5
1 2 3 2 1
3 2 2 1 1

Sample Output 3

Yes

Sample Input 4

8
1 2 3 4 5 6 7 8
7 8 5 6 4 3 1 2

Sample Output 4

No
G - Polygon and Points

Time Limit: 3 sec / Memory Limit: 1024 MB

配点 : 600

問題文

x 軸の正の向きを右、y 軸の正の向きを上とする 2 次元座標平面上に、凸 N 角形 S があります。S の頂点の座標は、反時計回りに (X_1,Y_1),\ldots,(X_N,Y_N) です。

Q 個の点 (A_i,B_i) について、それぞれ S の内部・外部・境界上のいずれにあるか求めてください。

制約

  • 3 \leq N \leq 2\times 10^5
  • 1 \leq Q \leq 2\times 10^5
  • -10^9 \leq X_i,Y_i,A_i,B_i \leq 10^9
  • S は狭義凸 N 角形である。すなわち、全ての内角は 180 度未満である。
  • (X_1,Y_1),\ldots,(X_N,Y_N)S の頂点を反時計回りに列挙したものである。
  • 入力は全て整数である。

入力

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

N
X_1 Y_1
\vdots
X_N Y_N
Q
A_1 B_1
\vdots
A_Q B_Q

出力

Q 行出力せよ。i 行目には、(A_i,B_i)S の内部(境界含まず)にあるとき IN、外部(境界含まず)にあるとき OUT、境界上にあるとき ON と出力せよ。


入力例 1

4
0 4
-2 2
-1 0
3 1
3
-1 3
0 2
2 0

出力例 1

ON
IN
OUT

S 及び 与えられた 3 個の点は下図の通りです。1 番目の点は S の境界上、2 番目の点は内部、3 番目の点は外部にあります。

図


入力例 2

3
0 0
1 0
0 1
3
0 0
1 0
0 1

出力例 2

ON
ON
ON

Score : 600 points

Problem Statement

There is a convex N-gon S in the two-dimensional coordinate plane where the positive x-axis points to the right and the positive y-axis points upward. The vertices of S have the coordinates (X_1,Y_1),\ldots,(X_N,Y_N) in counter-clockwise order.

For each of Q points (A_i,B_i), answer the following question: is that point inside or outside or on the boundary of S?

Constraints

  • 3 \leq N \leq 2\times 10^5
  • 1 \leq Q \leq 2\times 10^5
  • -10^9 \leq X_i,Y_i,A_i,B_i \leq 10^9
  • S is a strictly convex N-gon. That is, its interior angles are all less than 180 degrees.
  • (X_1,Y_1),\ldots,(X_N,Y_N) are the vertices of S in counter-clockwise order.
  • All values in the input are integers.

Input

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

N
X_1 Y_1
\vdots
X_N Y_N
Q
A_1 B_1
\vdots
A_Q B_Q

Output

Print Q lines. The i-th line should contain IN if (A_i,B_i) is inside S (and not on the boundary), OUT if it is outside S (and not on the boundary), and ON if it is on the boundary of S.


Sample Input 1

4
0 4
-2 2
-1 0
3 1
3
-1 3
0 2
2 0

Sample Output 1

ON
IN
OUT

The figure below shows S and the given three points. The first point is on the boundary of S, the second is inside S, and the third is outside S.

Figure


Sample Input 2

3
0 0
1 0
0 1
3
0 0
1 0
0 1

Sample Output 2

ON
ON
ON
Ex - Unite

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 600

問題文

NM 列のマス目があり、各マスは黒または白で塗られています。 ここで、少なくとも 1 つのマスが黒く塗られています。
最初のマス目の状態は N 個の長さ M の文字列 S_1,S_2,\ldots,S_N で与えられます。
マス目の上から i 行目 (1\leq i\leq N) かつ左から j 列目 (1\leq j\leq M) のマスは、 S_ij 文字目が # であるとき黒く、. であるとき白く塗られています。

高橋君の目標は白く塗られたいくつかのマス (0 個でもよい ) を新しく黒く塗ることによって、黒く塗られたマス全体が 連結 になるようにすることです。 高橋君が目標を達成するために新しく塗る必要のあるマスの個数としてあり得る最小値を求めてください。

ただし、黒く塗られたマス全体が 連結 であるとは、黒く塗られたどの 2 つのマスの組 (S,T) についても、 正整数 K と長さ K の黒く塗られたマスの列 X=(x_1,x_2,\ldots,x_K) であって、x_1=S, x_K=T かつ任意の 1\leq i\leq K-1 について x_ix_{i+1} が辺を共有しているようなものが存在することをいいます。
なお、問題の制約下でつねに、高橋君が目標を達成するような塗り方が存在することが証明できます。

制約

  • 1 \leq N \leq 100
  • 1\leq M \leq 7
  • N,M は整数
  • S_i#. のみからなる長さ M の文字列
  • 与えられるマス目において、黒く塗られたマスが 1 つ以上存在する。

入力

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

N M
S_1
S_2
\vdots
S_N

出力

黒く塗られたマス全体が連結になるようにするために、新しく塗る必要のあるマスの個数としてあり得る最小値を出力せよ。


入力例 1

3 5
...#.
.#...
....#

出力例 1

3

最初、マス目の状態は次のようになっています。ここで、上から i 行目、左から j 列目のマスを (i,j) で表しています。

ここで、例えば、高橋君が (2,3),(2,4),(3,4)3 つのマス(下図の赤いマス)を新しく黒く塗ったとします。

このとき、最初から黒く塗られていたマスと新しく黒く塗られたマスは合わせて次のようになり、黒く塗られたマス全体は連結となります。

2 つ以下のマスを新しく黒く塗ることで黒く塗られたマス全体を連結にすることはできないため、3 が答えとなります。
白く塗られたマス全体を連結にする必要はないことに注意してください。


入力例 2

3 3
###
###
###

出力例 2

0

最初から全てのマスが黒く塗られている可能性もあります。


入力例 3

10 1
.
#
.
.
.
.
.
.
#
.

出力例 3

6

Score : 600 points

Problem Statement

We have a grid with N rows and M columns, where each square is painted black or white. Here, at least one square is painted black.
The initial state of the grid is given as N strings S_1,S_2,\ldots,S_N of length M.
The square at the i-th row from the top and j-th column from the left is painted black if the j-th character of S_i is #, and white if it is ..

Takahashi wants to repaint some white squares (possibly zero) black so that the squares painted black are connected. Find the minimum number of squares he needs to repaint to achieve his objective.

Here, the squares painted black are said to be connected when, for every pair of squares (S,T) painted black, there are a positive integer K and a sequence of K squares X=(x_1,x_2,\ldots,x_K) painted black such that x_1=S, x_K=T, and x_i and x_{i+1} share a side for every 1\leq i\leq K-1.
It can be proved that, under the constraints of the problem, there is always a way for Takahashi to achieve his objective.

Constraints

  • 1 \leq N \leq 100
  • 1\leq M \leq 7
  • N and M are integers.
  • S_i is a string of length M consisting of # and ..
  • The given grid has at least one square painted black.

Input

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

N M
S_1
S_2
\vdots
S_N

Output

Print the minimum number of squares that Takahashi needs to repaint for the squares painted black to be connected.


Sample Input 1

3 5
...#.
.#...
....#

Sample Output 1

3

The initial grid looks as follows, where (i,j) denotes the square at the i-th row from the top and j-th column from the left.

Assume that Takahashi repaints three squares (2,3),(2,4),(3,4) (shown red in the figure below) black.

Then, we have the following squares painted black, including the ones painted black from the beginning. These squares are connected.

It is impossible to repaint two or fewer squares black so that the squares painted black are connected, so the answer is 3.
Note that the squares painted white do not have to be connected.


Sample Input 2

3 3
###
###
###

Sample Output 2

0

All squares might be painted black from the beginning.


Sample Input 3

10 1
.
#
.
.
.
.
.
.
#
.

Sample Output 3

6