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: Time Limit Exceeded

Solution code:
h, w = map(int,input().split())
S = [list(input()) for i in range(h)]

S_l = [[0] * w for _ in range(h)]
S_r = [[0] * w for _ in range(h)]
S_u = [[0] * w for _ in range(h)]
S_d = [[0] * w for _ in range(h)]

for i in range(h):
    cnt = 0
    for j in range(w):
        if S[i][j] == '.':
            if cnt == 0:
                S_l[i][j] = 1
                cnt = 1
            else:
                S_l[i][j] = S_l[i][j-1] + 1
        else:
            cnt = 0
            S_l[i][j] = 0

for i in range(h):
    cnt = 0
    for j in reversed(range(w)):
        if S[i][j] == '.':
            if cnt == 0:
                S_r[i][j] = 1
                cnt = 1
            else:
                S_r[i][j] = S_r[i][j+1] + 1
        else:
            cnt = 0
            S_r[i][j] = 0

for j in range(w):
    cnt = 0
    for i in range(h):
        if S[i][j] == '.':
            if cnt == 0:
                S_u[i][j] = 1
                cnt = 1
            else:
                S_u[i][j] = S_u[i-1][j] + 1
        else:
            cnt = 0
            S_u[i][j] = 0

for j in range(w):
    cnt = 0
    for i in reversed(range(h)):
        if S[i][j] == '.':
            if cnt == 0:
                S_d[i][j] = 1
                cnt = 1
            else:
                S_d[i][j] = S_d[i+1][j] + 1
        else:
            cnt = 0
            S_d[i][j] = 0

ans = 0

for i in range(h):
    for j in range(w):
        ans = max(ans, S_l[i][j] + S_r[i][j] + S_u[i][j] + S_d[i][j] - 3)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
h, w = map(int,input().split())
S = [list(input()) for i in range(h)]

S_x = [[0] * w for _ in range(h)]

for i in range(h):
    cnt = 0
    start = 0
    for j in range(w):
        if S[i][j] == '.':
            if cnt == 0:
                start = j
            cnt += 1
            if j == w-1:
                for k in range(start, w):
                    S_x[i][k] = cnt
        else:
            for k in range(start, j):
                S_x[i][k] = cnt
            start = j
            S_x[i][j] = 0
            cnt = 0
        
S_t = [list(x) for x in zip(*S_x)]

ans = 0

for i in range(w):
    cnt = 0
    start = 0
    for j in range(h):
        if S_t[i][j] != 0:
            if cnt == 0:
                start = j
            cnt += 1
            if j == h-1:
                for k in range(start, h):
                    S_t[i][k] += cnt - 1
                    ans = max(ans, S_t[i][k])

        else:
            for k in range(start, j):
                S_t[i][k] += cnt - 1
                ans = max(ans, S_t[i][k])
            start = j
            S_t[i][j] = 0
            cnt = 0

print(ans)

Submission status: Time Limit Exceeded

Solution code:
h, w = map(int, input().rsplit())
S = [list(input().rstrip()) for _ in range(h)]
U = [[0] * w for _ in range(h)]
D = [[0] * w for _ in range(h)]
L = [[0] * w for _ in range(h)]
R = [[0] * w for _ in range(h)]
for i in range(h):
    for j in range(w):
        if j == 0:
            if S[i][j] == ".":
                L[i][j] = 1
            continue
        if S[i][j] == "#":
            continue
        else:
            L[i][j] += 1 + L[i][j - 1]
for i in range(h):
    for j in range(w):
        if i == 0:
            if S[i][j] == ".":
                U[i][j] = 1
            continue
        if S[i][j] == "#":
            continue
        else:
            U[i][j] += 1 + U[i - 1][j]
for i in range(h - 1, -1, -1):
    for j in range(w - 1, -1, -1):
        if j == w - 1:
            if S[i][j] == ".":
                R[i][j] = 1
            continue
        if S[i][j] == "#":
            continue
        else:
            R[i][j] += 1 + R[i][j + 1]
