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:
# coding: UTF-8
import sys
sys.setrecursionlimit(10**5)
import heapq
import re
import bisect
import random
import math
import itertools
from collections import defaultdict, deque
from copy import deepcopy
from decimal import *

readl= lambda: list(map(str, sys.stdin.readline().split()))
readt= lambda: tuple(map(int, sys.stdin.readline().split()))
read = lambda: sys.stdin.readline().rstrip()
readi = lambda: int(read())
readmi = lambda: map(int, sys.stdin.readline().split())
readms = lambda: map(str, sys.stdin.readline().split())

h, w = readmi()
wt = []
ht = []
count = 0
def dfs(th, tw, allow):
    global count
    if allow == True:
        if tw + 1 < w:
            if wt[th][tw + 1] == '.':
                count += 1
                wt[th][tw] = dfs(th, tw + 1, allow)
            else:
                count += 1
                wt[th][tw] = count
        else:
            count += 1
            wt[th][tw] = count
    return count
count = 0

def dfs_h(th, tw, allow):
    global count
    if allow == True:
        if th + 1 < h:
            if ht[th + 1][tw] == '.':
                count += 1
                ht[th][tw] = dfs_h(th + 1, tw, allow)
            else:
                count += 1
                ht[th][tw] = count
        else:
            count += 1
            ht[th][tw] = count
    return count

for i in range(h):
    p = read()
    wt.append(list(p))
    ht.append(list(p))

for i in range(h):
    for j in range(w):
        if wt[i][j] == '.':
            count = 0
            dfs(i,j,True)
        elif wt[i][j] == '#':
            wt[i][j] = 0

for i in range(w):
    for j in range(h):
        if ht[j][i] == '.':
            count = 0
            dfs_h(j,i,True)
        elif ht[j][i] == '#':
            ht[j][i] = 0
mx = 0
for i in range(w):
    for j in range(h):
        mx = max(mx,ht[j][i] + wt[j][i])
print(mx - 1)

Submission status: Time Limit Exceeded

Solution code:
import sys

h, w = list(map(int, input().split()))
field = [line.rstrip() for line in sys.stdin]

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

for i in range(1, h + 1):
    fi = field[i - 1]
    l_tmp = 0
    li = l[i]
    for j in range(1, w + 1):
        c = fi[j - 1]
        u[i][j] = u[i - 1][j] + 1 if c == '.' else 0
        li[j] = l_tmp = l_tmp + 1 if c == '.' else 0
for i in range(h, 0, -1):
    fi = field[i - 1]
    r_tmp = 0
    ri = r[i]
    for j in range(w, 0, -1):
        c = fi[j - 1]
        d[i][j] = d[i + 1][j] + 1 if c == '.' else 0
        ri[j] = r_tmp = r_tmp + 1 if c == '.' else 0

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

Submission status: Time Limit Exceeded

Solution code:

import sys
inf = 10 ** 20; INF = float("INF"); ans = 0
"""for dx, dy in dd:
        nx = j + dx; ny = i + dy
            if 0 <= nx < w and 0 <= ny < h:"""
def wi(): return list(map(int, sys.stdin.readline().split()))
def wip(): return [int(x) - 1 for x in sys.stdin.readline().split()]#WideIntPoint
def ws(): return sys.stdin.readline().split()
def i(): return int(sys.stdin.readline())
def s(): return input()
def hi(n): return [i() for _ in range(n)]
def hs(n): return [s() for _ in range(n)]#HeightString
def mi(n): return [wi() for _ in range(n)]#MatrixInt
def mip(n): return [wip() for _ in range(n)]
def ms(n): return [ws() for _ in range(n)]

h, w = wi()
s = []
for i in range(h):#h:高さ
    s.append([str(m) for m in list(input())])

