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 = [input() for _ in range(h)]

x_inf = [[0 for i in range(w)] for j in range(h)]
y_inf = [[0 for i in range(w)] for j in range(h)]
for i in range(h):
    p = -1
    for j in range(w):
        if s[i][j]=="#":
            p = j
        else:
            x_inf[i][j] = p

for i in range(h):
    p = w
    for j in range(-1,-w-1,-1):
        if s[i][j]=="#":
            p = w+j
        else:
            x_inf[i][j] -= p-1

for i in range(w):
    p = -1
    for j in range(h):
        if s[j][i]=="#":
            p = j
        else:
            y_inf[j][i] = p

for i in range(w):
    p = h
    for j in range(h-1,-1,-1):
        if s[j][i]=="#":
            p = j
        else:
            y_inf[j][i] -= p-1
result = []
for i in range(h):
    for j in range(w):
        if s[i][j]==".":
            result.append(x_inf[i][j]+y_inf[i][j]+1)
print(-min(result))

Submission status: Time Limit Exceeded

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

s = [input() for _ in range(h)]

result = []
for i in range(h):
    for j in range(w):
        if s[i][j] == "#":
            continue
        else:
            c = 1
            x = 1
            while i+x < h and s[i+x][j] == ".":
                x += 1
                c += 1
            x = 1
            while i-x >= 0 and s[i-x][j] == ".":
                x += 1
                c += 1
            y = 1
            while j+y < w and s[i][j+y] == ".":
                y += 1
                c += 1
            y = 1
            while j-y >= 0 and s[i][j-y] == ".":
                y += 1
                c += 1
        result.append(c) 
print(max(result))

Submission status: Time Limit Exceeded

Solution code:
import sys
from bisect import bisect_left

h,w=list(map(int,input().split()))
s=[]

lstw=[]
lsth=[]

for i in range(h):
    s.append([x for x in sys.stdin.readline().strip()])
    
    tw=[-1]
    for j in range(w):
        if s[i][j]=='#':
            tw.append(j)
    tw.append(w)

    lstw.append(tw)

for j in range(w):
    th=[-1]
    for i in range(h):
        if s[i][j]=='#':
            th.append(i)
    th.append(h)
    lsth.append(th)

ret=0

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

        posh=bisect_left(lsth[j],i)
        posw=bisect_left(lstw[i],j)

        cnth=lsth[j][posh]-lsth[j][posh-1]-1
        cntw=lstw[i][posw]-lstw[i][posw-1]-1

        ret=max(ret,cnth+cntw-1)

print(ret)

Submission status: Time Limit Exceeded

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

L = [[0]*W for i in range(H)]
R = [[0]*W for i in range(H)]
U = [[0]*W for i in range(H)]
D = [[0]*W for i in range(H)]
for h in range(H):
    for w in range(W):
        if S[h][w]==".":
            if h==0:D[h][w]=1
            else:D[h][w]=D[h-1][w]+1
            if w==0:R[h][w]=1
            else:R[h][w]=R[h][w-1]+1
for h in range(H-1,-1,-1):
    for w in range(W-1,-1,-1):
        if S[h][w]==".":
            if h==H-1:U[h][w]=1
            else:U[h][w]=U[h+1][w]+1
            if w==W-1:L[h][w]=1
            else:L[h][w]=L[h][w+1]+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)

Submission status: Time Limit Exceeded

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

T = [[0]*W for i in range(H)]
L = [[0]*W for i in range(H)]
for w in range(W):
    a = 0
    cnt = 0
    for h in range(H):
        if S[h][w]==".":
            cnt+=1
        else:
            for ha in range(a,h):
                T[ha][w]=cnt
            a = h+1
            cnt = 0
    if cnt!=0:
        for ha in range(a,H):
            T[ha][w]=cnt