for i in range(h - 1, -1, -1):
    for j in range(w - 1, -1, -1):
        if i == h - 1:
            if S[i][j] == ".":
                D[i][j] = 1
            continue
        if S[i][j] == "#":
            continue
        else:
            D[i][j] += 1 + D[i + 1][j]
ans = 0
for i in range(h):
    for j in range(w):
        temp = D[i][j] + U[i][j] + L[i][j] + R[i][j] - 3
        ans = max(temp, ans)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys

sys.setrecursionlimit(10 ** 6)
INF = float("inf")
MOD = 10 ** 9 + 7


def input():
    return sys.stdin.readline().strip()


def main():
    H, W = map(int, input().split())
    maze = [input() for _ in range(H)]

    left = [[0] * W for _ in range(H)]
    right = [[0] * W for _ in range(H)]
    up = [[0] * W for _ in range(H)]
    down = [[0] * W for _ in range(H)]

    for h in range(H):
        cnt = 0
        for w in range(W):
            if maze[h][w] == "#":
                cnt = 0
            else:
                cnt += 1
                left[h][w] = cnt

    for h in range(H):
        cnt = 0
        for w in range(W - 1, -1, -1):
            if maze[h][w] == "#":
                cnt = 0
            else:
                cnt += 1
                right[h][w] = cnt

    for w in range(W):
        cnt = 0
        for h in range(H):
            if maze[h][w] == "#":
                cnt = 0
            else:
                cnt += 1
                up[h][w] = cnt

    for w in range(W):
        cnt = 0
        for h in range(H - 1, -1, -1):
            if maze[h][w] == "#":
                cnt = 0
            else:
                cnt += 1
                down[h][w] = cnt

    ans = 0
    for h in range(H):
        for w in range(W):
            tmp = up[h][w] + down[h][w] + left[h][w] + right[h][w] - 3
            if tmp > ans:
                ans = tmp

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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

left = [[0] * W for _ in range(H)]
right = [[0] * W for _ in range(H)]
up = [[0] * W for _ in range(H)]
down = [[0] * W for _ in range(H)]

for h in range(H):
    cnt = 0
    for w in range(W):
        if maze[h][w] == "#":
            cnt = 0
        else:
            cnt += 1
            left[h][w] = cnt

for h in range(H):
    cnt = 0
    for w in range(W - 1, -1, -1):
        if maze[h][w] == "#":
            cnt = 0
        else:
            cnt += 1
            right[h][w] = cnt

for w in range(W):
    cnt = 0
    for h in range(H):
        if maze[h][w] == "#":
            cnt = 0
        else:
            cnt += 1
            up[h][w] = cnt

for w in range(W):
    cnt = 0
    for h in range(H - 1, -1, -1):
        if maze[h][w] == "#":
            cnt = 0
        else:
            cnt += 1
            down[h][w] = cnt

ans = 0
for h in range(H):
    for w in range(W):
        tmp = up[h][w] + down[h][w] + left[h][w] + right[h][w] - 3
        if tmp > ans:
            ans = tmp

print(ans)

Submission status: Time Limit Exceeded

Solution code:
H, W = map(int,input().split())
s = [list(input()) for i in range(H)]
sl = [[0]*(W) for _ in range(H)]
sr = [[0]*(W) for _ in range(H)]
su = [[0]*(W) for _ in range(H)]
sd = [[0]*(W) for _ in range(H)]

for i in range(H):
    for j in range(W):
        if s[i][j]=='.':
            if i==0:
                sl[i][j] = 1
            else:
                sl[i][j] = sl[i-1][j]+1
            if j==0:
                su[i][j]=1
            else:
                su[i][j] = su[i][j-1]+1
