PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#DSU0000060. Sân trượt băng (Skate)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Sân trượt băng (Skate)

Skate

Source: AtCoder

Version: Phuoc Hung OJ Extended

Problem Statement

A skating rink has HH rows and WW columns, with walls around its boundary. Every square is either ice (.) or ground (#). The skater may begin at rest on any square of the rink, including a ground square.

On each move, the skater chooses one of the four directions: north, south, east, or west. Once the skater starts moving, they continue straight until reaching a boundary wall or a ground square other than the square where that move started. The skater stops there and can then choose another direction. In particular, a ground square is not impassable: the skater can enter and stop on it.

A square is considered visited if the skater passes through it; stopping on the square is not necessary. The rink is called efficient if, from every possible starting square, there is a sequence of moves that visits every square on the rink.

Before play begins, you may change some ice squares . into ground squares #. Find the minimum number of changes required to make the rink efficient.

Input

The first line contains H,WH,W, followed by HH strings of . and #, each of length WW.

Output

Print the minimum number of cells to change.

Subtasks

  • Subtask 1 (20 points): 2≤H,W≤102\le H,W\le10; Every cell is either . or #.
  • Subtask 2 (30 points): 2≤H,W≤1002\le H,W\le100; Every cell is either . or #.
  • Subtask 3 (50 points): 2≤H,W≤10002\le H,W\le1000; Every cell is either . or #.

Examples

Input

3 9
.........
.........
.........

Output

1

Explanation

Initially, starting from (1,1)(1,1), it is impossible to pass square (2,2)(2,2). Changing just square (2,9)(2,9) from . to # produces:

.........
........#
.........

After this change, a route covering every square exists from any starting square. One change is sufficient and necessary.