for h in range(H):
    a = 0
    cnt = 0
    for w in range(W):
        if S[h][w]==".":cnt+=1
        else:
            for wa in range(a,w):
                L[h][wa]=cnt
            a = w+1
            cnt = 0
    if cnt!=0:
        for wa in range(a,W):
            L[h][wa]=cnt

ans = 0
for h in range(H):
    for w in range(W):
        ans = max(ans,T[h][w]+L[h][w]-1)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
H,W = map(int,input().split())
S = [input() for i in range(H)]
ans = 0
def f(h,w):
    global H,W,S
    cnt = 1
    for ha in range(h-1,-1,-1):
        if S[ha][w]=="#":break
        cnt+=1
    for hb in range(h+1,H):
        if S[hb][w]=="#":break
        cnt+=1
    for wa in range(w-1,-1,-1):
        if S[h][wa]=="#":break
        cnt+=1
    for wb in range(w+1,W):
        if S[h][wb]=="#":break
        cnt+=1
    return cnt
for h in range(H):
    for w in range(W):
        if S[h][w]==".":
            ans = max(ans,f(h,w))
print(ans)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/python3
# -*- coding:utf-8 -*-

def main():
  h, w = map(int, input().strip().split())
  maze = [list(input().strip()) for _ in range(h)]
  countw = [[0]*w for _ in range(h)]
  counth = [[0]*w for _ in range(h)]
  for i in range(h):
    l=r=0
    for j in range(w):      
      if maze[i][j] == '#':
        l=0
      else:
        l+=1
        countw[i][j] += l
      if maze[i][-(j+1)] == '#':
        r=0
      else:
        r+=1
        countw[i][-(j+1)] += r
        
  for j in range(w):
    t=b=0
    for i in range(h):
      if maze[i][j] == '#':
        t=0
      else:
        t+=1
        counth[i][j] += t
      if maze[-(i+1)][j] == '#':
        b=0
      else:
        b+=1
        counth[-(i+1)][j] += b
    
  max = -1
  for i in range(h):
    for j in range(w):
      score = counth[i][j] + countw[i][j]
      if max < score:
        max = score
  print(max-3)
  
if __name__=='__main__':
  main()

Submission status: Time Limit Exceeded

Solution code:
H, W = map(int, input().split())
S = []
for _ in range(H):
    S.append(input())
cnt = [[1 for _ in range(W)] for _ in range(H)]
for h in range(H):
    ws = 0
    for w in range(W):
        if S[h][w] == "#":
            ws = 0
        else:
            cnt[h][w] += ws
            ws += 1
    ws = 0
    for w in range(W-1, -1, -1):
        if S[h][w] == "#":
            ws = 0
        else:
            cnt[h][w] += ws
            ws += 1

for w in range(W):
    hs = 0
    for h in range(H):
        if S[h][w] == "#":
            hs = 0
        else:
            cnt[h][w] += hs
            hs += 1
    hs = 0
    for h in range(H-1, -1, -1):
        if S[h][w] == "#":
            hs = 0
        else:
            cnt[h][w] += hs
            hs += 1
ans = 0
for h in range(H):
    for w in range(W):
        ans = max(ans, cnt[h][w])
print(ans)

Submission status: Time Limit Exceeded

Solution code:
#lamp
import sys
input=sys.stdin.readline
h,w=map(int,input().split())
lists=[]
for i in range(h):
    b=list(input())
    lists.append(b)
    
right=[[0 for i in range(w)] for j in range(h)]
left=[[0 for i in range(w)] for j in range(h)]
up=[[0 for i in range(w)] for j in range(h)]
down=[[0 for i in range(w)] for j in range(h)]

#rightの累積和
for i in range(h):
    for j in range(w-1,-1,-1):
        if j==w-1:
            if lists[i][j]=="#":
                right[i][j]=0
            else:
                right[i][j]=1
        else:
            if lists[i][j+1]=="#":
                if lists[i][j]=="#":
                    right[i][j]=0
                else:
                    right[i][j]=1
            elif lists[i][j+1]==".":
                if lists[i][j]=="#":
                    right[i][j]=0
                else:
                    right[i][j]=right[i][j+1]+1