for i in list(range(H))[::-1]:
    for j in list(range(W))[::-1]:
        if s[i][j]=='.':
            if i==H-1:
                sr[i][j] = 1
            else:
                sr[i][j] = sr[i+1][j]+1
            if j==W-1:
                sd[i][j]=1
            else:
                sd[i][j] = sd[i][j+1]+1
ans = 0
for i in range(H):
    for j in range(W):
        if s[i][j]=='.':
            ans = max(ans, sl[i][j]+sr[i][j]+su[i][j]+sd[i][j]-3)
print(ans)

Submission status: Time Limit Exceeded

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

alr = [[["#", "#"] for j in range(W)] for i in range(H)]
cnt_max = 0
def dfs(i, j):
    global cnt_max
    if alr[i][j][0] != "#":
        hor = alr[i][j][0]
    else:
        w, e = 0, 0
        while field[i][j+w] == ".": #西
            w += 1
            if j+w == W:
                break
        while field[i][j-e] == ".": #東
            e += 1
            if j-e == -1:
                break
        for k in range(j-e+1, j+w-1):
            alr[i][k][0] = w+e-1
        hor = w + e -1
    if alr[i][j][1] != "#":
        ver = alr[i][j][1]
    else:
        n, s = 0, 0
        while field[i-n][j] == ".": #北
            n += 1
            if i-n == -1:
                break
        while field[i+s][j] == ".": #北
            s += 1
            if i+s == H:
                break
        for k in range(i-n+1, i+s-1):
            alr[k][j][1] = n+s-1
        ver = n + s - 1
    cnt = hor + ver - 1
    cnt_max = max(cnt, cnt_max)

for i in range(H):
    for j in range(W):
        if field[i][j] == ".":
            dfs(i, j)
print(cnt_max)

Submission status: Time Limit Exceeded

Solution code:
def main():
    import bisect
    h,w = map(int,input().split())
    x = {}
    y = {}
    for i in range(h):
        x[i]=[-1]
        s = input()
        for j in range(w):
            if i==0:
                y[j]=[-1]
            if s[j]=='#':
                x[i] += [j]
                y[j] += [i]
            if i==h-1:
                y[j].append(h)
        x[i]+=[w]
    m = 0
    for i in range(h):
        for j in range(w):
            if j in x[i]:
                continue
            r = bisect.bisect(x[i],j)
            c = bisect.bisect(y[j],i)
            cnt = x[i][r]-x[i][r-1]-1
            cnt += y[j][c]-y[j][c-1]-1-1
            if cnt > m:
                m = cnt
    print(m)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
H, W = map(int, input().split())
Map = ["#" + input() + "#" for i in range(H)]
Map = ["#"*(W+2)] + Map[:] + ["#"*(W+2)]
A = [[0 for _ in range(W)] for __ in range(H)]
hl = [[] for _ in range(H)]
wl = [[] for _ in range(W)]
nhl = [0 for _ in range(H)]
nwl = [0 for _ in range(W)]
l = -1
r = -1
for i in range(H+2):
  n = 0
  for j in range(W+2):
    if Map[i][j] == '.' and l == -1:
      l = j - 1
      n += 1
    elif Map[i][j] == '#' and l != -1:
      r = j - 1
      hl[i-1].append(l*(W+1)+r)
      l = -1
  if 0 < i < H + 1:
    nhl[i-1] = n
l = -1
for j in range(W+2):
  n = 0
  for i in range(H+2):
    if Map[i][j] == '.' and l == -1:
      l = i - 1
      n += 1
    elif Map[i][j] == '#' and l != -1:
      r = i - 1
      wl[j-1].append(l*(H+1)+r)
      l = -1
  if 0 < j < W + 1:
    nwl[j-1] = n
