Problem:


Score : 400 points

Problem Statement

There is a grid with H horizontal rows and W vertical columns, and there are obstacles on some of the squares.

Snuke is going to choose one of the squares not occupied by an obstacle and place a lamp on it.
The lamp placed on the square will emit straight beams of light in four cardinal directions: up, down, left, and right.
In each direction, the beam will continue traveling until it hits a square occupied by an obstacle or it hits the border of the grid. It will light all the squares on the way, including the square on which the lamp is placed, but not the square occupied by an obstacle.

Snuke wants to maximize the number of squares lighted by the lamp.

You are given H strings S_i (1 \leq i \leq H), each of length W. If the j-th character (1 \leq j \leq W) of S_i is #, there is an obstacle on the square at the i-th row from the top and the j-th column from the left; if that character is ., there is no obstacle on that square.

Find the maximum possible number of squares lighted by the lamp.

Constraints

1 \leq H \leq 2,000

1 \leq W \leq 2,000

S_i is a string of length W consisting of # and ..

. occurs at least once in one of the strings S_i (1 \leq i \leq H).

Input

Input is given from Standard Input in the following format:

H W
S_1
:
S_H

Output

Print the maximum possible number of squares lighted by the lamp.

Sample Input 1

4 6
#..#..
.....#
....#.
#.#...

Sample Output 1

8

If Snuke places the lamp on the square at the second row from the top and the second column from the left, it will light the following squares: the first through fifth squares from the left in the second row, and the first through fourth squares from the top in the second column, for a total of eight squares.

Sample Input 2

8 8
..#...#.
....#...
##......
..###..#
...#..#.
##....#.
#...#...
###.#..#

Sample Output 2

13



Sample input:
4 6
#..#..
.....#
....#.
#.#...


Sample output:
8

Submission status: Accepted

Solution code:
import numpy as np
import sys
def main():
    line = sys.stdin.readline

    H, W = map(int, line().split())
    grid = np.zeros((H + 2, W + 2), np.int32)
    grid[1:-1, 1:-1] = (np.array([list(line().rstrip()) for _ in range(H)]).reshape(H, W) == '.') * 1
    left = np.zeros((H + 2, W + 2), np.int32)
    right = np.zeros((H + 2, W + 2), np.int32)
    up = np.zeros((H + 2, W + 2), np.int32)
    down = np.zeros((H + 2, W + 2), np.int32)

    # 下方向に照らせるマスの数（自分の場所含む）
    down[H] = grid[H]
    for i in range(H, 0, -1):
        down[i - 1] = (down[i] + 1) * grid[i - 1]

    up[1] = grid[1]
    for i in range(1, H):
        up[i + 1] = (up[i] + 1) * grid[i + 1]

    right[:, W] = grid[:, W]
    for i in range(W, 0, -1):
        right[:, i - 1] = (right[:, i] + 1) * grid[:, i - 1]

    left[:, 1] = grid[:, 1]
    for i in range(1, W):
        left[:, i + 1] = (left[:, i] + 1) * grid[:, i + 1]

    answer = (left + right + up + down) * grid
    print(np.max(answer) - 3)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import numpy as np
import sys
line = sys.stdin.readline

H, W = map(int, line().split())
grid = np.zeros((H + 2, W + 2), np.int32)
grid[1:-1, 1:-1] = (np.array([list(line().rstrip()) for _ in range(H)]).reshape(H, W) == '.') * 1
left = np.zeros((H + 2, W + 2), np.int32)
right = np.zeros((H + 2, W + 2), np.int32)
up = np.zeros((H + 2, W + 2), np.int32)
down = np.zeros((H + 2, W + 2), np.int32)

# 下方向に照らせるマスの数（自分の場所含む）
down[H] = grid[H]
for i in range(H, 0, -1):
    down[i - 1] = (down[i] + 1) * grid[i - 1]

up[1] = grid[1]
for i in range(1, H):
    up[i + 1] = (up[i] + 1) * grid[i + 1]

right[:, W] = grid[:, W]
for i in range(W, 0, -1):
    right[:, i - 1] = (right[:, i] + 1) * grid[:, i - 1]

left[:, 1] = grid[:, 1]
for i in range(1, W):
    left[:, i + 1] = (left[:, i] + 1) * grid[:, i + 1]

answer = (left + right + up + down) * grid
print(np.max(answer) - 3)

Submission status: Accepted

Solution code:
import sys
h, w = [int(i) for i in sys.stdin.readline().split()]
s_ls = []
for y in range(h):
    s = input()
    s_ls.append(s)
r_ij = [[0 for i in range(w)] for j in range(h)]
l_ij = [[0 for i in range(w)] for j in range(h)]
d_ij = [[0 for i in range(w)] for j in range(h)]
u_ij = [[0 for i in range(w)] for j in range(h)]
for y in range(h):
    for x in range(w):
        if s_ls[y][x] == "#":
            continue
        r_ij[y][x] = r_ij[y][max(0, x-1)] + 1
        d_ij[y][x] = d_ij[max(0, y-1)][x] + 1
for y in range(h-1,-1,-1):
    for x in range(w-1,-1,-1):
        if s_ls[y][x] == "#":
            continue
        l_ij[y][x] = l_ij[y][min(w-1, x+1)] + 1
        u_ij[y][x] = u_ij[min(y+1, h-1)][x] + 1

best = -1e10
for y in range(h):
    for x in range(w):
        res = r_ij[y][x] + l_ij[y][x] + u_ij[y][x] + d_ij[y][x] - 3
        if res > best:
            best = res