#leftの累積和
for i in range(h):
    for j in range(w):
        if j==0:
            if lists[i][j]=="#":
                left[i][j]=0
            else:
                left[i][j]=1
        else:
            if lists[i][j]=="#":
                left[i][j]=0
            else:
                left[i][j]=left[i][j-1]+1


#upの累積和
for i in range(w):
    for j in range(h):
        if j==0:
            if lists[j][i]=="#":
                up[j][i]=0
            else:
                up[j][i]=1
        else:
            if lists[j][i]=="#":
                up[j][i]=0
            else:
                up[j][i]=up[j-1][i]+1

                
#downの累積和
for i in range(w):
    for j in range(h-1,-1,-1):
        if j==h-1:
            if lists[j][i]=="#":
                down[j][i]=0
            else:
                down[j][i]=1
        else:
            if lists[j][i]=="#":
                down[j][i]=0
            else:
                down[j][i]=down[j+1][i]+1
                
maxi=0
for i in range(h):
    for j in range(w):
        maxi=max(maxi,right[i][j]+left[i][j]+up[i][j]+down[i][j]-3)
print(maxi)

Submission status: Time Limit Exceeded

Solution code:
#lamp
h,w=map(int,input().split())
lists=[]
for i in range(h):
    b=list(input())
    lists.append(b)
    
right=[[0 for i in range(w)] for j in range(h)]
left=[[0 for i in range(w)] for j in range(h)]
up=[[0 for i in range(w)] for j in range(h)]
down=[[0 for i in range(w)] for j in range(h)]

#rightの累積和
for i in range(h):
    for j in range(w-1,-1,-1):
        if j==w-1:
            if lists[i][j]=="#":
                right[i][j]=0
            else:
                right[i][j]=1
        else:
            if lists[i][j+1]=="#":
                if lists[i][j]=="#":
                    right[i][j]=0
                else:
                    right[i][j]=1
            elif lists[i][j+1]==".":
                if lists[i][j]=="#":
                    right[i][j]=0
                else:
                    right[i][j]=right[i][j+1]+1


#leftの累積和
for i in range(h):
    for j in range(w):
        if j==0:
            if lists[i][j]=="#":
                left[i][j]=0
            else:
                left[i][j]=1
        else:
            if lists[i][j]=="#":
                left[i][j]=0
            else:
                left[i][j]=left[i][j-1]+1


#upの累積和
for i in range(w):
    for j in range(h):
        if j==0:
            if lists[j][i]=="#":
                up[j][i]=0
            else:
                up[j][i]=1
        else:
            if lists[j][i]=="#":
                up[j][i]=0
            else:
                up[j][i]=up[j-1][i]+1

                
#downの累積和
for i in range(w):
    for j in range(h-1,-1,-1):
        if j==h-1:
            if lists[j][i]=="#":
                down[j][i]=0
            else:
                down[j][i]=1
        else:
            if lists[j][i]=="#":
                down[j][i]=0
            else:
                down[j][i]=down[j+1][i]+1
                
maxi=0
for i in range(h):
    for j in range(w):
        maxi=max(maxi,right[i][j]+left[i][j]+up[i][j]+down[i][j]-3)
print(maxi)

Submission status: Time Limit Exceeded

Solution code:
H,W=map(int,input().split())
S=[]
for i in range(H):
    s=input()
    S.append(s)
L=[[0]*W for _ in range(H)]
R=[[0]*W for _ in range(H)]
D=[[0]*W for _ in range(H)]
U=[[0]*W for _ in range(H)]

for i in range(H):
    for j in range(W):
        if S[i][j]==".":
            if j==0:
                L[i][j]=1
            else:
                L[i][j]=L[i][j-1]+1
