Problem: 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.

Submission attempts (in order):
import sys
import numpy as np
H, W = map(int, input().split())
S = [line.strip("\n") for line in sys.stdin]


trailLeft = np.zeros((H, W), dtype=int)
trailRight = np.zeros((H, W), dtype=int)
trailUp = np.zeros((H, W), dtype=int)
trailDown = np.zeros((H, W), dtype=int)

for i in range(H):  
    for j in range(W):  
        if S[i][j] == '#':
            pass  
        else:
            if i == 0 and j == 0:  
                trailLeft[i][j] = 1
                trailUp[i][j] = 1
            elif i == 0:  
                trailLeft[i][j] = trailLeft[i][j - 1] + 1
                trailUp[i][j] = 1
            elif j == 0:  
                trailLeft[i][j] = 1
                trailUp[i][j] = trailUp[i - 1][j] + 1
            else:
                trailLeft[i][j] = trailLeft[i][j - 1] + 1
                trailUp[i][j] = trailUp[i - 1][j] + 1

        k = H - i - 1  # H-1 to 0
        l = W - j - 1  # W-1 to 0
        に見ていく
        if S[k][l] == "#":
            pass
        else:
            if k == H - 1 and l == W - 1:  
                trailRight[k][l] = 1
                trailDown[k][l] = 1
            elif k == H - 1:  
                trailRight[k][l] = trailRight[k][l + 1] + 1
                trailDown[k][l] = 1
            elif l == W - 1:  
                trailRight[k][l] = 1
                trailDown[k][l] = trailDown[k + 1][l] + 1
            else:
                trailRight[k][l] = trailRight[k][l + 1] + 1
                trailDown[k][l] = trailDown[k + 1][l] + 1

print(np.max(trailLeft + trailRight + trailUp + trailDown - 3))


import sys
H, W = map(int, input().split())
S = [line.strip("\n") for line in sys.stdin]


trailLeft = [[0] * W for _ in range(H)]
trailRight = [[0] * W for _ in range(H)]
trailUp = [[0] * W for _ in range(H)]
trailDown = [[0] * W for _ in range(H)]

for i in range(H):  
    for j in range(W):  
        if S[i][j] == '#':
            pass  
        else:
            if i == 0 and j == 0:  
                trailLeft[i][j] = 1
                trailUp[i][j] = 1
            elif i == 0:  
                trailLeft[i][j] = trailLeft[i][j - 1] + 1
                trailUp[i][j] = 1
            elif j == 0:  
                trailLeft[i][j] = 1
                trailUp[i][j] = trailUp[i - 1][j] + 1
            else:
                trailLeft[i][j] = trailLeft[i][j - 1] + 1
                trailUp[i][j] = trailUp[i - 1][j] + 1

        k = H - i - 1  # H-1 to 0
        l = W - j - 1  # W-1 to 0
        に見ていく
        if S[k][l] == "#":
            pass
        else:
            if k == H - 1 and l == W - 1:  
                trailRight[k][l] = 1
                trailDown[k][l] = 1
            elif k == H - 1:  
                trailRight[k][l] = trailRight[k][l + 1] + 1
                trailDown[k][l] = 1
            elif l == W - 1:  
                trailRight[k][l] = 1
                trailDown[k][l] = trailDown[k + 1][l] + 1
            else:
                trailRight[k][l] = trailRight[k][l + 1] + 1
                trailDown[k][l] = trailDown[k + 1][l] + 1

ans = 0
print(ans)  


import sys
H, W = map(int, input().split())
S = [line.strip("\n") for line in sys.stdin]


trailLeft = [[0] * W for _ in range(H)]
trailRight = [[0] * W for _ in range(H)]
trailUp = [[0] * W for _ in range(H)]
trailDown = [[0] * W for _ in range(H)]

for i in range(H):  
    for j in range(W):  
        if S[i][j] == '#':
            pass  
        else:
            if i == 0 and j == 0:  
                trailLeft[i][j] = 1
                trailUp[i][j] = 1
            elif i == 0:  
                trailLeft[i][j] = trailLeft[i][j - 1] + 1
                trailUp[i][j] = 1
            elif j == 0:  
                trailLeft[i][j] = 1
                trailUp[i][j] = trailUp[i - 1][j] + 1
            else:
                trailLeft[i][j] = trailLeft[i][j - 1] + 1
                trailUp[i][j] = trailUp[i - 1][j] + 1

        k = H - i - 1  # H-1 to 0
        l = W - j - 1  # W-1 to 0
        に見ていく
        if S[k][l] == "#":
            pass
        else:
            if k == H - 1 and l == W - 1:  
                trailRight[k][l] = 1
                trailDown[k][l] = 1
            elif k == H - 1:  
                trailRight[k][l] = trailRight[k][l + 1] + 1
                trailDown[k][l] = 1
            elif l == W - 1:  
                trailRight[k][l] = 1
                trailDown[k][l] = trailDown[k + 1][l] + 1
            else:
                trailRight[k][l] = trailRight[k][l + 1] + 1
                trailDown[k][l] = trailDown[k + 1][l] + 1

