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:
import sys
import time
from collections import deque, Counter, defaultdict
#from fractions import gcd
#import bisect
#import heapq
import math
start=time.time()
input = sys.stdin.readline
sys.setrecursionlimit(10**8)
inf = 10**18
MOD = 1000000007
ri = lambda : int(input())
rs = lambda : input().strip()
rl = lambda : list(map(int, input().split()))
h,w = rl()
s = [list(rs()) for i in range(h)]
u=[[0]*w for i in range(h)]
b=[[0]*w for i in range(h)]
l=[[0]*w for i in range(h)]
r=[[0]*w for i in range(h)]

#縦方向
for i in range(w):
    if s[0][i]==".":
        u[0][i]=1
for i in range(w):
    for j in range(1,h):
        if s[j][i]==".":
            u[j][i]=u[j-1][i]+1
for i in range(w):
    if s[-1][i]==".":
        b[-1][i]=1
for i in range(w):
    for j in range(1,h):
        if s[-j-1][i]==".":
            b[-j-1][i]=b[-j][i]+1            
            

#横方向
for i in range(h):
    if s[i][0]==".":
        l[i][0]=1
for i in range(1,w):
    for j in range(h):
        if s[j][i]==".":
            l[j][i]=l[j][i-1]+1
for i in range(h):
    if s[i][-1]==".":
        r[i][-1]=1
for i in range(1,w):
    for j in range(h):
        if s[j][-i-1]==".":
            r[j][-i-1]=r[j][-i]+1
ans=0
for i in range(w):
    for j in range(h):
        ans=max(u[j][i]+b[j][i]+l[j][i]+r[j][i]-3, ans)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
def s(): return input()
def i(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10**9)
mod = 10**9+7

H,W = I()
maze = [list(s()) for _ in range(H)]
ansy = [[0]*W for _ in range(H)]
ans = 0
for i in range(H):
    for j in range(W):
        if maze[i][j] == '#':
            continue
        h,w = i,j
        for k in range(i): 
            h -= 1
            if maze[h][w] == '#':
                break
            ansy[i][j] += 1
        h,w = i,j
        for k in range(j): 
            w -= 1
            if maze[h][w] == '#':
                break
            ansy[i][j] += 1
        h,w = i,j
        for k in range(H-i-1): 
            h += 1
            if maze[h][w] == '#':
                break
            ansy[i][j] += 1
        h,w = i,j
        for k in range(W-j-1): 
            w += 1
            if maze[h][w] == '#':
                break
            ansy[i][j] += 1
    ans = max(ans,max(ansy[i]))
print(ans+1)

Submission status: Time Limit Exceeded

Solution code:
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
def s(): return input()
def i(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10**9)
mod = 10**9+7

H,W = I()
maze = [list(s()) for _ in range(H)]
ansy = [[0]*W for _ in range(H)]
ans = 0
for i in range(H):
    for j in range(W):
        if maze[i][j] == '#':
            continue
        que = deque([[i,j]])
        while que:
            h,w = que.pop()
            nh = h+1
            nw = w
            if nh < 0 or nw < 0 or nh >= H or nw >= W:
                continue
            if maze[nh][nw] == '#':
                continue
            ansy[i][j] += 1
            que.appendleft([nh,nw])
        que = deque([[i,j]])
        while que:
            h,w = que.pop()
            nh = h
            nw = w+1
            if nh < 0 or nw < 0 or nh >= H or nw >= W:
                continue
            if maze[nh][nw] == '#':
                continue
            ansy[i][j] += 1
            que.appendleft([nh,nw])
        que = deque([[i,j]])
        while que:
            h,w = que.pop()
            nh = h-1
            nw = w
            if nh < 0 or nw < 0 or nh >= H or nw >= W:
                continue
            if maze[nh][nw] == '#':
                continue
            ansy[i][j] += 1
            que.appendleft([nh,nw])
        que = deque([[i,j]])
        while que:
            h,w = que.pop()
            nh = h
            nw = w-1
            if nh < 0 or nw < 0 or nh >= H or nw >= W:
                continue
            if maze[nh][nw] == '#':
                continue
            ansy[i][j] += 1
            que.appendleft([nh,nw])
    ans = max(ans,max(ansy[i]))