for i in range(H):
    for j in range(W-1,-1,-1):
        if S[i][j]==".":
            if j==(W-1):
                R[i][j]=1
            else:
                R[i][j]=R[i][j+1]+1

for j in range(W):
    for i in range(H-1,-1,-1):
        if S[i][j]==".":
            if i==(H-1):
                D[i][j]=1
            else:
                D[i][j]=D[i+1][j]+1

for j in range(W):
    for i in range(H):
        if S[i][j]==".":
            if i==0:
                U[i][j]=1
            else:
                U[i][j]=U[i-1][j]+1
ans=0
X=[[0]*W for _ in range(H)]
for i in range(H):
    for j in range(W):
        X[i][j]=L[i][j]+R[i][j]+D[i][j]+U[i][j]-3
    ans=max(ans,max(X[i]))
print(ans)

Submission status: Time Limit Exceeded

Solution code:
H,W=map(int,input().split())
S=[]
for i in range(H):
    s=input()
    S.append(s)
L=[[] for _ in range(H)]
R=[[] for _ in range(H)]
D=[[] for _ in range(H)]
U=[[] for _ in range(H)]
for i in range(H):
    for j in range(W):
        if S[i][j]=="#":
            L[i].append(0)
        else:
            if j==0:
                L[i].append(1)
            else:
                L[i].append(L[i][j-1]+1)
for i in range(H):
    for j in range(W):
        if S[i][W-1-j]=="#":
            R[i].append(0)
        else:
            if j==0:
                R[i].append(1)
            else:
                R[i].append(R[i][j-1]+1)
for i in range(W):
    for j in range(H):
        if S[j][i]=="#":
            U[j].append(0)
        else:
            if j==0:
                U[j].append(1)
            else:
                U[j].append(U[j-1][i]+1)
for i in range(W):
    for j in range(H):
        if S[H-1-j][i]=="#":
            D[j].append(0)
        else:
            if j==0:
                D[j].append(1)
            else:
                D[j].append(D[j-1][i]+1)
ans=0

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

Submission status: Time Limit Exceeded

Solution code:
import math
import copy
from copy import deepcopy
import sys
import fractions
# import numpy as np
from functools import reduce
# import statistics
import heapq
sys.setrecursionlimit(3000)

# input = sys.stdin.readline
# sys.setrecursionlimit(10**6)

# ===FUNCTION===

def getInputInt():
    inputNum = int(input())
    return inputNum


def getInputListInt():
    outputData = []
    inputData = input().split()
    outputData = [int(n) for n in inputData]

    return outputData


def getSomeInputInt(n):
    outputDataList = []
    for i in range(n):
        inputData = int(input())
        outputDataList.append(inputData)

    return outputDataList


def getSomeInputListInt(n):
    inputDataList = []
    outputDataList = []
    for i in range(n):
        inputData = input().split()
        inputDataList = [int(n) for n in inputData]
        outputDataList.append(inputDataList)

    return outputDataList


# ===CODE===
input_num = getInputListInt()

h = input_num[0]
w = input_num[1]

data=[]
for i in range(h):
    tmp = str(input())
    tmp_list = list(tmp)
    data.append(tmp_list)

def saikiU(cnt, i, j):
    if i < 0 or i > h-1: return cnt
    if j < 0 or j > w-1: return cnt
    if data[i][j] == "#": return cnt

    return saikiU(cnt+1, i-1, j)


def saikiD(cnt, i, j):
    if i < 0 or i > h - 1: return cnt
    if j < 0 or j > w - 1: return cnt
    if data[i][j] == "#": return cnt

    return saikiD(cnt + 1, i + 1, j)

def saikiL(cnt, i, j):
    if i < 0 or i > h - 1: return cnt
    if j < 0 or j > w - 1: return cnt
    if data[i][j] == "#": return cnt

    return saikiL(cnt + 1, i, j-1)