numj = [[0 for _ in range(w)] for _ in range(h)]
for i in range(h):
    for j in range(w):
        if j == 0:
            if s[i][j] != "#":
                numj[i][j] += 1
        elif s[i][j] != "#":
            numj[i][j] += numj[i][j - 1] + 1
        else:
            numj[i][j] = 0
    for j2 in range(w - 2, -1, -1):
        if s[i][j2] != "#":
            if s[i][j2 + 1] != "#":
                numj[i][j2] = numj[i][j2 + 1]

numi = [[0 for _ in range(w)] for _ in range(h)]
for j in range(w):
    for i in range(h):
        if i == 0:
            if s[i][j] != "#":
                numi[i][j] += 1
        elif s[i][j] != "#":
            numi[i][j] += numi[i - 1][j] + 1
        else:
            numi[i][j] = 0
    for i2 in range(h - 2, -1, -1):
        if s[i2][j] != "#":
            if s[i2 + 1][j] != "#":
                numi[i2][j] = numi[i2 + 1][j]

for i in range(h):
    for j in range(w):
        ans = max(ans, numj[i][j] + numi[i][j])


print(ans - 1)

Submission status: Time Limit Exceeded

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


p = []
append1 = p.append
for i in range(h):
    q = []
    t = False
    now = 0
    for j in range(w):
        if s[i][j] == '.':
            t = True
            now += 1
            if j == w-1:
                q = q + [now for _ in range(now)]
        else:
            if t:
                q = q + [now for _ in range(now)] + [0]
                t = False
                now = 0
            else:
                q = q + [0]
                now = 0
    append1(q)
p2 = []
append2 = p2.append
for i in range(w):
    r = []
    t = False
    now = 0
    for j in range(h):
        if s[j][i] == '.':
            t = True
            now += 1
            if j == h-1:
                r = r + [now for _ in range(now)]
        else:
            if t:
                r = r + [now for _ in range(now)] + [0]
                t = False
                now = 0
            else:
                r = r + [0]
                now = 0
    append2(r)

ans = 0
for i in range(h):
    for j in range(w):
        ans = max(ans,p[i][j] + p2[j][i]-1)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys

h, w = list(map(int, input().split()))
field = [line.rstrip() for line in sys.stdin]

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

for i in range(1, h + 1):
    fi = field[i - 1]
    li = l[i]
    for j in range(1, w + 1):
        c = fi[j - 1]
        u[i][j] = u[i - 1][j] + 1 if c == '.' else 0
        li[j] = li[j - 1] + 1 if c == '.' else 0
for i in range(h, 0, -1):
    fi = field[i - 1]
    ri = r[i]
    for j in range(w, 0, -1):
        c = fi[j - 1]
        d[i][j] = d[i + 1][j] + 1 if c == '.' else 0
        ri[j] = ri[j + 1] + 1 if c == '.' else 0

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

Submission status: Time Limit Exceeded

Solution code:
sizeI, sizeJ = map(int, input().split())
a = [input().strip() for _ in range(sizeI)]
 
rowVal = [[0 for _ in range(sizeJ)] for _ in range(sizeI)]
for i in range(sizeI):
    for j in range(sizeJ):
        if j == 0:
            rowVal[i][j] = 1 if a[i][j] == '.' else 0
        else:
            if a[i][j] == '.':
                rowVal[i][j] = rowVal[i][j - 1] + 1
            else:
                rowVal[i][j] = 0
    for j in range(sizeJ - 2, -1, -1):
        if rowVal[i][j] and rowVal[i][j + 1]:
            rowVal[i][j] = rowVal[i][j + 1]
 
colVal = [[0 for _ in range(sizeJ)] for _ in range(sizeI)]
for j in range(sizeJ):
    for i in range(sizeI):
        if i == 0:
            colVal[i][j] = 1 if a[i][j] == '.' else 0
        else:
            if a[i][j] == '.':
                colVal[i][j] = colVal[i - 1][j] + 1
            else:
                colVal[i][j] = 0
    for i in range(sizeI - 2, -1, -1):
        if colVal[i][j] and colVal[i + 1][j]:
            colVal[i][j] = colVal[i + 1][j]
 