print(ans+1)

Submission status: Time Limit Exceeded

Solution code:
def solver(H, W, S, i, j):
    if S[j][i] == "#":
        return 0

    cnt = 0
    # 左方向
    for x in range(i - 1, -1, -1):
        if S[j][x] == ".":
            cnt += 1
        else:
            break

    # 右方向
    for x in range(i + 1, W):
        if S[j][x] == ".":
            cnt += 1
        else:
            break

    # 上方向
    for x in range(j - 1, -1, -1):
        if S[x][i] == ".":
            cnt += 1
        else:
            break

    # 下方向
    for x in range(j + 1, H):
        if S[x][i] == ".":
            cnt += 1
        else:
            break

    # 足元分を足す
    return cnt + 1


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

    c_max = 0
    for i in range(W):
        for j in range(H):
            c = solver(H, W, S, i, j)
            if c > c_max:
                c_max = c

    print(c_max)


run()

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
from numba import njit


@njit("i2(i2, i2, i2[:,:])")
def solve(H, W, field):
    u = field.copy()
    d = field.copy()
    l = field.copy()
    r = field.copy()

    for i in range(1, H):
        u[i] = (u[i - 1] + 1) * u[i]
        d[-i - 1] = (d[-i] + 1) * d[-i - 1]

    for j in range(1, W):
        l[:, j] = (l[:, j - 1] + 1) * l[:, j]
        r[:, -j - 1] = (r[:, -j] + 1) * r[:, -j - 1]

    return (u + d + l + r - 3).max()


H, W = map(int, input().split())
field = [[c == "." for c in input()] for _ in range(H)]
field = np.array(field, dtype=np.int16)

print(solve(H, W, field))

Submission status: Time Limit Exceeded

Solution code:
from copy import deepcopy

import numpy as np


H, W = map(int, input().split())
field = np.array([[c == "." for c in input()] for _ in range(H)], dtype=np.int16)

u = deepcopy(field)
d = deepcopy(field)
l = deepcopy(field)
r = deepcopy(field)

for i in range(H):
    for j in range(1, W):
        if field[i, j]:
            l[i, j] = l[i, j - 1] + 1
        if field[i, W - j - 1]:
            r[i, W - j - 1] = r[i, W - j] + 1

for j in range(W):
    for i in range(1, H):
        if field[i, j]:
            u[i, j] = u[i - 1, j] + 1
        if field[H - i - 1, j]:
            d[H - i - 1, j] = d[H - i, j] + 1

print((u + d + l + r - 3).max())

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
h, w = map(int, input().split())
S = [list(input()) for _ in range(h)]
L = np.zeros((h, w), dtype=int)
for i in range(h):
    for j in range(w):
        if j == 0 and S[i][j] != '#':
            L[i, j] = 1
        elif S[i][j] != '#':
            L[i, j] = L[i, j-1] + 1
R = np.zeros((h, w), dtype=int)
for i in range(h):
    for j in range(w-1, -1, -1):
        if j == w-1 and S[i][j] != '#':
            R[i, j] = 1
        elif S[i][j] != '#':
            R[i, j] = R[i, j+1] + 1
U = np.zeros((h, w), dtype=int)
for j in range(w):
    for i in range(h):
        if i == 0 and S[i][j] != '#':
            U[i, j] = 1
        elif S[i][j] != '#':
            U[i, j] = U[i-1, j] + 1
D = np.zeros((h, w), dtype=int)
for j in range(w):
    for i in range(h-1, -1, -1):
        if i == h-1 and S[i][j] != '#':
            D[i, j] = 1
        elif S[i][j] != '#':
            D[i, j] = D[i+1, j] + 1