def saikiR(cnt, i, j):
    if i < 0 or i > h - 1: return cnt
    if j < 0 or j > w - 1: return cnt
    if data[i][j] == "#": return cnt

    return saikiR(cnt + 1, i, j+1)

ans = 0
for i in range(h):
    for j in range(w):
        if data[i][j] == "#": continue
        tmp_ans = -3
        tmp_ans += saikiU(0, i, j)
        tmp_ans += saikiD(0, i, j)
        tmp_ans += saikiL(0, i, j)
        tmp_ans += saikiR(0, i, j)
        ans = max(ans, tmp_ans)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import math
import copy
from copy import deepcopy
import sys
import fractions
import numpy as np
from functools import reduce
# import statistics
import heapq
sys.setrecursionlimit(3000)

# input = sys.stdin.readline
# sys.setrecursionlimit(10**6)

# ===FUNCTION===

def getInputInt():
    inputNum = int(input())
    return inputNum


def getInputListInt():
    outputData = []
    inputData = input().split()
    outputData = [int(n) for n in inputData]

    return outputData


def getSomeInputInt(n):
    outputDataList = []
    for i in range(n):
        inputData = int(input())
        outputDataList.append(inputData)

    return outputDataList


def getSomeInputListInt(n):
    inputDataList = []
    outputDataList = []
    for i in range(n):
        inputData = input().split()
        inputDataList = [int(n) for n in inputData]
        outputDataList.append(inputDataList)

    return outputDataList


# ===CODE===
input_num = getInputListInt()

h = input_num[0]
w = input_num[1]

data=[]
for i in range(h):
    tmp = str(input())
    tmp_list = list(tmp)
    data.append(tmp_list)

def saikiU(cnt, i, j):
    if i < 0 or i > h-1: return cnt
    if j < 0 or j > w-1: return cnt
    if data[i][j] == "#": return cnt

    return saikiU(cnt+1, i-1, j)


def saikiD(cnt, i, j):
    if i < 0 or i > h - 1: return cnt
    if j < 0 or j > w - 1: return cnt
    if data[i][j] == "#": return cnt

    return saikiD(cnt + 1, i + 1, j)

def saikiL(cnt, i, j):
    if i < 0 or i > h - 1: return cnt
    if j < 0 or j > w - 1: return cnt
    if data[i][j] == "#": return cnt

    return saikiL(cnt + 1, i, j-1)

def saikiR(cnt, i, j):
    if i < 0 or i > h - 1: return cnt
    if j < 0 or j > w - 1: return cnt
    if data[i][j] == "#": return cnt

    return saikiR(cnt + 1, i, j+1)

ans = 0
for i in range(h):
    for j in range(w):
        if data[i][j] == "#": continue
        tmp_ans = -3
        tmp_ans += saikiU(0, i, j)
        tmp_ans += saikiD(0, i, j)
        tmp_ans += saikiL(0, i, j)
        tmp_ans += saikiR(0, i, j)
        ans = max(ans, tmp_ans)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
h,w = map(int,stdin.readline().rstrip().split())
li = [stdin.readline().rstrip() for _ in range(h)]
#各四方向のDPをつくる
L = [[0 for i in range(w)]for j in range(h)]
R = [[0 for i in range(w)]for j in range(h)]
D = [[0 for i in range(w)]for j in range(h)]
U = [[0 for i in range(w)]for j in range(h)]
ma = 0
for i in range(h):
    for j in range(w):
        if li[i][j] == "#":L[i][j] = 0
        elif j == 0:L[i][j] = 1
        else:L[i][j] = L[i][j-1]+1
for i in range(h):
    for j in range(w):
        if li[i][w-j-1] == "#":R[i][w-j-1] = 0
        elif w-j-1 == w-1:R[i][w-j-1] = 1
        else:R[i][w-j-1] = R[i][w-j]+1
for i in range(h):
    for j in range(w):
        if li[i][j] == "#":U[i][j] = 0
        elif i == 0:U[i][j] = 1
        else:U[i][j] = U[i-1][j]+1