ret = 0
for i in range(sizeI):
    for j in range(sizeJ):
        ret = max(ret, rowVal[i][j] + colVal[i][j] - 1)
print(ret)

Submission status: Time Limit Exceeded

Solution code:
import fractions as fr
import itertools as it
import collections as col
import heapq as hq
import sys
sys.setrecursionlimit(10**9)

"""
N=int(input())
W=list(map(int, input().split()))
ans=10**100
for T in range(1,N):
        S1=sum(W[:T])
        S2=sum(W[T:])
        ans = min(ans, abs(S1-S2))
print(ans)
"""
"""
P,Q,R=map(int, input().split())
print(min([P+Q, P+R, Q+R]))
"""
"""
MOD=1000000007
N,M=map(int, input().split())
A=set()
for i in range(M):
        A.add(int(input()))

dp=dict()
dp[0]=1
dp[1]=0 if 1 in A else 1
for i in range(2, N+1):
        if i in A: dp[i] = 0
        else:
                m1=0 if (i-1) in A else dp[i-1]
                m2=0 if (i-2) in A else dp[i-2]
                dp[i]=(m1+m2) % MOD
print(dp[N])
"""
def genArr2(h,w):
        return [None]*(h*w)
def setArr2(A,i,j,x):
        A[i*W + j] = x
def getArr2(A,i,j):
        return A[i*W + j]
H,W=map(int, input().split())
S=genArr2(H,W)
for i in range(H):
        s=input()
        l=list(s)
        for j in range(W):
                setArr2(S,i,j,l[j])

SWR=genArr2(H,W)
for i in range(H):
        setArr2(SWR, i, 0, 0 if getArr2(S, i, 0) == "#" else 1)
        for j in range(1,W):
                if getArr2(S, i, j) == "#": setArr2(SWR, i, j, 0)
                else: setArr2(SWR, i, j, getArr2(SWR, i, j-1) + 1)
SWL=genArr2(H,W)
for i in range(H):
        setArr2(SWL, i, W-1, 0 if getArr2(S, i, W-1) == "#" else 1)
        for j in range(W-2,-1,-1):
                if getArr2(S, i, j) == "#": setArr2(SWL, i, j, 0)
                else: setArr2(SWL, i, j, getArr2(SWL, i, j+1) + 1)
SHB=genArr2(H,W)
for j in range(W):
        setArr2(SHB, 0, j, 0 if getArr2(S, 0, j) == "#" else 1)
        for i in range(1,H):
                if getArr2(S, i, j) == "#": setArr2(SHB, i, j, 0)
                else: setArr2(SHB, i, j, getArr2(SHB, i-1, j) + 1)
SHT=genArr2(H,W)
for j in range(W):
        setArr2(SHT, H-1, j, 0 if getArr2(S, H-1, j) == "#" else 1)
        for i in range(H-2, -1, -1):
                if getArr2(S, i, j) == "#": setArr2(SHT, i, j, 0)
                else: setArr2(SHT, i, j, getArr2(SHT, i+1, j) + 1)

ans=0
for i in range(H):
        for j in range(W):
                x=getArr2(SWR, i, j) + getArr2(SWL, i, j) + getArr2(SHB, i, j) + getArr2(SHT,i,j) - 3
                ans=max(ans,x)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n,m = map(int, input().split())
l = []
for i in range(n):
    a = input()
    a.split()
    l.append(a)
def f(x,y):
    global l ,n,m
    ans = -3
    for i in range(x,n):
        if l[i][y] == ".":
            ans += 1
        else :
            break
    for i in range(x+1):
        if l[x-i][y] == ".":
            ans += 1
        else :
            break
    for i in range(y,m):
        if l[x][i] == ".":
            ans += 1
        else :
            break
    for i in range(y+1):
        if l[x][y-i] == ".":
            ans += 1
        else:
            break
    return ans