for i in range(W):
  for j in range(nwl[i]):
    s = wl[i][j]
    d = s % (H+1) - s // (H+1)
    for k in range(s // (H+1), s % (H+1)):
      A[k][i] = d
m = 0
for i in range(H):
  for j in range(nhl[i]):
    s = hl[i][j]
    d = s % (W+1) - s // (W+1)
    for k in range(s // (W+1), s % (W+1)):
      if m < A[i][k] + d - 1:
        m = A[i][k] + d - 1
print(m)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
def main():
    h, w = map(int, input().split())
    s = np.array([list(input()) for _ in range(h)])
    t = np.zeros((h+2, w+2))
    t[1:-1, 1:-1][s == "."] = 1
    row = np.zeros((h+2, w+2))
    col = row.T.copy()
    for i in range(1, h+1):
        idx = np.where(t[i] == 0)
        dif = np.diff(idx[0])
        for j in range(np.size(idx) - 1):
            L, R = idx[0][j], idx[0][j+1]
            row[i][L+1] += dif[j]-1
            row[i][R] -= dif[j]-1
        row[i] = np.cumsum(row[i])
    t = t.T
    for i in range(1, w+1):
        idx = np.where(t[i] == 0)
        dif = np.diff(idx[0])
        for j in range(np.size(idx) - 1):
            L, R = idx[0][j], idx[0][j+1]
            col[i][L+1] += dif[j]-2
            col[i][R] -= dif[j]-2
        col[i] = np.cumsum(col[i])
    col = col.T
#    for i in range(h+2):
#        print(row[i])
#    print("")
#    for i in range(h+2):
#        print(col[i])
    cost = np.add(row, col)
    print(int(np.max(cost)))

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
def main():
    h, w = map(int, input().split())
    s = np.array([list(input()) for _ in range(h)])
    t = np.zeros((h+2, w+2))
    t[1:-1, 1:-1][s == "."] = 1
    cost = np.zeros((h+2, w+2))
    for i in range(1, h+1):
        idx = np.where(t[i] == 0)
        for j in range(np.size(idx) - 1):
            L, R = idx[0][j], idx[0][j+1]
            cost[i][L+1:R] = np.add(cost[i][L+1:R], R-L-1)
    cost = cost.T
    t = t.T
    for i in range(1, w+1):
        idx = np.where(t[i] == 0)
        for j in range(np.size(idx) - 1):
            L, R = idx[0][j], idx[0][j+1]
            cost[i][L+1:R] = np.add(cost[i][L+1:R], R-L-1-1)
    print(int(np.max(cost)))

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
H, W = map(int, input().split())
lab = ["" for _ in range(H)]
L = [[0 for _ in range(W+2)] for _ in range(H+2)]
R = [[0 for _ in range(W+2)] for _ in range(H+2)]
D = [[0 for _ in range(W+2)] for _ in range(H+2)]
U = [[0 for _ in range(W+2)] for _ in range(H+2)]
for h in range(H):
    lab[h] = input()
for h in range(H):
    for w in range(W):
        if lab[h][w] == ".":
            L[h+1][w+1] = L[h+1][w] + 1
            U[h+1][w+1] = U[h][w+1] + 1
for h in range(H):
    for w in range(W):
        if lab[H-h-1][W-w-1] == ".":
            R[H-h][W-w] = R[H-h][W-w+1] + 1
            D[H-h][W-w] = D[H-h+1][W-w] + 1
m = 0
for h in range(H):
    for w in range(W):
        m = max(m, L[h+1][w+1] + R[h+1][w+1] + D[h+1][w+1] + U[h+1][w+1] - 3)
print(m)

Submission status: Time Limit Exceeded

Solution code:
H, W = map(int, input().split())
lab = ["" for _ in range(H)]
vir = [[0 for _ in range(W+2)] for _ in range(H+2)]
hor = [[0 for _ in range(W+2)] for _ in range(H+2)]
for h in range(H):
    lab[h] = input()
for h in range(H):
    for w in range(W):
        if lab[h][w] == ".":
            if vir[h][w+1] > 0:
                vir[h+1][w+1] = vir[h][w+1]
            else:
                for h2 in range(h, H):
                    if lab[h2][w] == '.':
                        vir[h+1][w+1] += 1
                    else:
                        break
            if hor[h+1][w] > 0:
                hor[h+1][w+1] = hor[h+1][w]
            else:
                for w2 in range(w, W):
                    if lab[h][w2] == '.':
                        hor[h+1][w+1] += 1
                    else:
                        break
m = 0
for h in range(H):
    for w in range(W):
        m = max(m, vir[h+1][w+1] + hor[h+1][w+1] - 1)
print(m)

Submission status: Time Limit Exceeded

Solution code:
def main():
    h, w = map(int, input().split())
    s = [""]*(h+2)
    for j in range(w+2):
        s[0] += "#"
        s[h+1] += "#"
    for i in range(1, h+1):
        s[i] = "#" + input() + "#"
    cost = [[[0]*4 for j in range(w+2)] for i in range(h+2)]
    for i in range(1, h+1):
        for j in range(1, w+1):
            if s[i][j] == ".":
                cost[i][j][0] = 1
                cost[i][j][2] = 1
                cost[i][j][0] += cost[i-1][j][0]
                cost[i][j][2] += cost[i][j-1][2]
            if s[h+1-i][w+1-j] == ".":
                cost[h+1-i][w+1-j][1] = 1
                cost[h+1-i][w+1-j][3] = 1
                cost[h+1-i][w+1-j][1] += cost[h+1-i+1][w+1-j][1]
                cost[h+1-i][w+1-j][3] += cost[h+1-i][w+1-j+1][3]
    ans = 0
    for i in range(1, h+1):
        for j in range(1, w+1):
            if s[i][j] == ".":
                tmp = cost[i-1][j][0] + cost[i+1][j][1] + cost[i][j-1][2] + cost[i][j+1][3] + 1
                ans = ans if ans > tmp else tmp
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    h,w = map(int,input().split())
    S = []
    for i in range(h):
        s = input()
        S.append(s)
    m = 0
    for i in range(h):
        for j in range(w):
            if S[i][j] == '.':
                l,r = j-1,j+1
                u,d = i-1,i+1
                cnt = 1
                while l>-1 and S[i][l]=='.':
                    l -= 1
                    cnt += 1
                while r<w and S[i][r]=='.':
                    r += 1
                    cnt += 1
                while u>-1 and S[u][j]=='.':
                    u -= 1
                    cnt += 1
                while d<h and S[d][j]=='.':
                    d += 1
                    cnt += 1
                if cnt > m:
                    m = cnt
    print(m)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
input=sys.stdin.readline

from functools import lru_cache
from collections import deque
@lru_cache()
def dfs(i,j):
    q = deque()
    q.append((i, j, 0))

    d = [(1, 0), (0, 1), (-1, 0), (0, -1)]
    cnt = 1

    while q:
        x, y, num = q.pop()
        if num == 0:
            for i in range(1,5):
                dx, dy = d[i-1]
                nx = x + dx
                ny = y + dy
                if not(0 <= nx < h and 0 <= ny < w):continue
                if s[nx][ny] == '#':continue
                q.append((nx, ny, i))
                cnt += 1

        else:
            nx = x + d[num-1][0]
            ny = y + d[num-1][1]
            if not(0 <= nx < h and 0 <= ny < w):continue
            if s[nx][ny] == '#':continue
            q.append((nx, ny, num))
            cnt += 1

    return cnt

h,w = map(int, input().split())
s = [list(input()) for i in range(h)]

ans = 0
for i in range(h):
    for j in range(w):
        if ans == h + w -1:
            print(ans)
            exit()

        if s[i][j] == '#':continue
        temp = dfs(i, j)
        #print(temp,(i,j))
        if temp > ans:ans = temp

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
def dfs(i,j):
    q = deque()
    q.append((i, j, 0))

    d = [(1, 0), (0, 1), (-1, 0), (0, -1)]
    cnt = 1

    while q:
        x, y, num = q.pop()
        if num == 0:
            for i in range(1,5):
                dx, dy = d[i-1]
                nx = x + dx
                ny = y + dy
                if not(0 <= nx < h and 0 <= ny < w):continue
                if s[nx][ny] == '#':continue
                q.append((nx, ny, i))
                cnt += 1

        else:
            nx = x + d[num-1][0]
            ny = y + d[num-1][1]
            if not(0 <= nx < h and 0 <= ny < w):continue
            if s[nx][ny] == '#':continue
            q.append((nx, ny, num))
            cnt += 1

    return cnt

h,w = map(int, input().split())
s = [list(input()) for i in range(h)]

ans = 0
for i in range(h):
    for j in range(w):
        if ans == h + w -1:
            print(ans)
            exit()
            
        if s[i][j] == '#':continue
        temp = dfs(i, j)
        #print(temp,(i,j))
        if temp > ans:ans = temp

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
def dfs(i,j):
    q = deque()
    q.append((i, j, 0))

    d = [(1, 0), (0, 1), (-1, 0), (0, -1)]
    cnt = 1

    while q:
        x, y, num = q.pop()
        if num == 0:
            for i in range(1,5):
                dx, dy = d[i-1]
                nx = x + dx
                ny = y + dy
                if not(0 <= nx < h and 0 <= ny < w):continue
                if s[nx][ny] == '#':continue
                q.append((nx, ny, i))
                cnt += 1

        else:
            nx = x + d[num-1][0]
            ny = y + d[num-1][1]
            if not(0 <= nx < h and 0 <= ny < w):continue
            if s[nx][ny] == '#':continue
            q.append((nx, ny, num))
            cnt += 1

    return cnt

h,w = map(int, input().split())
s = [list(input()) for i in range(h)]

ans = 0
for i in range(h):
    for j in range(w):
        if s[i][j] == '#':continue
        temp = dfs(i, j)
        #print(temp,(i,j))
        if temp > ans:ans = temp
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

from copy import deepcopy

h, w = map(int, readline().split())
s = [input() for _ in range(h)]
cnt_x = [[0] * w for _ in range(h)]
cnt_y = deepcopy(cnt_x)
for i in range(h):
    memo = 0
    for j in range(w):
        if s[i][j] == '.':
            memo += 1
            cnt_x[i][j] = memo
        else:
            memo = 0
    for j in range(w - 1, 0, -1):
        if s[i][j - 1] == '.':
            cnt_x[i][j - 1] = max(cnt_x[i][j - 1], cnt_x[i][j])
for i in range(w):
    memo = 0
    for j in range(h):
        if s[j][i] == '.':
            memo += 1
            cnt_y[j][i] = memo
        else:
            memo = 0
    for j in range(h - 1, 0, -1):
        if s[j - 1][i] == '.':
            cnt_y[j - 1][i] = max(cnt_y[j - 1][i], cnt_y[j][i])
ans = 0
for i in range(h):
    for j in range(w):
        ans = max(ans, cnt_x[i][j] + cnt_y[i][j] - 1)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
h, w = map(lambda x: int(x)+2, input().split())
MAZE = ['#'*w]
for _ in range(h-2):
    MAZE.append('#'+input()+'#')
else:
    MAZE.append('#'*w)

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)]