for i in range(h):
    for j in range(w):
        if li[h-i-1][j] == "#":D[h-i-1][j] = 0
        elif h-i-1 == h-1:D[h-i-1][j] = 1
        else:D[h-i-1][j] = D[h-i][j]+1
for i in range(h):
    for j in range(w):
        point = L[i][j]+R[i][j]+D[i][j]+U[i][j]-3
        ma = max(ma,point)
print(ma)

Submission status: Time Limit Exceeded

Solution code:
h,w=map(int,input().split())
g=[['#']*(w+2)]
for _ in range(h):
  g.append(list('#'+input()+'#'))
g.append(['#']*(w+2))

ans=0
for i in range(1,h+1):
  for j in range(1,w+1):
    c=0
    if g[i][j]=='.':
      c+=1
      u,d,r,l=0,0,0,0
      while g[i-(u+1)][j]=='.':
        u+=1
        c+=1
      while g[i+(d+1)][j]=='.':
        d+=1
        c+=1
      while g[i][j-(l+1)]=='.':
        l+=1
        c+=1
      while g[i][j+(r+1)]=='.':
        r+=1
        c+=1
    ans=max(ans,c)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
h,w = (int(x) for x in input().split())
ab = []
count = 0
for _ in range(h):
    a = input()
    a = a+'#'
    ab.append(list(a))
ab.append(list('#'*w))
mx = 0

for i in range(h):
    for j in range(w):
        if ab[i][j] == '.':
            dp = [[0 for a in range(w)] for b in range(h)]
            dp[i][j] = 1
            count = 1 
            step = []

            y, x = i, j
            if ab[y+1][x] == '.' :
                step.append([y+1, x])
                dp[y+1][x] = 1
                count += 1
                while len(step)>0:
                    y1,x1=step.pop()
                    if ab[y1+1][x1] == '.' :
                        step.append([y1+1, x1])
                        dp[y1+1][x1] = 1
                        count += 1 
                        
            if ab[y][x+1] == '.' :
                step.append([y, x+1])
                dp[y][x+1] = 1
                count += 1
                while len(step)>0:
                    y1,x1=step.pop()
                    if ab[y1][x1+1] == '.' :
                        step.append([y1, x1+1])
                        dp[y1][x1+1] = 1
                        count += 1
            if ab[y-1][x] == '.' :
                step.append([y-1, x])
                dp[y-1][x] = 1
                count += 1
                while len(step)>0:
                    y1,x1=step.pop()
                    if ab[y1-1][x1] == '.' :
                        step.append([y1-1, x1])
                        dp[y1-1][x1] = 1
                        count += 1
            if ab[y][x-1] == '.' :
                step.append([y, x-1])
                dp[y][x-1] = 1
                count += 1
                while len(step)>0:
                    y1,x1=step.pop()
                    if ab[y1][x1-1] == '.' :
                        step.append([y1, x1-1])
                        dp[y1][x1-1] = 1
                        count += 1

            mx = max([mx, count])
            #print(mx, i, j)

print(mx)

Submission status: Time Limit Exceeded

Solution code:
import itertools
    
h,w = (int(x) for x in input().split())
ab = []
count = 0
for _ in range(h):
    a = input()
    a = a+'#'
    ab.append(list(a))
ab.append(list('#'*w))
mx = 0