k = 1
for i in range(n):
    for j in range(m):
        k = max(k,f(i,j))
print(k)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

H, W = map(int, input().split())
S = []
for i in range(H):
    s = input()
    S.append(list(s))
arr_S = np.array(S)
S_2 = arr_S.T

Ans = []
for index, char in enumerate(S):
    tmp = 0
    stepper = 0
    a = []
    for i in char:
        if i == "#" and tmp == 0:
            a.append(0)
        elif i == "." and tmp == 0:
            tmp = 1
            stepper += 1
        elif i == "." and tmp == 1:
            stepper += 1

        elif i == "#" and tmp == 1:
            tmp = 0
            for j in range(stepper):
                a.append(stepper)
            stepper = 0
            a.append(0)
    if stepper > 0:
        for j in range(stepper):
            a.append(stepper)
    Ans.append(a)

Ans2 = []
for index, char in enumerate(S_2):
    tmp = 0
    stepper = 0
    a = []
    for i in char:
        if i == "#" and tmp == 0:
            a.append(0)
        elif i == "." and tmp == 0:
            tmp = 1
            stepper += 1
        elif i == "." and tmp == 1:
            stepper += 1

        elif i == "#" and tmp == 1:
            tmp = 0
            for j in range(stepper):
                a.append(stepper)
            stepper = 0
            a.append(0)
    if stepper > 0:
        for j in range(stepper):
            a.append(stepper)
    Ans2.append(a)

S3 = np.array(Ans2).T

x = np.array(Ans)
y = np.array(S3)

Ans_array = x+y
maxNum = 0
for i in Ans_array:
    mx = max(i)
    if mx > maxNum:
        maxNum = mx
print(maxNum-1)

Submission status: Time Limit Exceeded

Solution code:
#!venv/bin/python
 
import math
import numpy as np
import random
import copy
import statistics 
import bisect
import heapq
from collections import defaultdict 
import time
import sys
from pprint import pprint

WARU = 1000000007

H, W = [ int(x) for x in input().split() ]
S = []
for _i in range(H):
    S.append(input())

def key(h,w):
    return "{}:{}".format(h,w)

ans = [] # right,under
for h in range(H):
    line = [ [0,0] for x in range(W) ]
    ans.append(line)

for h in range(H):
    for w in range(W):
        if S[h][w] == "#":
            continue
        right = 0
        under = 0
        if w == 0:
            right = 1
        else:
            right = ans[h][w-1][0] + 1
        if h == 0:
            under = 1
        else:
            under = ans[h-1][w][1] + 1
        ans[h][w] = [right,under]

ans2 = [] # left,up
for h in range(H):
    line = [ [0,0] for x in range(W) ]
    ans2.append(line)

for h in range(H-1,-1,-1):
    for w in range(W-1,-1,-1):
        if S[h][w] == "#":
            continue
        right = 0
        under = 0
        if w == W - 1:
            right = 1
        else:
            right = ans2[h][w+1][0] + 1
        if h == H - 1:
            under = 1
        else:
            under = ans2[h+1][w][1] + 1
        ans2[h][w] = [right,under]

max_mass = 0
max_count = 0

ans3 = [] # right,under
for h in range(H):
    line = [ 0 for x in range(W) ]
    ans3.append(line)


for h in range(H):
    for w in range(W):
        ans3[h][w] = ans[h][w][0] + ans[h][w][1] + ans2[h][w][0] + ans2[h][w][1]


max = 0
for h in range(H):
    for w in range(W):
        if max < ans3[h][w]:
            max = ans3[h][w]
print (max - 3)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

h, w = map(int, input().split())
x = [list(map(int, list(input().replace('.', '0').replace('#', '1')))) for _ in range(h)]
x = np.array(x, dtype=np.int)
xh = np.zeros([h, w], dtype=np.int)
for i in range(h):
    l = 0
    for j in range(w):
        if x[i][j]:
            xh[i, l:j] = j - l
            l = j + 1
    xh[i, l:] = w - l