LAMP = [[0]*w for _ in range(h)]

for i in range(1, h-1):
    for j in range(1, w-1):
        if MAZE[i][j] == '.':
            L[i][j+1] = L[i][j]+1
            U[i+1][j] = U[i][j]+1
        if MAZE[i][w-j] == '.':
            R[i][w-j-1] = R[i][w-j]+1
        if MAZE[h-i][j] == '.':
            D[h-i-1][j] = D[h-i][j]+1

ans = 0
for i in range(1, h-1):
    for j in range(1, w-1):
        if MAZE[i][j] == '#':
            continue
        LAMP[i][j] = L[i][j]+R[i][j]+U[i][j]+D[i][j]+1
        ans = max(ans, LAMP[i][j])

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
h, w = map(lambda x: int(x)+2, input().split())
MAZE = ['#'*w]
for _ in range(h-2):
    MAZE.append('#'+input()+'#')
else:
    MAZE.append('#'*w)

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)]

LAMP = [[0]*w for _ in range(h)]

for i in range(1, h-1):
    for j in range(1, w-1):
        if MAZE[i][j] == '.':
            L[i][j+1] = L[i][j]+1
        if MAZE[i][w-j] == '.':
            R[i][w-j-1] = R[i][w-j]+1

for j in range(1, w-1):
    for i in range(1, h-1):
        if MAZE[i][j] == '.':
            U[i+1][j] = U[i][j]+1
        if MAZE[h-i][j] == '.':
            D[h-i-1][j] = D[h-i][j]+1