ans = -1
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)

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10**6)

h,w = map(int,input().split())

s = [input() for _ in range(h)]
cnt = [[0] * w for _ in range(h)]

for i in range(h):
    done = [0] * w

    for j in range(w):
        if s[i][j] == '#': continue
        if done[j]: continue

        l = 0
        while j+l < w:
            if s[i][j+l] == '#': break
            l += 1

        for k in range(l):
            cnt[i][j+k] += l
            done[j+k] = 1

for j in range(w):
    done = [0] * h

    for i in range(h):
        if s[i][j] == '#': continue
        if done[i] :continue

        l = 0
        while i+l < h:
            if s[i+l][j] == '#': break
            l += 1

        for k in range(l):
            cnt[i+k][j] += l
            done[i+k] = 1

ans = 0

for i in range(h):
    for j in range(w):
        ans = max(ans, cnt[i][j]-1)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10**6)

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

field = [list(f) for f in s]

def dfs(x, y):
    global total
    total += 1
    # 移動する4方向をループ
    for dx in [-1, 1]:
        nx = x + dx; ny = y
        while 0 <= nx and nx < h and 0 <= ny and ny < w and field[nx][ny] == '.':
            total += 1
            # x方向にdx, y方向にdy移動した場所を(nx, ny)とする
            nx += dx
    for dy in [-1, 1]:
        nx = x; ny = y + dy
        while 0 <= nx and nx < h and 0 <= ny and ny < w and field[nx][ny] == '.':
            total += 1
            # x方向にdx, y方向にdy移動した場所を(nx, ny)とする
            ny += dy
    
    return total

res = 0
for i in range(h):
    for j in range(w):
        if field[i][j] == '.':
            # '.'からdfsを始める
            total = 0
            dfs(i, j)
            res = max(total, res)

print(res)

Submission status: Time Limit Exceeded

Solution code:
H, W = map(int, input().split())

G = []
for i in range(H):
    S = input()
    S = S.replace("#", "1").replace(".", "0")
    S = list(map(int, S))
    G.append(S)

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 i in range(H):
    for j in range(W):
        if G[i][j] == 1:
            L[i][j] = 0
        else:
            L[i][j] = L[i][max(0, j-1)] + 1
        if G[i][-(j+1)] == 1:
            R[i][-(j+1)] = 0
        else:
            R[i][-(j+1)] = R[i][min(-1, -(j+1)+1)] + 1

for j in range(W):
    for i in range(H):
        if G[i][j] == 1:
            U[i][j] = 0
        else:
            U[i][j] = U[max(0, i-1)][j] + 1
        if G[-(i+1)][j] == 1:
            D[-(i+1)][j] = 0
        else:
            D[-(i+1)][j] = D[min(-1, -(i+1)+1)][j] + 1

MAX = 0
for i in range(H):
    for j in range(W):
        MAX = max(MAX, L[i][j]+R[i][j]+U[i][j]+D[i][j]-3)
print(MAX)

Submission status: Time Limit Exceeded

Solution code:

H, W = map(int, input().split())

G = []
for i in range(H):
    S = input()
    S = S.replace("#", "1").replace(".", "0")
    S = list(map(int, S))
    G.append(S)
    
"""
for j in range(H):
    print(G[j])
"""

L = [[0] * W for _ in range(H)]
for i in range(H):
    for j in range(W):
        if G[i][j] == 1:
            L[i][j] = 0
            continue
        L[i][j] = L[i][max(0, j-1)] + 1
#print(L)

R = [[0] * W for _ in range(H)]
for i in range(H):
    for j in range(W):
        if G[i][-(j+1)] == 1:
            R[i][-(j+1)] = 0
            continue
        R[i][-(j+1)] = R[i][min(-1, -(j+1)+1)] + 1
#print(R)
    
U = [[0] * W for _ in range(H)]
for j in range(W):
    for i in range(H):
        if G[i][j] == 1:
            U[i][j] = 0
            continue
        U[i][j] = U[max(0, i-1)][j] + 1