xh = xh.T
x = x.T
ans = 0
for i in range(w):
    l = 0
    for j in range(h):
        if x[i][j]:
            if j > l:
                ans = max(ans, max(xh[i, l:j]) + j - l - 1)
            l = j + 1
    if h > l:
        ans = max(ans, max(xh[i, l:]) + h - l - 1)
print(ans)

Submission status: Time Limit Exceeded

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

s = [[i for i in list(str(input()))] for i in range(h)]

board_w = [[0 for i in range(w)] for i in range(h)]
board_h = [[0 for i in range(h)] for i in range(w)]

# 横、縦の順に走査
for i in range(h):
    cnt_continue = 0
    for j, c in enumerate(s[i]):
        if c == ".":
            cnt_continue += 1
        elif cnt_continue > 0:
            for k in range(1, cnt_continue+1):
                board_w[i][j-k] = cnt_continue
            cnt_continue = 0
    for k in range(cnt_continue):
        board_w[i][w-1-k] = cnt_continue
    cnt_continue = 0

s = list(map(list, zip(*s)))

for i in range(w):
    cnt_continue = 0
    for j, c in enumerate(s[i]):
        if c == ".":
            cnt_continue += 1
        elif cnt_continue > 0:
            for k in range(1, cnt_continue+1):
                board_h[i][j-k] = cnt_continue
            cnt_continue = 0
    for k in range(cnt_continue):
        board_h[i][h-1-k] = cnt_continue
    cnt_continue = 0

board_h = list(map(list, zip(*board_h)))
ans = 0

for i in range(h):
    for j in range(w):
        v = int(board_w[i][j]) + int(board_h[i][j])
        ans = max(ans, v)

print(ans-1)

Submission status: Time Limit Exceeded

Solution code:
import sys
h,w = map(int,input().split())
left = [[0 for x in range(w)]for _ in range(h)]
right = [[0 for x in range(w)]for _ in range(h)]
up = [[0 for x in range(w)]for _ in range(h)]
down = [[0 for x in range(w)]for _ in range(h)]
arr = []
for i in range(h):
    arr.append(sys.stdin.readline())
for i in range(h):
    s = 0
    for j in range(w):
        if arr[i][j]=='.':
            left[i][j]=s
            s+=1
        else:
            s=0
for i in range(h):
    s = 0
    s2 =0
    for j in range(w):
        if arr[i][j] == '.':
            left[i][j] = s
            s += 1
        else:
            s = 0
        if arr[i][w-1-j]=='.':
            right[i][w-1-j] = s2
            s2+=1
        else:
            s2=0
for i in range(w):
    s = 0
    s2 = 0
    for j in range(h):
        if arr[j][i] == '.':
            up[j][i] = s
            s+=1
        else:
            s=0
        if arr[h-1-j][i] == '.':
            down[h-1-j][i] = s2
            s2+=1
        else:
            s2=0
mm = 0
for i in range(h):
    for j in range(w):
        if arr[i][j] =='.':
            mm = max(mm,left[i][j]+right[i][j]+up[i][j]+down[i][j]+1)
print(mm)

Submission status: Time Limit Exceeded

Solution code:
h,w = map(int, input().split())
S = [""] * (h+2)
s = "#"*(w+2)
S[0] = s
for i in range(1, h+1):
    s = input()
    S[i] = "#" + s + "#"

S[h+1] = "#"*(w+2)
ans = 0
for i in range((h)):
    for j in range((w)):
        cnt = 1
        if S[i+1][j+1] == ".":
        
            #ue
            idx = 1
            while S[i+1-idx][j+1] != "#":
                cnt += 1
                idx += 1
            #sita
            idx = 1
            while S[i+1+idx][j+1] != "#":
                cnt += 1
                idx += 1

            #hidari
            idx = 1
            while S[i+1][j+1-idx] != "#":
                cnt += 1
                idx += 1
            #migi
            idx = 1
            while S[i+1][j+1+idx] != "#":
                cnt += 1
                idx += 1

        ans = max(ans, cnt)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