ans = 0
for i in range(1, h-1):
    for j in range(1, w-1):
        if MAZE[i][j] == '#':
            continue
        LAMP[i][j] = L[i][j]+R[i][j]+U[i][j]+D[i][j]+1
        ans = max(ans, LAMP[i][j])

print(ans)

Submission status: Time Limit Exceeded

Solution code:
h, w = map(lambda x: int(x)+2, input().split())
MAZE = ['#'*w]
for _ in range(h-2):
    MAZE.append('#'+input()+'#')
else:
    MAZE.append('#'*w)

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)]

LAMP = [[0]*w for _ in range(h)]

for i in range(1, h-1):
    for j in range(1, w-1):
        if MAZE[i][j] == '#':
            continue
        L[i][j+1] = L[i][j]+1

for i in range(1, h-1):
    for j in reversed(range(1, w-1)):
        if MAZE[i][j] == '#':
            continue
        R[i][j-1] = R[i][j]+1

for j in range(1, w-1):
    for i in range(1, h-1):
        if MAZE[i][j] == '#':
            continue
        U[i+1][j] = U[i][j]+1

for j in range(1, w-1):
    for i in reversed(range(1, h-1)):
        if MAZE[i][j] == '#':
            continue
        D[i-1][j] = D[i][j]+1