#print(U)

D = [[0] * W for _ in range(H)]
for j in range(W):
    for i in range(H):
        if G[-(i+1)][j] == 1:
            D[-(i+1)][j] = 0
            continue
        D[-(i+1)][j] = D[min(-1, -(i+1)+1)][j] + 1
#print(D)

MAX = 0
for i in range(H):
    for j in range(W):
        MAX = max(MAX, L[i][j]+R[i][j]+U[i][j]+D[i][j]-3)
print(MAX)

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10 ** 9)
# input = sys.stdin.readline    ####
def int1(x): return int(x) - 1
def II(): return int(input())
def MI(): return map(int, input().split())
def MI1(): return map(int1, input().split())
def LI(): return list(map(int, input().split()))
def LI1(): return list(map(int1, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def MS(): return input().split()
def LS(): return list(input())
def LLS(rows_number): return [LS() for _ in range(rows_number)]
def printlist(lst, k=' '): print(k.join(list(map(str, lst))))
INF = float('inf')
# from math import ceil, floor, log2
# from collections import deque
from itertools import combinations as comb, combinations_with_replacement as comb_w, accumulate, product, permutations
# from heapq import heapify, heappop, heappush
# import numpy as np
# from numpy import cumsum  # accumulate

def solve():
    H, W = MI()
    G = LLS(H)

    R = [[] for _ in range(H)]
    for i, row in enumerate(G):
        tmp = [0] * (W+1)
        cnt = 0
        flag = True
        idx = -1
        # print(row)
        for j, h in enumerate(row):
            if h == '#':
                if cnt > 0:
                    tmp[idx] = tmp[idx] + cnt
                    tmp[j] = tmp[j] - cnt
                    cnt = 0
                    flag = True
            else:
                if flag:
                    idx = j
                    flag = False
                cnt = cnt + 1
        if cnt > 0:
            tmp[idx] = tmp[idx] + cnt
            tmp[-1] = - cnt

        # print(tmp)
        # print(*accumulate(tmp))
        R[i] = list(accumulate(tmp))
    # print(R)

    ans = -1
    for i in range(W):
        tmp2 = [0] * (H+1)
        cnt = 0
        flag2 = True
        idx = -1
        for j in range(H):
            d = G[j][i]
            if d == '#':
                if cnt > 0:
                    tmp2[idx] = cnt
                    tmp2[j] = - cnt
                    cnt = 0
                    flag2 = True
            else:
                if flag2:
                    idx = j
                    flag2 = False
                cnt = cnt + 1
        if cnt > 0:
            tmp2[idx] = cnt
            tmp2[-1] = - cnt
        # print(tmp2)
        # print(*accumulate(tmp2))
        for j, a in enumerate(list(accumulate(tmp2))[:-1]):
            ans = max(ans, R[j][i] + a - 1)

    print(ans)



if __name__ == '__main__':
    solve()

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect as b

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

X=[]
HL=[[] for _ in range(H)]
WL=[[] for _ in range(W)]
for h in range(H):
    for w in range(W):
        if S[h][w]=="#":
            HL[h].append(w)
            WL[w].append(h)

ans=0

for h in range(H):
    for w in range(W):
        if S[h][w]==".":
            if len(HL[h])==0:
                hol=W
            else:
                x=b(HL[h],w)
                N=len(HL[h])
                if x==0:
                    hol=HL[h][x]
                elif x==N:
                    hol=W-1-HL[h][-1]
                else:
                    hol=HL[h][x]-HL[h][x-1]-1

            if len(WL[w])==0:
                ver=H
            else:
                y=b(WL[w],h)
                M=len(WL[w])
                if y==0:
                    ver=WL[w][y]
                elif y==M:
                    ver=H-1-WL[w][-1]
                else:
                    ver=WL[w][y]-WL[w][y-1]-1
            ans=max(ans,ver+hol-1)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
from itertools import groupby

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60


def len_count(S):
    H, W = len(S), len(S[0])
    L = [[0] * W for _ in range(H)]
    for row_S, row_L in zip(S, L):
        vec = [1]
        for i in range(1, W):
            if row_S[i - 1] == row_S[i]:
                vec[-1] += 1
            else:
                vec.append(1)

        idx = 0
        valid = row_S[0] == '.'
        for n in vec:
            if valid:
                for i in range(idx, idx + n):
                    row_L[i] = n
            idx += n
            valid = not valid

    return L


def main():
    H, W = map(int, readline().split())
    S = [readline().strip() for _ in range(H)]

    L1 = len_count(S)
    L2 = list(zip(*len_count(list(zip(*S)))))

    ans = 0
    for row1, row2 in zip(L1, L2):
        for j in range(W):
            if ans < row1[j] + row2[j] - 1:
                ans = row1[j] + row2[j] - 1

    print(ans)
    return


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
from itertools import groupby

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60


def len_count(S):
    H, W = len(S), len(S[0])
    L = [[0] * W for _ in range(H)]
    for row_S, row_L in zip(S, L):
        idx = 0
        for k, g in groupby(list(row_S)):
            n = len(list(g))
            if k == '.':
                for i in range(idx, idx + n):
                    row_L[i] = n
            idx += n
    return L


def main():
    H, W = map(int, readline().split())
    S = [readline().strip() for _ in range(H)]

    L1 = len_count(S)
    L2 = list(zip(*len_count(list(zip(*S)))))

    ans = 0
    for row1, row2 in zip(L1, L2):
        for j in range(W):
            if ans < row1[j] + row2[j] - 1:
                ans = row1[j] + row2[j] - 1

    print(ans)
    return


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
from itertools import groupby

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60


def len_count(S):
    H, W = len(S), len(S[0])
    L = [[0] * W for _ in range(H)]
    for row_S, row_L in zip(S, L):
        idx = 0
        for k, g in groupby(list(row_S)):
            n = len(list(g))
            if k == '.':
                row_L[idx : idx + n] = [n] * n
            idx += n
    return L


def main():
    H, W = map(int, readline().split())
    S = [readline().strip() for _ in range(H)]

    L1 = len_count(S)
    L2 = list(zip(*len_count(list(zip(*S)))))

    ans = 0
    for row1, row2 in zip(L1, L2):
        for j in range(W):
            if ans < row1[j] + row2[j] - 1:
                ans = row1[j] + row2[j] - 1

    print(ans)
    return


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
from itertools import groupby

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60


def len_count(S):
    H, W = len(S), len(S[0])
    L = [[0] * W for _ in range(H)]
    for i, row in enumerate(S):
        idx = 0
        for k, g in groupby(list(row)):
            n = len(list(g))
            if k == '.':
                L[i][idx : idx + n] = [n] * n
            idx += n
    return L


def main():
    H, W = map(int, readline().split())
    S = [readline().strip() for _ in range(H)]

    L1 = len_count(S)
    L2 = list(zip(*len_count(list(zip(*S)))))

    ans = 0
    for i in range(H):
        for j in range(W):
            ans = max(ans, L1[i][j] + L2[i][j] - 1)

    print(ans)
    return


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys

sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7


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

    CW = [[0] * W for _ in range(H)]
    for h in range(H):
        cnt = 0
        for w in range(W):
            if S[h][w] == "#":
                cnt = 0
            else:
                cnt += 1
                CW[h][w] = cnt
        cnt = 0
        for w in reversed(range(W)):
            if S[h][w] == ".":
                cnt = max(cnt, CW[h][w])
                CW[h][w] = cnt
            else:
                cnt = 0

    CH = [[0] * W for _ in range(H)]
    for w in range(W):
        cnt = 0
        for h in range(H):
            if S[h][w] == "#":
                cnt = 0
            else:
                cnt += 1
                CH[h][w] = cnt
        cnt = 0
        for h in reversed(range(H)):
            if S[h][w] == ".":
                cnt = max(cnt, CH[h][w])
                CH[h][w] = cnt
            else:
                cnt = 0

    res = 0
    for h in range(H):
        for w in range(W):
            C = CW[h][w] + CH[h][w] - 1
            res = max(res, C)
    print(res)


if __name__ == '__main__':
    resolve()

Submission status: Time Limit Exceeded

Solution code:
import sys

H, W = map(int, sys.stdin.readline().strip().split())
grid = []
for _ in range(H):
    grid.append(sys.stdin.readline().strip())

# (x, y)のx, y方向のscore [a, b]
scores = [[[0, 0] for _ in range(W)] for _ in range(H)]
for y in range(H):
    start = 0
    count = 0
    for x in range(W):
        if grid[y][x] == "#" or x == W-1:
            if x == W-1 and grid[y][x] != "#":
                count += 1
            for i in range(start, x+1):
                scores[y][i][0] = count
            count = 0
            start = x + 1
        else:
            count += 1

for x in range(W):
    start = 0
    count = 0
    for y in range(H):
        if grid[y][x] == "#" or y == H-1:
            if y == H-1 and grid[y][x] != "#":
                count += 1
            for i in range(start, y+1):
                scores[i][x][1] = count
            count = 0
            start = y + 1
        else:
            count += 1

# print(scores)
ans = 0
for y in range(H):
    for x in range(W):
        ans = max(ans, scores[y][x][0] + scores[y][x][1] -1)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys

H, W = map(int, sys.stdin.readline().strip().split())
grid = []
for _ in range(H):
    grid.append(list(sys.stdin.readline().strip()))

# (x, y)B$Nx, yBJ}8~$Nscore [a, b]
scores = [[[0, 0] for _ in range(W)] for _ in range(H)]
for y in range(H):
    start = 0
    count = 0
    for x in range(W):
        if grid[y][x] == "#" or x == W-1:
            if x == W-1 and grid[y][x] != "#":
                count += 1
            for i in range(start, x+1):
                scores[y][i][0] = count
            count = 0
            start = x + 1
        else:
            count += 1

for x in range(W):
    start = 0
    count = 0
    for y in range(H):
        if grid[y][x] == "#" or y == H-1:
            if y == H-1 and grid[y][x] != "#":
                count += 1
            for i in range(start, y+1):
                scores[i][x][1] = count
            count = 0
            start = y + 1
        else:
            count += 1

# print(scores)
ans = 0
for y in range(H):
    for x in range(W):
        ans = max(ans, sum(scores[y][x])-1)
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):
        # print(f"i{i} j{j}")
        cnt = 0
        if S[i][j] == ".":
            #右に進む
            for k in range(j+1, W):
                # print(f"rk{k}")
                if S[i][k] == "#":
                    # print("BBB")
                    break
                if k < W:
                    # print("c")                    
                    cnt += 1
            #左に進む
            for k in range(1, j+1):
                # print(f"lk{k}")
                if S[i][j-k] == "#":
                    # print("BBB")
                    break
                if k >= 0:
                    # print("c")
                    cnt += 1
            #下
            for k in range(i+1, H):
                # print(f"dk{k}")
                if S[k][j] == "#":
                    # print("BBB")
                    break
                if k < H:
                    # print("c")
                    cnt += 1
            #上
            for k in range(1, i+1):
                # print(f"uk{k}")
                if S[i-k][j] == "#":
                    # print("BBB")
                    break
                if k >= 0:
                    # print("c")
                    cnt += 1
        ans = max(ans, cnt+1)
        # print(f"ans{ans}--------------")
print(ans)

Submission status: Time Limit Exceeded

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

s=[['#']*(w+2)]
for i in range(h):
    s.append(['#']+list(input())+['#'])
s.append([['#']*(w+2)])

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

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

ans=0
for i in range(1, h+1):  # hはy座標
    for j in range(1, w+1):  # wはx座標
        tmp_ans=l[i][j]+r[i][j]+u[i][j]+d[i][j]-3
        ans=max(ans, tmp_ans)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def main():
    H, W = list(map(int, input().split()))
    A = [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 h in range(H):
        for w in range(W):
            if A[h][w] == '#':
                pass
            else:
                if w == 0:
                    L[h][w] = 1
                else:
                    L[h][w] = L[h][w - 1] + 1
    for h in range(H):
        for w in range(W - 1, -1, -1):
            if A[h][w] == '#':
                pass
            else:
                if w == W - 1:
                    R[h][w] = 1
                else:
                    R[h][w] = R[h][w + 1] + 1
    for w in range(W):
        for h in range(H):
            if A[h][w] == '#':
                pass
            else:
                if h == 0:
                    U[h][w] = 1
                else:
                    U[h][w] = U[h - 1][w] + 1
    for w in range(W):
        for h in range(H - 1, -1, -1):
            if A[h][w] == '#':
                pass
            else:
                if h == H - 1:
                    D[h][w] = 1
                else:
                    D[h][w] = D[h + 1][w] + 1
    ans = 0
    for h in range(H):
        for w in range(W):
            ans = max(ans, L[h][w] + R[h][w] + U[h][w] + D[h][w] - 3)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
from itertools import product
def main():
    H, W = list(map(int, input().split()))
    A = [['#'] * (W + 2)] + [['#', ] + list(input()) + ['#', ] for _ in range(H)] + [['#'] * (W + 2)]
    DX = [[1, 0], [-1, 0], [0, 1], [0, -1]]
    ans = 0
    for h, w in product(range(1, H + 1), range(1, W + 1)):
        if A[h][w] == '.':
            count = 1
            for (dx, dy) in DX:
                x, y = h, w
                while True:
                    if A[x + dx][y + dy] == '.':
                        x += dx
                        y += dy
                        count += 1
                    else:
                        break
            ans = max(ans, count)
        else:
            continue
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
from itertools import product
def main():
    H, W = list(map(int, input().split()))
    A = [['#'] * (W + 2)] + [['#', ] + list(input()) + ['#', ] for _ in range(H)] + [['#'] * (W + 2)]
    DX = [[1, 0], [-1, 0], [0, 1], [0, -1]]
    ans = 0
    for h, w in product(range(1, H + 1), range(1, W + 1)):
        if A[h][w] == '.':
            count = 1
            for (dx, dy) in DX:
                stack = deque([[h, w]])
                while stack:
                    x, y = stack.popleft()
                    if A[x + dx][y + dy] == '.':
                        stack.append([x + dx, y + dy])
                        count += 1
                    else:
                        continue
            ans = max(ans, count)
        else:
            continue
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def m():
    H, W = map(int, input().split())
    S = [list(input()) for _ in range(H)]
    G = [[[-1, -1] for _ in range(W)] for _ in range(H)]
    for a in range(H):
        left = 0
        for b in range(W):
            if S[a][b] == '#':
                left = 0
            else:
                G[a][b][0] = left + 1
                left = G[a][b][0]
    for b in range(W):
        top = 0
        for a in range(H):
            if S[a][b] == '#':
                top = 0
            else:
                G[a][b][1] = top + 1
                top = G[a][b][1]

    for i in range(H-1, -1, -1):
        right = 0
        for j in range(W-1, -1, -1):
            if G[i][j][0] == -1:
                right = 0
            else:
                G[i][j][0] = max(G[i][j][0], right)
                right = G[i][j][0]

    for j in range(W-1, -1, -1):
        bot = 0
        for i in range(H-1, -1, -1):
            if G[i][j][1] == -1:
                bot = 0
            else:
                G[i][j][1] = max(G[i][j][1], bot)
                bot = G[i][j][1]
    ans = 0
    for i in range(H):
        for j in range(W):
            ans = max(sum(G[i][j]), ans)
    return ans-1


print(m())