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:
# ABC129D - Lamp
import sys
input = sys.stdin.readline

def main():
    H, W = tuple(map(int, input().split()))
    S = tuple(input().rstrip() for _ in range(H))
    L, R = [[0] * W for _ in range(H)], [[0] * W for _ in range(H)]
    U, D = [[0] * W for _ in range(H)], [[0] * W for _ in range(H)]
    for x1 in range(H):
        for y1 in range(W):
            if S[x1][y1] == ".":  # left and upper sides of S[i][j]
                U[x1][y1] = U[x1 - 1][y1] + 1
                L[x1][y1] = L[x1][y1 - 1] + 1
            x2, y2 = -x1 - 1, -y1 - 1
            if S[x2][y2] == ".":  # right and under sides
                D[x2][y2] = D[x2 + 1][y2] + 1
                R[x2][y2] = R[x2][y2 + 1] + 1
    ans = -float("inf")
    for i in range(H):
        for j in range(W):
            x = L[i][j] + R[i][j] + U[i][j] + D[i][j] - 3
            if x > ans:
                ans = x
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# ABC129D - Lamp
import sys
input = sys.stdin.readline

def main():
    H, W = tuple(map(int, input().split()))
    S = tuple(input().rstrip() for _ in range(H))
    L, R = [[0] * W for _ in range(H)], [[0] * W for _ in range(H)]
    U, D = [[0] * W for _ in range(H)], [[0] * W for _ in range(H)]
    for x1 in range(H):
        for y1 in range(W):
            if S[x1][y1] == ".":  # left and upper sides of S[i][j]
                U[x1][y1] = U[x1 - 1][y1] + 1
                L[x1][y1] = L[x1][y1 - 1] + 1
            x2, y2 = -x1 - 1, -y1 - 1
            if S[x2][y2] == ".":  # right and under sides
                D[x2][y2] = D[x2 + 1][y2] + 1
                R[x2][y2] = R[x2][y2 + 1] + 1
    ans = -float("inf")
    for i in range(H):
        for j in range(W):
            ans = max(ans, L[i][j] + R[i][j] + U[i][j] + D[i][j] - 3)
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# ABC129D - Lamp
import sys
input = sys.stdin.readline

def main():
    H, W = tuple(map(int, input().split()))
    S = tuple(input().rstrip() for _ in range(H))
    L, R = [[0] * W for _ in range(H)], [[0] * W for _ in range(H)]
    U, D = [[0] * W for _ in range(H)], [[0] * W for _ in range(H)]
    for x1 in range(H):
        for y1 in range(W):
            if S[x1][y1] == ".":  # left and upper sides of S[i][j]
                U[x1][y1] = U[x1 - 1][y1] + 1
                L[x1][y1] = L[x1][y1 - 1] + 1
            x2, y2 = -x1 - 1, -y1 - 1
            if S[x2][y2] == ".":  # right and under sides
                D[x2][y2] = D[x2 + 1][y2] + 1
                R[x2][y2] = R[x2][y2 + 1] + 1
    cand = [
        L[i][j] + R[i][j] + U[i][j] + D[i][j] - 3 for i in range(H) for j in range(W)
    ]
    ans = max(cand)
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
def slove():
    import sys
    input = sys.stdin.readline
    H, W = tuple(map(int, input().split()))
    s = tuple(input().rstrip() for _ in range(H))
    L, R = [[0] * W for _ in range(H)], [[0] * W for _ in range(H)]
    U, D = [[0] * W for _ in range(H)], [[0] * W for _ in range(H)]
    for i in range(H):
        for j in range(W):
            if s[i][j] == "#":
                L[i][j] = 0
            else:
                if i == 0:
                    L[i][j] = 1
                else:
                    L[i][j] = L[i - 1][j] + 1
            if s[-i-1][j] == "#":
                R[-i-1][j] = 0
            else:
                if i == 0:
                    R[-i-1][j] = 1
                else:
                    R[-i-1][j] = R[-i][j] + 1
            if s[i][j] == "#":
                U[i][j] = 0
            else:
                if j == 0:
                    U[i][j] = 1
                else:
                    U[i][j] = U[i][j-1] + 1
            if s[i][-j-1] == "#":
                D[i][-j-1] = 0
            else:
                if j == 0:
                    D[i][-j-1] = 1
                else:
                    D[i][-j-1] = D[i][-j] + 1
    mn = 0
    for i in range(H):
        for j in range(W):
            mn = max(mn, U[i][j] + D[i][j] + L[i][j] + R[i][j] - 3)
    print(mn)


