D - Guard Placement Editorial /

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 400 点

問題文

H 行 W 列のマス目で表される部屋があります。上から i 行目、左から j 列目のマスをマス (i, j) と呼びます。

部屋の各マスの状態は H 個の文字列 S_1, S_2, \dots, S_H によって表されます。マス (i, j) の状態を表す文字を S_{i, j} (文字列 S_i の j 文字目)とすると、

  • S_{i, j} = . のとき、マス (i, j) は空きマスであることを表す。
  • S_{i, j} = # のとき、マス (i, j) は障害物があるマスであることを表す。

警備員である高橋君は、いずれかの空きマスに立ち、上下左右の 4 方向を見渡して警備を行います。

空きマス (i, j) に立ったとき、高橋君は以下の条件をともに満たすすべての空きマス (x, y) を見渡すことができます(高橋君が立っているマス (i, j) 自身も見渡せるマスに含まれます)。

  • マス (x, y) はマス (i, j) と同じ行または同じ列にある(すなわち、i = x または j = y である)。
  • マス (i, j) とマス (x, y) の間(両端を含む)にあるすべてのマスが空きマスである。

高橋君が一度に見渡すことのできる空きマスの個数として考えられる最大値を求めてください。ただし、部屋の中に空きマスが 1 つも存在しない場合は、代わりに -1 を出力してください。

制約

  • 1 \leq H, W \leq 10^5
  • H \times W \leq 10^6
  • H, W は整数
  • S_i は . および # のみからなる長さ W の文字列 (1 \leq i \leq H)

入力

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

H W
S_1
S_2
:
S_H
  • 1 行目には、部屋の行数を表す H と、列数を表す W がスペース区切りで与えられる。
  • 続く H 行のうちの i 行目(全体で 1 + i 行目)には、マス目の上から i 行目の状態を表す長さ W の文字列 S_i が与えられる。

出力

高橋君が見渡すことのできる空きマスの個数の最大値を 1 行で出力してください。ただし、空きマスが存在しない場合は -1 を出力してください。


入力例 1

4 4
..#.
#...
..#.
....

出力例 1

7

入力例 2

2 3
###
###

出力例 2

-1

入力例 3

10 10
..........
.###..###.
.#......#.
.###..###.
....##....
....##....
.###..###.
.#......#.
.###..###.
..........

出力例 3

19

入力例 4

20 20
....................
.###.###.###.###.###
.#.#.#.#.#.#.#.#.#.#
.###.###.###.###.###
....................
###.###.###.###.###.
#.#.#.#.#.#.#.#.#.#.
###.###.###.###.###.
....................
.###.###.###.###.###
.#.#.#.#.#.#.#.#.#.#
.###.###.###.###.###
....................
###.###.###.###.###.
#.#.#.#.#.#.#.#.#.#.
###.###.###.###.###.
....................
.###.###.###.###.###
.#.#.#.#.#.#.#.#.#.#
....................

出力例 4

24

入力例 5

1 1
.

出力例 5

1

Score : 400 pts

Problem Statement

There is a room represented by a grid with H rows and W columns. Let (i, j) denote the square at the i-th row from the top and the j-th column from the left.

The state of each square in the room is represented by H strings S_1, S_2, \dots, S_H. Let S_{i, j} (the j-th character of the string S_i) denote the character representing the state of square (i, j):

  • If S_{i, j} = ., square (i, j) is an empty square.
  • If S_{i, j} = #, square (i, j) is a square with an obstacle.

Takahashi, a security guard, will stand on one of the empty squares and keep watch by looking in four directions: up, down, left, and right.

When standing on an empty square (i, j), Takahashi can see all empty squares (x, y) that satisfy both of the following conditions (the square (i, j) where Takahashi is standing is also included among the visible squares):

  • Square (x, y) is in the same row or the same column as square (i, j) (that is, i = x or j = y).
  • All squares between square (i, j) and square (x, y) (inclusive) are empty squares.

Find the maximum possible number of empty squares Takahashi can see at once. However, if there are no empty squares in the room, output -1 instead.

Constraints

  • 1 \leq H, W \leq 10^5
  • H \times W \leq 10^6
  • H and W are integers.
  • S_i is a string of length W consisting of only . and # (1 \leq i \leq H).

Input

The input is given in the following format:

H W
S_1
S_2
:
S_H
  • The first line contains H, the number of rows, and W, the number of columns, separated by a space.
  • The i-th of the following H lines (the (1 + i)-th line overall) contains a string S_i of length W, representing the state of the i-th row from the top of the grid.

Output

Print the maximum possible number of empty squares Takahashi can see in a single line. However, if there are no empty squares, print -1 instead.


Sample Input 1

4 4
..#.
#...
..#.
....

Sample Output 1

7

Sample Input 2

2 3
###
###

Sample Output 2

-1

Sample Input 3

10 10
..........
.###..###.
.#......#.
.###..###.
....##....
....##....
.###..###.
.#......#.
.###..###.
..........

Sample Output 3

19

Sample Input 4

20 20
....................
.###.###.###.###.###
.#.#.#.#.#.#.#.#.#.#
.###.###.###.###.###
....................
###.###.###.###.###.
#.#.#.#.#.#.#.#.#.#.
###.###.###.###.###.
....................
.###.###.###.###.###
.#.#.#.#.#.#.#.#.#.#
.###.###.###.###.###
....................
###.###.###.###.###.
#.#.#.#.#.#.#.#.#.#.
###.###.###.###.###.
....................
.###.###.###.###.###
.#.#.#.#.#.#.#.#.#.#
....................

Sample Output 4

24

Sample Input 5

1 1
.

Sample Output 5

1