print(best)

Submission status: Accepted

Solution code:
import numpy as np
import sys
buf = sys.stdin.buffer

H, W = map(int, buf.readline().split())
grid = np.zeros((H + 2, W + 2), np.int32)
grid[1:-1, 1:-1] = (np.array(list(map(list, buf.read().decode('utf-8').split()))).reshape(H, W) == '.') * 1
grid_v = grid.T

right = np.maximum.accumulate(np.where(grid < 1, np.arange(W + 2), 0), axis=1)
left = np.fliplr(np.minimum.accumulate(np.fliplr(np.where(grid < 1, np.arange(W + 2), 10 ** 10)).copy(), axis=1))
down = np.maximum.accumulate(np.where(grid_v < 1, np.arange(H + 2), 0), axis=1).T
up = np.fliplr(np.minimum.accumulate(np.fliplr(np.where(grid_v < 1, np.arange(H + 2), 10 ** 10)).copy(), axis=1)).T

print(np.max((left - right) + (up - down)) - 3)

Submission status: Accepted

Solution code:
import numpy as np
import sys
line = sys.stdin.readline

H, W = map(int, line().split())
grid = np.zeros((H + 2, W + 2), np.int32)
grid[1:-1, 1:-1] = (np.array([list(line().rstrip()) for _ in range(H)]).reshape(H, W) == '.') * 1
grid_v = grid.T

right = np.maximum.accumulate(np.where(grid < 1, np.arange(W + 2), 0), axis=1)
left = np.fliplr(np.minimum.accumulate(np.fliplr(np.where(grid < 1, np.arange(W + 2), 10 ** 10)).copy(), axis=1))
down = np.maximum.accumulate(np.where(grid_v < 1, np.arange(H + 2), 0), axis=1).T
up = np.fliplr(np.minimum.accumulate(np.fliplr(np.where(grid_v < 1, np.arange(H + 2), 10 ** 10)).copy(), axis=1)).T

print(np.max((left - right) + (up - down)) - 3)

Submission status: Accepted

Solution code:
import numpy as np
import sys
buf = sys.stdin.buffer

H, W = map(int, buf.readline().split())
grid = np.zeros((H + 2, W + 2), np.int32)
grid[1:-1, 1:-1] = np.where(np.array(list(map(list, buf.read().decode('utf-8').split()))).reshape(H, W) == '.', 1, 0)
grid_v = grid.T

right = np.maximum.accumulate(np.where(grid < 1, np.arange(W + 2), 0), axis=1)
left = np.fliplr(np.minimum.accumulate(np.fliplr(np.where(grid < 1, np.arange(W + 2), 10 ** 10)).copy(), axis=1))
down = np.maximum.accumulate(np.where(grid_v < 1, np.arange(H + 2), 0), axis=1).T
up = np.fliplr(np.minimum.accumulate(np.fliplr(np.where(grid_v < 1, np.arange(H + 2), 10 ** 10)).copy(), axis=1)).T

print(np.max((left - right) + (up - down)) - 3)

Submission status: Accepted

Solution code:
import sys
from itertools import accumulate
input = sys.stdin.readline
def inputs():return [int(x) for x in input().split()]
H,W = inputs()
meiro = [tuple(input()) for i in range(H)]
#print(meiro)
imosw = [[0]*(W) for _ in range(H)]
imosh = [[0]*(H) for _ in range(W)]
for i in range(H):
    cnt=0
    for j in range(W):
        if meiro[i][j]=="#":
            if cnt!=0:
                imosw[i][j]-=cnt
                imosw[i][j-cnt]+=cnt
            cnt = 0
        else:
            cnt+=1
    if cnt!=0:
    	imosw[i][W-1-cnt+1]+=cnt
    imosw[i][:]=list(accumulate(imosw[i]))
for j in range(W):
    cnt= 0
    imos=[0]*H
    for i in range(H):
        if meiro[i][j]=="#":
            if cnt!=0:
                imos[i]-=cnt
                imos[i-cnt]+=cnt
            cnt = 0
        else:
            cnt+=1
    if cnt!=0:
    	imos[H-1-cnt+1]+=cnt
    imosh[j]=list(accumulate(imos))
    #print(imosh[j])
ans = 0
for i in range(H):
  for j in range(W):
    ans = max(ans,imosw[i][j]+imosh[j][i]-1)
print(ans)

Submission status: Accepted

Solution code:
H,W=map(int,input().split())
field=[list(input()) for _ in range(H)]

l=[[0]*W for _ in range(H)]
r=[[0]*W for _ in range(H)]
u=[[0]*W for _ in range(H)]
d=[[0]*W for _ in range(H)]

for j in range(H):
  for i in range(W):
    if field[j][i]==".":
      if i==0:
        l[j][i]=1
      else:
        l[j][i]=l[j][i-1]+1
      if j==0:
        u[j][i]=1
      else:
        u[j][i]=u[j-1][i]+1
        

for j in reversed(range(H)):
  for i in reversed(range(W)):
    if field[j][i]==".":
      if i==W-1:
        r[j][i]=1
      else:
        r[j][i]=r[j][i+1]+1
      if j==H-1:
        d[j][i]=1
      else:
        d[j][i]=d[j+1][i]+1

A=[]
for j in range(H):
  for i in range(W):
    A.append(l[j][i]+r[j][i]+d[j][i]+u[j][i]-3)

print(max(A))