ans = 0
for i in range(1, h-1):
    for j in range(1, w-1):
        if MAZE[i][j] == '#':
            continue
        LAMP[i][j] = L[i][j]+R[i][j]+U[i][j]+D[i][j]+1
        ans = max(ans, LAMP[i][j])

print(ans)

Submission status: Time Limit Exceeded

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

Left = [[0] * W for _ in range(H)]
for y in range(H):
    count = 0
    for x in range(W):
        if S[y][x] == '#':
            count = 0
        else:
            count += 1
        Left[y][x] = count

Right = [[0] * W for _ in range(H)]
for y in range(H):
    count = 0
    for x in range(W-1, -1, -1):
        if S[y][x] == '#':
            count = 0
        else:
            count += 1
        Right[y][x] = count

Up = [[0] * W for _ in range(H)]
for x in range(W):
    count = 0
    for y in range(H):
        if S[y][x] == '#':
            count = 0
        else:
            count += 1
        Up[y][x] = count

Down = [[0] * W for _ in range(H)]
for x in range(W):
    count = 0
    for y in range(H-1, -1, -1):
        if S[y][x] == '#':
            count = 0
        else:
            count += 1
        Down[y][x] = count

ans = 0
for i in range(H):
    for j in range(W):
        if S[i][j] == '#':
            continue
        else:
            res = Left[i][j] + Right[i][j] + Up[i][j] + Down[i][j] - 3
            ans = max(ans, res)

print(ans)

Submission status: Time Limit Exceeded

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

ans = 0
for i in range(H):
    for j in range(W):
        if S[i][j] == '.':
            res = 0
            count = 0
            for x in range(W):
                if S[i][x] == '.':
                    count += 1
                elif x < j:
                    count = 0
                elif x > j:
                    break
            res += count
            count = 0
            for y in range(H):
                if y == i:
                    continue
                elif S[y][j] == '.':
                    count += 1
                elif y < i:
                    count = 0
                elif y > i:
                    break
            res += count
            ans = max(ans, res)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