for i in range(h):
    for j in range(w):
        if ab[i][j] == '.':
            dp = [[0 for a in range(w)] for b in range(h)]
            dp[i][j] = 1
            step = []

            y, x = i, j
            if ab[y+1][x] == '.' :
                step.append([y+1, x])
                dp[y+1][x] = 1
                while len(step)>0:
                    y1,x1=step.pop()
                    if ab[y1+1][x1] == '.' :
                        step.append([y1+1, x1])
                        dp[y1+1][x1] = 1
                        
            if ab[y][x+1] == '.' :
                step.append([y, x+1])
                dp[y][x+1] = 1
                while len(step)>0:
                    y1,x1=step.pop()
                    if ab[y1][x1+1] == '.' :
                        step.append([y1, x1+1])
                        dp[y1][x1+1] = 1
            if ab[y-1][x] == '.' :
                step.append([y-1, x])
                dp[y-1][x] = 1
                while len(step)>0:
                    y1,x1=step.pop()
                    if ab[y1-1][x1] == '.' :
                        step.append([y1-1, x1])
                        dp[y1-1][x1] = 1
            if ab[y][x-1] == '.' :
                step.append([y, x-1])
                dp[y][x-1] = 1
                while len(step)>0:
                    y1,x1=step.pop()
                    if ab[y1][x1-1] == '.' :
                        step.append([y1, x1-1])
                        dp[y1][x1-1] = 1
            #print(dp)

            mx = max([mx, sum(list(itertools.chain.from_iterable(dp)))])
            #print(mx, i, j)

print(mx)

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=np.int)
R = np.zeros((H,W),dtype=np.int)
U = np.zeros((H,W),dtype=np.int)
D = np.zeros((H,W),dtype=np.int)
for i in range(H):
    for j in range(W):
        if S[i][j] == ".":
            L[i][j] = L[i][j-1]+1
        if S[-i-1][-j-1] == ".":
            R[-i-1][-j-1] = R[-i-1][-j]+1
        if S[i][j] == ".":
            U[i][j] = U[i-1][j]+1
        if S[-i-1][-j-1] == ".":
            D[-i-1][-j-1] = D[-i][-j-1]+1
print(np.max(L+R+U+D)-3)

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=np.int)
R = np.zeros((H,W),dtype=np.int)
U = np.zeros((H,W),dtype=np.int)
D = np.zeros((H,W),dtype=np.int)
for i in range(H):
    for j in range(W):
        if S[i][j] == "#":
            L[i][j] = 0
        else:
            L[i][j] = L[i][j-1]+1
        if S[-i-1][-j-1] == "#":
            R[-i-1][-j-1] = 0
        else:
            R[-i-1][-j-1] = R[-i-1][-j]+1

for i in range(H):
    for j in range(W):
        if S[i][j] == "#":
            None
        else:
            U[i][j] = U[i-1][j]+1
        if S[-i-1][-j-1] == "#":
            None
        else:
            D[-i-1][-j-1] = D[-i][-j-1]+1
print(np.max(L+R+U+D)-3)

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=np.int)
R = np.zeros((H,W),dtype=np.int)
U = np.zeros((H,W),dtype=np.int)
D = np.zeros((H,W),dtype=np.int)
for i in range(H):
    for j in range(W):
        if S[i][j] == "#":
            L[i][j] = 0
        else:
            L[i][j] = L[i][j-1]+1
        if S[-i-1][-j-1] == "#":
            R[-i-1][-j-1] = 0
        else:
            R[-i-1][-j-1] = R[-i-1][-j]+1

for i in range(H):
    for j in range(W):
        if S[i][j] == "#":
            U[i][j] = 0
        else:
            U[i][j] = U[i-1][j]+1
        if S[-i-1][-j-1] == "#":
            D[-i-1][-j-1] = 0
        else:
            D[-i-1][-j-1] = D[-i][-j-1]+1
print(np.max(L+R+U+D)-3)

Submission status: Time Limit Exceeded

Solution code:
h, w = map(int, input().split())
s = [list(input()) for _ in range(h)]
cnt = [[0] * w for _ in range(h)]

def rotate(l_2d):
    l_2d = np.array(l_2d)
    return (l_2d.T).tolist()


# check horizontal direction(done)
done = [[0] * w for _ in range(h)]
for i in range(h):
    for j in range(w):
        if done[i][j] == 1 or s[i][j] == "#":
            continue
        l = 0
        k = j
        while k < w:
            if s[i][k] == ".":
                l += 1
            else:
                break
            k += 1
        for o in range(l):
            cnt[i][j+o] += l
            done[i][j+o] = 1