if __name__ == '__main__':
    slove()

Submission status: Accepted

Solution code:
# ABC129D - Lamp
import sys
input = sys.stdin.readline

def main():
    H, W = tuple(map(int, input().split()))
    S = tuple(input().rstrip() for _ in range(H))
    L, R = [[0] * W for _ in range(H)], [[0] * W for _ in range(H)]
    U, D = [[0] * W for _ in range(H)], [[0] * W for _ in range(H)]
    for x1 in range(H):
        for y1 in range(W):
            if S[x1][y1] == ".":  # left and upper sides of S[i][j]
                U[x1][y1] = U[x1 - 1][y1] + 1
                L[x1][y1] = L[x1][y1 - 1] + 1
            x2, y2 = -x1 - 1, -y1 - 1
            if S[x2][y2] == ".":  # right and under sides
                D[x2][y2] = D[x2 + 1][y2] + 1
                R[x2][y2] = R[x2][y2 + 1] + 1
    ans = max(
        L[i][j] + R[i][j] + U[i][j] + D[i][j] - 3 for i in range(H) for j in range(W)
    )
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# ABC129D - Lamp
import sys
input = sys.stdin.readline

def main():
    H, W = tuple(map(int, input().split()))
    S = tuple(input().rstrip() for _ in range(H))
    L, R = [[0] * W for _ in range(H)], [[0] * W for _ in range(H)]
    U, D = [[0] * W for _ in range(H)], [[0] * W for _ in range(H)]
    for x1 in range(H):
        for y1 in range(W):
            if S[x1][y1] == ".":  # left and upper sides of S[i][j]
                U[x1][y1] = 1 if x1 == 0 else U[x1 - 1][y1] + 1
                L[x1][y1] = 1 if y1 == 0 else L[x1][y1 - 1] + 1
            x2, y2 = -x1 - 1, -y1 - 1
            if S[x2][y2] == ".":  # right and under sides
                D[x2][y2] = 1 if x2 == H - 1 else D[x2 + 1][y2] + 1
                R[x2][y2] = 1 if y2 == W - 1 else R[x2][y2 + 1] + 1
    ans = max(
        L[i][j] + R[i][j] + U[i][j] + D[i][j] - 3 for i in range(H) for j in range(W)
    )
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# ABC129D - Lamp
import sys
input = sys.stdin.readline

def main():
    H, W = tuple(map(int, input().split()))
    S = tuple(input().rstrip() for _ in range(H))
    L, R = [[0] * W for _ in range(H)], [[0] * W for _ in range(H)]
    U, D = [[0] * W for _ in range(H)], [[0] * W for _ in range(H)]
    for x1, x2 in zip(range(H), range(H - 1, -1, -1)):
        for y1, y2 in zip(range(W), range(W - 1, -1, -1)):
            if S[x1][y1] == ".":  # left and upper sides of S[i][j]
                U[x1][y1] = 1 if x1 == 0 else U[x1 - 1][y1] + 1
                L[x1][y1] = 1 if y1 == 0 else L[x1][y1 - 1] + 1

            if S[x2][y2] == ".":  # right and under sides
                D[x2][y2] = 1 if x2 == H - 1 else D[x2 + 1][y2] + 1
                R[x2][y2] = 1 if y2 == W - 1 else R[x2][y2 + 1] + 1
    ans = max(
        L[i][j] + R[i][j] + U[i][j] + D[i][j] - 3 for i in range(H) for j in range(W)
    )
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# ABC129D - Lamp
import sys
input = sys.stdin.readline

def main():
    H, W = tuple(map(int, input().split()))
    S = tuple(input().rstrip() for _ in range(H))
    L, R = [[0] * W for _ in range(H)], [[0] * W for _ in range(H)]
    U, D = [[0] * W for _ in range(H)], [[0] * W for _ in range(H)]
    for x1, x2 in zip(range(H), range(H - 1, -1, -1)):
        for y1, y2 in zip(range(W), range(W - 1, -1, -1)):
            if S[x1][y1] == "#":  # left and upper sides of S[i][j]
                L[x1][y1] == 0
                U[x1][y1] == 0
            else:
                U[x1][y1] = 1 if x1 == 0 else U[x1 - 1][y1] + 1
                L[x1][y1] = 1 if y1 == 0 else L[x1][y1 - 1] + 1

            if S[x2][y2] == "#":  # right and under sides
                R[x2][y2] == 0
                D[x2][y2] == 0
            else:
                D[x2][y2] = 1 if x2 == H - 1 else D[x2 + 1][y2] + 1
                R[x2][y2] = 1 if y2 == W - 1 else R[x2][y2 + 1] + 1
    ans = max(
        L[i][j] + R[i][j] + U[i][j] + D[i][j] - 3 for i in range(H) for j in range(W)
    )
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# ABC129D - Lamp
import sys