h, w = map(int, input().split())
S = []
for i in range(h):
    S.append(input())

q = [[] for i in range(w)]

sw = []

for i in range(h):
    c = []
    for j in range(w):
        if S[i][j] == '#':
            c.append(j)
            q[j].append(i)
    c.append(w)
    k = []
    p = c[0]
    k += [p] * p
    l = len(c)
    for j in range(1, l):
        k += [0]
        p = c[j] - c[j-1] - 1
        k += [p] * p
    sw.append(k)

sh =[]

for i in range(w):
    c = q[i]
    c.append(h)
    k = []
    p = c[0]
    k += [p] * p
    l = len(c)
    for j in range(1, l):
        k += [0]
        p = c[j] - c[j-1] - 1
        k += [p] * p
    sh.append(k)

sw = np.array(sw)
sh = np.array(sh)
sh = sh.T

x = sw + sh

print(np.amax(x) - 1)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
H, W = map(int, input().split())
S = [list(input()) for i in range(H)]

# 横
A = np.array([[-10000] * W for i in range(H)])
B = np.array([[-10000] * W for i in range(H)])

S = np.array(S)
S2 = S.T


A2 = A.T
for j in range(H):
    ii = 0
    s = 0
    for i in range(W+1):
        if i == W or S2[i, j] == '#':
            A2[ii:i, j] = s
            s = 0
            ii = i + 1
        else:
            s += 1
A = A2.T

for j in range(W):
    ii = 0
    s = 0
    for i in range(H+1):
        if i == H or S[i, j] == '#':
            B[ii:i, j] = s
            s = 0
            ii = i + 1
        else:
            s += 1

X = A + B - 1
print(np.max(X))

Submission status: Time Limit Exceeded

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


def yoko(y, x):
    ret1 = 0
    xx = x
    while xx + 1 <= W - 1 and S[y][xx + 1] != '#':
        ret1 += 1
        xx += 1
    xxx = x
    ret2 = 0
    while 0 <= xxx - 1 and S[y][xxx - 1] != '#':
        ret2 += 1
        xxx -= 1
    ret3 = ret1 + ret2
    for i in range(xx, xxx + 1):
        yo[y][i] = ret3
    return ret3


def tate(y, x):
    ret1 = 0
    yy = y
    while 0 <= yy - 1 and S[yy - 1][x] != '#':
        ret1 += 1
        yy -= 1
    yyy = y
    ret2 = 0
    while yyy + 1 <= H - 1 and S[yyy + 1][x] != '#':
        ret2 += 1
        yyy += 1
    ret3 = ret1 + ret2
    for i in range(yy, yyy + 1):
        ta[i][x] = ret3
    return ret3


ans = 0
for y in range(H):
    for x in range(W):
        if S[y][x] == '#':
            continue
        else:
            if yo[y][x] != '@' and ta[y][x] != '@':
                T = ta[y][x]
                Y = yo[y][x]
            elif yo[y][x] != '@':
                Y = yo[y][x]
                T = tate(y, x)
            elif ta[y][x] != '@':
                T = ta[y][x]
                Y = yoko(y, x)
            else:
                Y = yoko(y, x)
                T = tate(y, x)
        ans = max(ans, T + Y)
print(ans + 1)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
import math
import sys
sys.setrecursionlimit(10000000)
from collections import deque


def search_y(i, j, matrix):
    """i, j について，y方向を埋める
    """
    to_plus = 0
    to_minus = 0
    while(True):
        to_plus += 1
        if (matrix[i + to_plus][j][0] == 0):
            continue
        else:
            to_plus -= 1
            break
    while(True):
        to_minus += 1
        if (matrix[i - to_minus][j][0] == 0):
            continue
        else:
            to_minus -= 1
            break
    sum_ = to_plus + to_minus + 1
    for i2 in range(0, to_plus + 1):
        matrix[i + i2][j][0] = sum_
    for i2 in range(0, to_minus + 1):
        matrix[i - i2][j][0] = sum_
    return matrix