ans = 0
for i in range(H):  
    for j in range(W):  
        ans = max(ans, trailLeft[i][j] + trailRight[i][j] + trailUp[i][j] + trailDown[i][j] - 3)

print(ans)


import sys
import numpy as np
H, W = map(int, input().split())
S = []
for _ in range(H):
    S.append(list(input()))


trailLeft = np.zeros((H, W), dtype=int)
trailRight = np.zeros((H, W), dtype=int)
trailUp = np.zeros((H, W), dtype=int)
trailDown = np.zeros((H, W), dtype=int)

for i in range(H):  
    for j in range(W):  
        if S[i][j] == '#':
            pass  
        else:
            if i == 0 and j == 0:  
                trailLeft[i][j] = 1
                trailUp[i][j] = 1
            elif i == 0:  
                trailLeft[i][j] = trailLeft[i][j - 1] + 1
                trailUp[i][j] = 1
            elif j == 0:  
                trailLeft[i][j] = 1
                trailUp[i][j] = trailUp[i - 1][j] + 1
            else:
                trailLeft[i][j] = trailLeft[i][j - 1] + 1
                trailUp[i][j] = trailUp[i - 1][j] + 1

        k = H - i - 1  # H-1 to 0
        l = W - j - 1  # W-1 to 0
        に見ていく
        if S[k][l] == "#":
            pass
        else:
            if k == H - 1 and l == W - 1:  
                trailRight[k][l] = 1
                trailDown[k][l] = 1
            elif k == H - 1:  
                trailRight[k][l] = trailRight[k][l + 1] + 1
                trailDown[k][l] = 1
            elif l == W - 1:  
                trailRight[k][l] = 1
                trailDown[k][l] = trailDown[k + 1][l] + 1
            else:
                trailRight[k][l] = trailRight[k][l + 1] + 1
                trailDown[k][l] = trailDown[k + 1][l] + 1

print(np.max(trailLeft + trailRight + trailUp + trailDown - 3))


import sys
H, W = map(int, input().split())
S = [line.strip("\n") for line in sys.stdin]
transposeS = ["" for _ in range(W)]

rowRunLength = [[None] * W for _ in range(H)]
columnRunLength = [[None] * H for _ in range(W)]


for idx, elem in enumerate(S):
    previousObstacleIndex = -1

    while previousObstacleIndex != W:
        obstacleIndex = elem.find("#", previousObstacleIndex + 1)
        if obstacleIndex == -1:
            obstacleIndex = W
        else:
            rowRunLength[idx][obstacleIndex] = 0
        for lightenedIndex in range(previousObstacleIndex + 1, obstacleIndex):
            rowRunLength[idx][lightenedIndex] = obstacleIndex - previousObstacleIndex - 1
        previousObstacleIndex = obstacleIndex

for i in range(W):
    for j in range(H):
        transposeS[i] += S[j][i]

for idx, elem in enumerate(transposeS):
    print(elem)
    previousObstacleIndex = -1

    while previousObstacleIndex != H:
        obstacleIndex = elem.find("#", previousObstacleIndex + 1)
        if obstacleIndex == -1:
            obstacleIndex = H
        else:
            columnRunLength[idx][obstacleIndex] = 0
        for lightenedIndex in range(previousObstacleIndex + 1, obstacleIndex):
            columnRunLength[idx][lightenedIndex] = obstacleIndex - previousObstacleIndex - 1
        previousObstacleIndex = obstacleIndex

ans = 0
for i in range(H):
    for j in range(W):
        if S[i][j] == ".":
            ans = max(ans, rowRunLength[i][j] + columnRunLength[j][i] - 1)

print(ans)


# can't AC without numpy, Python array handling with for is SLOW
import numpy as np
H, W = map(int, input().split())
S = []
for _ in range(H):
    S.append(list(input()))
S = (np.array(S) == ".") * 1


trailLeft = np.zeros((H, W), dtype=int)
trailRight = np.zeros((H, W), dtype=int)
trailUp = np.zeros((H, W), dtype=int)
trailDown = np.zeros((H, W), dtype=int)

for i in range(H):
    if i == 0:
        trailUp[i] = S[i]
        trailDown[H - i - 1] = S[H - i - 1] * 1
    else:
        trailUp[i] = (trailUp[i - 1] + 1) * S[i]
        trailDown[H - i - 1] = (trailDown[H - i] + 1) * S[H - i - 1]

for j in range(W):
    if j == 0:
        trailLeft[:, j] = S[:, j]
        trailRight[:, W - j - 1] = S[:, W - j - 1] * 1
    else:
        trailLeft[:, j] = (trailLeft[:, j - 1] + 1) * S[:, j]
        trailRight[:, W - j - 1] = (trailRight[:, W - j] + 1) * S[:, W - j - 1]

print(np.max(trailLeft + trailRight + trailUp + trailDown - 3))


Verdicts: Time Limit Exceeded

Wrong Answer

Time Limit Exceeded

Time Limit Exceeded

Wrong Answer

Accepted