h,w=map(int,input().split())
s=[list(input()) for i in range(h)]
a=[[[0,0] for i in range(w)] for j in range(h)]
for i in range(h):
    k=0
    while k<w:
        cnt=0
        if s[i][k]==".":
            j=k
            while k<w and s[i][k]==".":
                k+=1
                cnt+=1
            for x in range(j,k):
                a[i][x][0]=cnt
        else:
            k+=1
for i in range(w):
    k=0
    while k<h:
        cnt=0
        if s[k][i]==".":
            j=k
            while k<h and s[k][i]==".":
                k+=1
                cnt+=1
            for x in range(j,k):
                a[x][i][1]=cnt
        else:
            k+=1
ans=0
for i in range(h):
    for j in range(w):
        ans=max(ans,a[i][j][0]+a[i][j][1])
print(ans-1)

Submission status: Time Limit Exceeded

Solution code:
h,w=map(int,input().split())
s=[list(input()) for i in range(h)]
a=[[[0,0] for i in range(w)] for j in range(h)]
for i in range(h):
    k=0
    while k<w:
        cnt=0
        if s[i][k]==".":
            j=k
            while k<w and s[i][k]=="." and a[i][k][0]==0:
                k+=1
                cnt+=1
            for x in range(j,k):
                a[i][x][0]=cnt
        else:
            k+=1
for i in range(w):
    k=0
    while k<h:
        cnt=0
        if s[k][i]==".":
            j=k
            while k<h and s[k][i]=="." and a[k][i][1]==0:
                k+=1
                cnt+=1
            for x in range(j,k):
                a[x][i][1]=cnt
        else:
            k+=1
ans=0
for i in range(h):
    for j in range(w):
        ans=max(ans,a[i][j][0]+a[i][j][1])
print(ans-1)

Submission status: Time Limit Exceeded

Solution code:
h,w=map(int,input().split())
s=[list(input()) for i in range(h)]
a=[[[0,0] for i in range(w)] for j in range(h)]
for i in range(h):
    k=0
    for j in range(w):
        cnt=0
        k=j
        while k<w and s[i][k]=="." and a[i][k][0]==0:
            k+=1
            cnt+=1
        for x in range(j,k):
            a[i][x][0]=cnt

for i in range(w):
    k=0
    for j in range(h):
        cnt=0
        k=j
        while k<h and s[k][i]=="." and a[k][i][1]==0:
            k+=1
            cnt+=1
        for x in range(j,k):
            a[x][i][1]=cnt
ans=0
for i in range(h):
    for j in range(w):
        ans=max(ans,a[i][j][0]+a[i][j][1])
print(ans-1)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
import sys
import numpy as np

def solve(H: int, W: int, S: "List[str]"):
    S = np.array(S)
    s = np.ones((H+2, W+2),dtype=np.int)
    s[1:-1,1:-1] = S
    sh = np.ravel(s)
    sw = np.ravel(s.T)

    def lamp(a):
        left = np.arange(len(a))
        left[a==0] = 0
        left = np.maximum.accumulate(left)

        right = np.arange(len(a))
        right[a[::-1]==0] = 0
        right = np.maximum.accumulate(right)
        right = len(a) - right[::-1]
        k = (right-left) - 2
        k[a==1] =0

        return k

    
    s = lamp(sh)+ np.ravel(lamp(sw).reshape(W+2,H+2).T) 

    print(np.max(s)-1)

    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    H = int(next(tokens))  # type: int
    W = int(next(tokens))  # type: int
    S = [[ int(_.replace(".", "0").replace("#", "1")) for _ in next(tokens)] for _ in range(H)]  # type: "List[str]"
    solve(H, W, S)

if __name__ == '__main__':
    main()