def search_x(i, j, matrix):
    """i, j について，y方向を埋める
    """
    to_plus = 0
    to_minus = 0
    while(True):
        to_plus += 1
        if (matrix[i][j + to_plus][1] == 0):
            continue
        else:
            to_plus -= 1
            break
    while(True):
        to_minus += 1
        if (matrix[i][j - to_minus][1] == 0):
            continue
        else:
            to_minus -= 1
            break
    sum_ = to_plus + to_minus + 1
    for i2 in range(0, to_plus + 1):
        matrix[i][j + i2][1] = sum_
    for i2 in range(0, to_minus + 1):
        matrix[i][j - i2][1] = sum_
    return matrix

def main():
    H, W = map(int, input().split())
    matrix = []
    queue = deque()
    for i in range(0, H + 2):
        lines = []
        for j in range(0, W + 2):
            lines.append([-1, -1])
        matrix.append(lines)

    for i in range(1, H + 1):
        string = input()
        for j in range(1, W + 1):
            if string[j - 1] == "#":
                continue
            else:
                matrix[i][j] = [0, 0]
                queue.append([i, j])


    max_sum = 0
    while(queue):
        i, j = queue.pop()
        if (matrix[i][j][0] == 0):
            # y方向を埋める
            matrix = search_y(i, j, matrix)
        if (matrix[i][j][1] == 0):
            # x方向を埋める
            matrix = search_x(i, j, matrix)
        max_sum = max(matrix[i][j][0] + matrix[i][j][1], max_sum)
    
    print(max_sum - 1)



if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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


def LI(): return [int(x) for x in sys.stdin.readline().split()]


def main():
    H, W = LI()
    S = [0 for _ in range(H)]
    A = [[[-1, -1] for _ in range(W)] for _ in range(H)]
    for i in range(H):
        str = input()
        for j, s in enumerate(str):
            if s == '#':
                S[i] |= 1 << j
    ans = 1
    tate = 0
    yoko = 0
    for i in range(H):
        yoko = 0
        for j in range(W):
            if (S[i] >> j) & 1:
                yoko = 0
                continue
            if yoko == 0:
                for k in range(j+1, W):
                    if (S[i] >> k) & 1:
                        break
                    yoko += 1
                for k in range(j+1, W):
                    if (S[i] >> k) & 1:
                        break
                    A[i][k][0] = yoko
                A[i][j][0] = yoko
            tate = 0
            if A[i][j][1] == -1:
                for k in range(i+1, H):
                    if (S[k] >> j) & 1:
                        break
                    tate += 1
                for k in range(i+1, H):
                    if (S[k] >> j) & 1:
                        break
                    A[k][j][1] = tate
                A[i][j][1] = tate
            ans = max(ans, sum(A[i][j]) + 1)
    print(ans)


main()

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(500000)
H,W=map(int,input().split())
S=list(list(input())for _ in range(H))

def dfsr(h,w):
    if 0<=h<=H-1 and 0<=w<=W-1 and S[h][w]=='.':
        global ans
        ans+=1
        dfsr(h,w+1)
    else:return
def dfsl(h,w):
    if 0<=h<=H-1 and 0<=w<=W-1 and S[h][w]=='.':
        global ans
        ans+=1
        dfsl(h,w-1)
    else:return

def dfsd(h,w):
    if 0<=h<=H-1 and 0<=w<=W-1 and S[h][w]=='.':
        global ans
        ans+=1
        dfsd(h+1,w)
    else:return

def dfsu(h,w):
    if 0<=h<=H-1 and 0<=w<=W-1 and S[h][w]=='.':
        global ans
        ans+=1
        dfsu(h-1,w)
    else:return