input = sys.stdin.readline


def check_light():
    L, R = [[0] * W for _ in range(H)], [[0] * W for _ in range(H)]
    U, D = [[0] * W for _ in range(H)], [[0] * W for _ in range(H)]
    for i in range(H):
        for j in range(W):
            if S[i][j] == "#":
                L[i][j] == 0
                U[i][j] == 0
            else:
                if j == 0:
                    L[i][j] = 1
                else:
                    L[i][j] = L[i][j - 1] + 1
                if i == 0:
                    U[i][j] = 1
                else:
                    U[i][j] = U[i - 1][j] + 1
            if T[i][j] == "#":
                R[i][j] == 0
                D[i][j] == 0
            else:
                if j == 0:
                    R[i][j] = 1
                else:
                    R[i][j] = R[i][j - 1] + 1
                if i == 0:
                    D[i][j] = 1
                else:
                    D[i][j] = D[i - 1][j] + 1
    return U, L, D, R


def main():
    global H, W, S, T
    H, W = tuple(map(int, input().split()))
    S = tuple(input().rstrip() for _ in range(H))
    T = [s[::-1] for s in S[::-1]]  # reversed S
    U, L, D, R = check_light()
    D, R = [d[::-1] for d in D[::-1]], [r[::-1] for r in R[::-1]]
    ans = max(
        L[i][j] + R[i][j] + U[i][j] + D[i][j] - 3 for i in range(H) for j in range(W)
    )
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# ABC129D - Lamp
import sys
input = sys.stdin.readline

def check_light(A):
    X, Y = [[0] * W for _ in range(H)], [[0] * W for _ in range(H)]
    for i in range(H):
        for j in range(W):
            if A[i][j] == "#":
                X[i][j] == 0
                Y[i][j] == 0
            else:
                if i == 0:
                    X[i][j] = 1
                else:
                    X[i][j] = X[i - 1][j] + 1
                if j == 0:
                    Y[i][j] = 1
                else:
                    Y[i][j] = Y[i][j - 1] + 1
    return X, Y

def main():
    global H, W
    H, W = tuple(map(int, input().split()))
    S = tuple(input().rstrip() for _ in range(H))
    RS = [s[::-1] for s in S[::-1]]  # reversed S
    U, L = check_light(S)
    D, R = check_light(RS)
    D, R = [d[::-1] for d in D[::-1]], [r[::-1] for r in R[::-1]]
    ans = max(
        L[i][j] + R[i][j] + U[i][j] + D[i][j] - 3 for i in range(H) for j in range(W)
    )
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
h,w=map(int,input().split())

s=[]
for i in range(h):
    s.append(list(input()))

l=[[0]*w for i in range(h)]
r=[[0]*w for i in range(h)]
u=[[0]*w for i in range(h)]
d=[[0]*w for i in range(h)]

for i in range(h):
    f=0
    for j in range(w):
        if s[i][j]=='#':
            f=0
            l[i][j]=0
        else:
            f+=1
            l[i][j]=f
            
for i in range(h):
    f=0
    for j in range(w-1,-1,-1):
        if s[i][j]=='#':
            f=0
            r[i][j]=0
        else:
            f+=1
            r[i][j]=f
            
for j in range(w):
    f=0
    for i in range(h):
        if s[i][j]=='#':
            f=0
            u[i][j]=0
        else:
            f+=1
            u[i][j]=f
    
for j in range(w):
    f=0
    for i in range(h-1,-1,-1):
        if s[i][j]=='#':
            f=0
            d[i][j]=0
        else:
            f+=1
            d[i][j]=f

ans=0
for i in range(h):
    for j in range(w):
        ans=max(ans,l[i][j]+r[i][j]+u[i][j]+d[i][j]-3)
print(ans)