# check vertical direction
done = [[0] * w for _ in range(h)]
for j in range(w):
    for i in range(h):
        if done[i][j] == 1 or s[i][j] == "#":
            continue
        l = 0
        k = i
        while k < h:
            if s[k][j] == ".":
                l += 1
            else:
                break
            k += 1
        for o in range(l):
            cnt[i+o][j] += l
            done[i+o][j] = 1

print(max(list(map(max, cnt))) - 1)

Submission status: Time Limit Exceeded

Solution code:
import sys
import math
import fractions
import bisect
import queue
import heapq
import collections
import itertools
sys.setrecursionlimit(4100000)

MOD = int(1e9+7)
PI = 3.14159265358979323846264338327950288
INF = 1e18

'''
1行のint=====================================
N, K = map(int, input().split())
N, M = map(int, input().split())
A, B = map(int, input().split())

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

L, R = map(int, input().split())

1行のstring=================================
S, T = input().split()

1行の整数配列================================
P = list(map(int,input().split()))

A = list(map(int,input().split()))
B = list(map(int,input().split()))
C = list(map(int,input().split()))
D = list(map(int,input().split()))

L = list(map(int,input().split()))
R = list(map(int,input().split()))

T = list(map(int,input().split()))

改行あり整数==================================
X = []
Y = []
for i in range(N):
    x1,y1=[int(i) for i in input().split()]
    X.append(x1)
    Y.append(y1)

A = []
for i in range(N):
    a=int(input())
    A.append(a)

Pair = []
for i in range(N):
    x1,y1=[int(i) for i in input().split()]
    Pair.append((x1, y1))
    
2次元整数配列=====================================
A = []
for i in range(N):
    tmp = list(map(int,input().split()))
    A.append(tmp)


N行M列の初期化====================================
dp = [[VAL] * RETU for i in range(GYO)]

'''



H, W = map(int, input().split())
S = []
for i in range(H):
    a=input()
    S.append(a)

# その方向に照らせる個数
right = [[0] * W for i in range(H)]
left = [[0] * W for i in range(H)]
up = [[0] * W for i in range(H)]
down = [[0] * W for i in range(H)]


for h in range(0, H):
    for w in range(1, W):

        if S[h][w-1] == "#" or S[h][w] == "#":
            left[h][w] = 0
        else:
            left[h][w] = left[h][w-1] + 1


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

for h in range(1, H):
    for w in range(0, W):
        if S[h-1][w] == "#" or S[h][w] == "#":
            up[h][w] = 0
        else:
            up[h][w] = up[h-1][w] + 1

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

ans = 0
for h in range(H):
    for w in range(W):
        
        if S[h][w] == "#":
            continue

        ans = max(ans, right[h][w] + left[h][w] + up[h][w] + down[h][w] + 1)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
def read_values():
    return map(int, input().split())


def read_list():
    return list(read_values())


def main():
    H, W = read_values()
    S = [input() for _ in range(H)]
    X = [[0 for _ in range(W)] for __ in range(H)]
    Y = [[0 for _ in range(W)] for __ in range(H)]

    for h in range(H):
        for w in range(W):
            if S[h][w] == ".":
                X[h][w] = 1 if w == 0 else X[h][w - 1] + 1
                Y[h][w] = 1 if h == 0 else Y[h - 1][w] + 1

    res = 0
    for h in range(H - 1, -1, -1):
        for w in range(W - 1, -1, -1):
            if S[h][w] == "#":
                continue
            if w != W - 1:
                X[h][w] = max(X[h][w + 1], X[h][w]) 
            if h != H - 1:
                Y[h][w] = max(Y[h + 1][w], Y[h][w])
            res = max(res, X[h][w] + Y[h][w] - 1)
    print(res)

        
if __name__ == "__main__":
    main()