rans=0
ans=0
for h in range(H):
    for w in range(W):
        ans=0
        if S[h][w]=='.':
            dfsr(h,w+1)
            dfsl(h,w-1)
            dfsu(h-1,w)
            dfsd(h+1,w)
            ans+=1
            rans=max(ans,rans)
print(rans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

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

arr = np.empty((0, w), int)

for i in range(h):
    l = list(input())
    arr = np.append(arr, np.array([l]), axis = 0)

ans = 0
for i in range(h):
    for j in range(w):
        if arr[i, j] == "#":
            continue

        left = j
        right = w-j-1
        up = i
        down = h-i-1
        for k in range(j):
            if arr[i][j-k-1] == "#":
                left = k
                break
        for k in range(w-j-1):
            if arr[i][j+k+1] == "#":
                right = k
                break
        for k in range(i):
            if arr[i-k-1][j] == "#":
                up = k
                break
        for k in range(h-i-1):
            if arr[i+k+1][j] == "#":
                down = k
                break

        if left + right + up + down > ans:
            ans = left + right + up + down

print(ans+1)

Submission status: Time Limit Exceeded

Solution code:
import bisect

h, w = map(int, input().split())
s = [list(input()) for i in range(h)]
dh = {}
for i in range(h):
    dh[i] = []
    for j in range(w):
        if s[i][j] == "#":
            dh[i].append(j)
dw = {}
for i in range(w):
    dw[i] = []
    for j in range(h):
        if s[j][i] == "#":
            dw[i].append(j)
fw = [[0] * w for i in range(h)]
for i in range(h):
    for j in range(w):
        if s[i][j] == ".":
            if fw[i][j - 1]:
                fw[i][j] = 2
            else:
                fw[i][j] = 1
fh = [[0] * h for i in range(w)]
for i in range(w):
    for j in range(h):
        if s[j][i] == ".":
            if fh[i][j - 1]:
                fh[i][j] = 2
            else:
                fh[i][j] = 1
aw = [[0] * w for i in range(h)]
ah = [[0] * h for i in range(w)]
ans = 0
for i in range(h):
    a = dh[i]
    for j in range(w):
        if s[i][j] == "#":
            continue
        b = dw[j]
        if fw[i][j] == 1:
            c = bisect.bisect_left(a, j)
            if not a:
                wl = w
            else:
                if c == 0:
                    wl = a[0]
                elif c == len(a):
                    wl = w - a[-1] - 1
                else:
                    wl = a[c] - a[c - 1] - 1
        else:
            wl = aw[i][j - 1]
        if fh[j][i] == 1:
            d = bisect.bisect_left(b, i)
            if not b:
                hl = h
            else:
                if d == 0:
                    hl = b[0]
                elif d == len(b):
                    hl = h - b[-1] - 1
                else:
                    hl = b[d] - b[d - 1] - 1
        else:
            hl = ah[j][i - 1]
        aw[i][j] = wl
        ah[j][i] = hl
        ans = max(ans, wl + hl - 1)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
H, W = map(int, input().split())
S = [list(input()) for _ in range(H)]
X = [[-10**2 for __ in range(W)] for _ in range(H)]
Y = [[-10**2 for __ in range(W)] for _ in range(H)]
last_ys = [-1] * W
for y in range(H):
  last_x = -1
  for x in range(W):
    if S[y][x] == "#":
      for t in range(last_x+1,x):
        X[y][t] = x-1-last_x
      for t in range(last_ys[x]+1,y):
        Y[t][x] = y-1-last_ys[x]
      last_x = x
      last_ys[x] = y
  for t in range(last_x+1,W):
    X[y][t] = W-1-last_x
for x in range(W):
  for t in range(last_ys[x]+1,H):
    Y[t][x] = H-1-last_ys[x]
ans = 0
for y in range(H):
  for x in range(W):
    ans = max(ans, Y[y][x]+X[y][x])
print(ans-1)