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
h,w=map(int,sys.stdin.readline().split())
from itertools import *
g=[input() for _ in range(h)]
cw=[[] for _ in range(h)]
for i in range(h):
  gw=[(s,len(list(c))) for s,c in groupby(g[i])]
  for s,c in gw:
    if s=='.': cw[i]+=[c]*c
    else: cw[i]+=[0]*c
gt=list(zip(*g))
ch=[[] for _ in range(w)]
for i in range(w):
  gh=[(s,len(list(c))) for s,c in groupby(gt[i])]
  for s,c in gh:
    if s=='.': ch[i]+=[c]*c
    else: ch[i]+=[0]*c
ch=list(zip(*ch))
print(max(cw[i][j]+ch[i][j]-1 for i in range(h) for j in range(w)))

Submission status: Time Limit Exceeded

Solution code:
h,w=map(int,input().split())
from itertools import *
g=[input() for _ in range(h)]
cw=[[] for _ in range(h)]
for i in range(h):
  gw=[(s,len(list(c))) for s,c in groupby(g[i])]
  for s,c in gw:
    if s=='.': cw[i]+=[c]*c
    else: cw[i]+=[0]*c
gt=list(zip(*g))
ch=[[] for _ in range(w)]
for i in range(w):
  gh=[(s,len(list(c))) for s,c in groupby(gt[i])]
  for s,c in gh:
    if s=='.': ch[i]+=[c]*c
    else: ch[i]+=[0]*c
ch=list(zip(*ch))
print(max(cw[i][j]+ch[i][j]-1 for i in range(h) for j in range(w)))

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
h, w = map(int, input().split())
maze = [list(input()) for i in range(h)]
ans = 0

# (px, py)にライトを置いた時照らされるマスを返す関数
def lightdot(px, py):
    dx = [1, 0, -1, 0]
    dy = [0, 1, 0, -1]
    # それぞれ上、下、左、右に移動していく
    pos = deque([[px, py, 0, 0], [px, py, 1, 0], [px, py, 2, 0], [px, py, 3, 0]])
    res = 0

    while len(pos) > 0:
        x, y, direct, depth = pos.popleft()
        nx = x + dx[direct]
        ny = y + dy[direct]
        if 0 <= nx < w and 0 <= ny < h and maze[ny][nx] == '.':
            # 方向は引き継ぐ
            pos.append([nx, ny, direct, depth + 1])
        else:
            res += depth
    # 現在地点の'.'も含める
    return res + 1

for i in range(w):
    for j in range(h):
        if maze[j][i] == '.':
            ans = max(ans, lightdot(i, j))
print(ans)

Submission status: Time Limit Exceeded

Solution code:
H, W = map(int, input().split())
grid = []
for _ in range(H):
    grid.append([s for s in input()])
    
left = [[0 for _ in range(W)] for _ in range(H)]
right = [[0 for _ in range(W)] for _ in range(H)]
up = [[0 for _ in range(W)] for _ in range(H)]
down = [[0 for _ in range(W)] for _ in range(H)]

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

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

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

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

ans = -10000
for h in range(H):
    for w in range(W):
        ans = max(ans, up[h][w] + down[h][w] + right[h][w] + left[h][w] - 3)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
H, W = map(int, input().split())
grid = []
for _ in range(H):
    grid.append([s for s in input()])
    
left = [[0 for _ in range(W)] for _ in range(H)]
right = [[0 for _ in range(W)] for _ in range(H)]
up = [[0 for _ in range(W)] for _ in range(H)]
down = [[0 for _ in range(W)] for _ in range(H)]

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

for h in range(H):
    for w in range(W)[::-1]:
        if grid[h][w] == ".":
            if w == W - 1:
                right[h][w] = 1
            else:
                right[h][w] += right[h][w + 1] + 1
        else:
            right[h][w] = 0
            
for w in range(W):
    for h in range(H):
        if grid[h][w] == ".":
            if h == 0:
                up[h][w] = 1
            else:
                up[h][w] += up[h - 1][w] + 1
        else:
            up[h][w] = 0

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

ans = -10000
for h in range(H):
    for w in range(W):
        ans = max(ans, up[h][w] + down[h][w] + right[h][w] + left[h][w] - 3)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
H, W = map(int, input().split())
grid = []
for _ in range(H):
    grid.append([s for s in input()])
    
left = [[0 for _ in range(W)] for _ in range(H)]
right = [[0 for _ in range(W)] for _ in range(H)]
up = [[0 for _ in range(W)] for _ in range(H)]
down = [[0 for _ in range(W)] for _ in range(H)]

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

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

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

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

ans = -10000
for h in range(H):
    for w in range(W):
        ans = max(ans, up[h][w] + down[h][w] + right[h][w] + left[h][w] - 3)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def main():
    import sys
    input = sys.stdin.readline

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

    def wallen(g):
        L = len(g[0])
        ret = []
        ret.append('#' * (L + 2))
        for row in g:
            t = '#' + row + '#'
            ret.append(t)
        ret.append('#' * (L + 2))
        return ret

    g = wallen(s)
    # print(*g, sep='\n')

    left = [[-1] * (W + 2) for _ in range(H + 2)]  # 各方向に移動したときにぶつかる障害物の位置
    right = [[-1] * (W + 2) for _ in range(H + 2)]
    up = [[-1] * (W + 2) for _ in range(H + 2)]
    down = [[-1] * (W + 2) for _ in range(H + 2)]

    for r in range(H + 2):
        for c in range(W + 2):
            if g[r][c] == '#':
                left[r][c] = c
            else:
                left[r][c] = left[r][c - 1]

        for c in reversed(range(W + 2)):
            if g[r][c] == '#':
                right[r][c] = c
            else:
                right[r][c] = right[r][c + 1]

    for c in range(W + 2):
        for r in range(H + 2):
            if g[r][c] == '#':
                up[r][c] = r
            else:
                up[r][c] = up[r - 1][c]

        for r in reversed(range(H + 2)):
            if g[r][c] == '#':
                down[r][c] = r
            else:
                down[r][c] = down[r + 1][c]

    ans = 0
    for r in range(H + 2):
        for c in range(1, W + 1):
            t = right[r][c] - left[r][c] + down[r][c] - up[r][c] - 3
            ans = max(ans, t)

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import numpy as np 
H, W = map(int, input().split())
grid = []
for _ in range(H):
    grid.append([s for s in input()])

yoko = [[0 for _ in range(W)] for _ in range(H)]
tate =  [[0 for _ in range(W)] for _ in range(H)]

for h in range(H):
    cnt = 0
    for w in range(W):
        if grid[h][w] == ".":
            cnt += 1
        else:
            cnt = 0
        yoko[h][w] = cnt

for w in range(W):
    cnt = 0
    for h in range(H):
        if grid[h][w] == ".":
            cnt += 1
        else:
            cnt = 0
        tate[h][w] = cnt

tate = np.array(tate)
yoko = np.array(yoko)

for h in range(H):
    my_max = 0
    for w in range(W):
        if yoko[h][W - 1 - w] != 0:
            my_max = max(my_max, yoko[h][W - 1 - w])
            yoko[h][W - 1 - w] = my_max
        else:
            my_max = 0

tate = tate.T

for h in range(W):
    my_max = 0
    for w in range(H):
        if tate[h][H - 1 - w] != 0:
            my_max = max(my_max, tate[h][H - 1 - w])
            tate[h][H - 1 - w] = my_max
        else:
            my_max = 0
            
tate = tate.T

ans = -10000
for h in range(H):
    for w in range(W):
        ans = max(ans, tate[h][w] + yoko[h][w] - 1)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np 
H, W = map(int, input().split())
grid = []
for _ in range(H):
    grid.append([s for s in input()])

yoko = [[0 for _ in range(W)] for _ in range(H)]
tate =  [[0 for _ in range(W)] for _ in range(H)]

for h in range(H):
    cnt = 0
    for w in range(W):
        if grid[h][w] == ".":
            cnt += 1
        else:
            cnt = 0
        yoko[h][w] = cnt

for w in range(W):
    cnt = 0
    for h in range(H):
        if grid[h][w] == ".":
            cnt += 1
        else:
            cnt = 0
        tate[h][w] = cnt

tate = np.array(tate)
yoko = np.array(yoko)

for h in range(H):
    my_max = 0
    for w in range(W):
        if yoko[h][W - 1 - w] != 0:
            my_max = max(my_max, yoko[h][W - 1 - w])
            yoko[h][W - 1 - w] = my_max
        else:
            my_max = 0

tate = tate.T

for h in range(W):
    my_max = 0
    for w in range(H):
        if tate[h][H - 1 - w] != 0:
            my_max = max(my_max, tate[h][H - 1 - w])
            tate[h][H - 1 - w] = my_max
        else:
            my_max = 0
            
tate = tate.T
ans = tate + yoko - 1

print(np.max(ans.ravel()))

Submission status: Time Limit Exceeded

Solution code:
def main():
    H, W = (int(i) for i in input().split())
    A = [input() for i in range(H)]
    L = [[[0]*W for _ in range(H)] for _ in range(4)]
    for di in range(4):
        if di == 0 or di == 1:
            rangeH = range(H)
            rangeW = range(W)
        else:
            rangeH = range(H)[::-1]
            rangeW = range(W)[::-1]

        for h in rangeH:
            for w in rangeW:
                if A[h][w] == "#":
                    continue
                if di == 0:
                    if h-1 < 0 or A[h-1][w] == "#":
                        L[di][h][w] = 1
                    else:
                        L[di][h][w] = L[di][h-1][w] + 1
                elif di == 1:
                    if w-1 < 0 or A[h][w-1] == "#":
                        L[di][h][w] = 1
                    else:
                        L[di][h][w] = L[di][h][w-1] + 1
                elif di == 2:
                    if h+1 > H-1 or A[h+1][w] == "#":
                        L[di][h][w] = 1
                    else:
                        L[di][h][w] = L[di][h+1][w] + 1
                elif di == 3:
                    if w+1 > W-1 or A[h][w+1] == "#":
                        L[di][h][w] = 1
                    else:
                        L[di][h][w] = L[di][h][w+1] + 1
    ans = 0
    for h in range(H):
        for w in range(W):
            v = sum(L[i][h][w] for i in range(4)) - 3
            ans = max(ans, v)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
H, W = map(int, input().split())
grid = []
for _ in range(H):
    grid.append([s for s in input()])

def dfs(h, w):
    cnt = 0
    if grid[h][w] == "#":
        return cnt
    q = deque([(h, w)])
    cnt += 1
    h, w = q.pop()
    for i, j in [(0, 1), (1, 0), (0, -1), (-1, 0)]:
        if 0 <= h + i < H and 0 <= w + j < W and grid[h + i][w + j] == ".":
                cnt += 1
                q.append((h + i, w + j, (i, j)))
    while q:
        h, w, direct = q.pop()
        i, j = direct
        if 0 <= h + i < H and 0 <= w + j < W and grid[h + i][w + j] == ".":
            cnt += 1
            q.append((h + i, w + j, (i, j)))
 
    return cnt 
    
ans = 0
for h in range(H):
    for w in range(W):
        ans = max(ans, dfs(h, w))
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
H, W = map(int, input().split())
grid = []
for _ in range(H):
    grid.append([s for s in input()])

def dfs(h, w):
    cnt = 0
    if grid[h][w] == "#":
        return cnt
    q = deque([(h, w, "No")])
    cnt += 1
    while q:
        h, w, direct = q.pop()
        for i, j in [(0, 1), (1, 0), (0, -1), (-1, 0)]:
            if direct == "No" or direct == (i, j):
                if 0 <= h + i < H and 0 <= w + j < W and grid[h + i][w + j] == ".":
                        cnt += 1
                        q.append((h + i, w + j, (i, j)))
 
    return cnt 
    
ans = 0
for h in range(H):
    for w in range(W):
        ans = max(ans, dfs(h, w))
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
H, W = map(int, input().split())
grid = []
for _ in range(H):
    grid.append([s for s in input()])

def dfs(h, w):
    cnt = 0
    if grid[h][w] == "#":
        return cnt
    q = deque([(h, w, "No")])
    cnt += 1
    while q:
        h, w, direct = q.pop()
        for i, j in [(0, 1), (1, 0), (0, -1), (-1, 0)]:
            if 0 <= h + i < H and 0 <= w + j < W and grid[h + i][w + j] == ".":
                if direct == "No" or direct == (i, j):
                    cnt += 1
                    q.append((h + i, w + j, (i, j)))
            else:
                continue
    return cnt 
    
ans = 0
for h in range(H):
    for w in range(W):
        ans = max(ans, dfs(h, w))
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
H, W = map(int, input().split())
grid = []
for _ in range(H):
    grid.append([s for s in input()])

def dfs(h, w):
    done = [[0 for _ in range(W)] for _ in range (H)]
    cnt = 0
    if grid[h][w] == "#":
        return cnt
    q = deque([(h, w, "No")])
    cnt += 1
    while q:
        h, w, direct = q.pop()
        for i, j in [(0, 1), (1, 0), (0, -1), (-1, 0)]:
            if 0 <= h + i < H and 0 <= w + j < W and grid[h + i][w + j] == "." and done[h + i][w + j] == 0:
                if direct == "No" or direct == (i, j):
                    cnt += 1
                    q.append((h + i, w + j, (i, j)))
                    done[h + i][w + j] = 1
    return cnt 
    
ans = 0
for h in range(H):
    for w in range(W):
        ans = max(ans, dfs(h, w))
print(ans)

Submission status: Time Limit Exceeded

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

grid = [list(input()) for _ in range(h)]
left = [[0 for _ in range(w)] for _ in range(h)]
right = [[0 for _ in range(w)] for _ in range(h)]
up = [[0 for _ in range(w)] for _ in range(h)]
down = [[0 for _ in range(w)] for _ in range(h)]

for i in range(h):
    current = 0
    for j in range(w):
        if grid[i][j] == '#':
            current = 0
        else:
            current += 1
        left[i][j] = current
    
for i in range(h):
    current = 0
    for j in range(w - 1, -1, -1):
        if grid[i][j] == '#':
            current = 0
        else:
            current += 1
        right[i][j] = current

for i in range(w):
    current = 0
    for j in range(h):
        if grid[j][i] == '#':
            current = 0
        else:
            current += 1
        up[j][i] = current

for i in range(w):
    current = 0
    for j in range(h - 1, -1, -1):
        if grid[j][i] == '#':
            current = 0
        else:
            current += 1
        down[j][i] = current

ans = 0
for i in range(h):
    for j in range(w):
        if grid[i][j] == '#':
            continue
        ans = max(ans, left[i][j] + right[i][j] + up[i][j] + down[i][j] - 3)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
h,w=map(int,input().split())
M=[]
r=[]
for i in range(h):
  M.append(input())
ans=[[[-1 for x in range(2)] for y in range(w)] for z in range(h)]
max=0
for i in range(h):
  for j in range(w):
    if M[i][j]=='#':
      continue
    else:
      temp=1
      if ans[i][j][0]==-1:
        for k in range(i+1,h):
          if M[k][j]=='.':
            temp+=1
          else:
            break
        for k in range(i,i+temp):
          ans[k][j][0]=temp
      if ans[i][j][1]==-1:
        temp=1
        for k in range(j+1,w):
          if M[i][k]=='.':
            temp+=1
          else:
            break
        for k in range(j,j+temp):
          ans[i][k][1]=temp
      if ans[i][j][0]+ans[i][j][1]-1>max:
        max=ans[i][j][0]+ans[i][j][1]-1
print(max)

Submission status: Time Limit Exceeded

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

U = [[0] * W for _ in range(H)]
L = [[0] * W for _ in range(H)]
D = [[0] * W for _ in range(H)]
R = [[0] * W for _ in range(H)]

for h in range(H):
    for w in range(W):
        if A[h][w] == '#':
            U[h][w] = 0
            L[h][w] = 0
        else:
            U[h][w] = 1 + (U[h - 1][w] if h > 0 else 0)
            L[h][w] = 1 + (L[h][w - 1] if w > 0 else 0)

        y = H - h - 1
        x = W - w - 1
        if A[y][x] == '#':
            D[y][x] = 0
            R[y][x] = 0
        else:
            D[y][x] = 1 + (D[y + 1][x] if y < H - 1 else 0)
            R[y][x] = 1 + (R[y][x + 1] if x < W - 1 else 0)

ans = 0
for h in range(H):
    for w in range(W):
        if A[h][w] == '.':
            ans = max(ans, U[h][w] + D[h][w] + L[h][w] + R[h][w] - 3)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
h,w=map(int,input().split())
M=[]
r=[]
for i in range(h):
  M.append(input())
ans=np.zeros((h, w, 2), dtype=np.int) - 1
max=0
for i in range(h):
  for j in range(w):
    if M[i][j]=='#':
      continue
    else:
      temp=1
      if ans[i][j][0]==-1:
        for k in range(i+1,h):
          if M[k][j]=='.':
            temp+=1
          else:
            break
        for k in range(i,i+temp):
          ans[k][j][0]=temp
      if ans[i][j][1]==-1:
        temp=1
        for k in range(j+1,w):
          if M[i][k]=='.':
            temp+=1
          else:
            break
        for k in range(j,j+temp):
          ans[i][k][1]=temp
      if ans[i][j][0]+ans[i][j][1]-1>max:
        max=ans[i][j][0]+ans[i][j][1]-1
print(max)

Submission status: Time Limit Exceeded

Solution code:
H,W = map(int,input().split())
S = [input().strip() for _ in range(H)]
A = [[[0,0] for _ in range(W)] for _ in range(H)]
for i in range(H):
    cnt = 0
    for j in range(W):
        if S[i][j]=="#":
            cnt = 0
        else:
            cnt += 1
            A[i][j][0] = cnt
for i in range(H):
    for j in range(W-1,-1,-1):
        if S[i][j]=="." and j+1<W:
            A[i][j][0] = max(A[i][j][0],A[i][j+1][0])
for j in range(W):
    cnt = 0
    for i in range(H):
        if S[i][j]=="#":
            cnt = 0
        else:
            cnt += 1
            A[i][j][1] = cnt
cmax = 0
for j in range(W):
    for i in range(H-1,-1,-1):
        if S[i][j]=="." and i+1<H:
            A[i][j][1] = max(A[i][j][1],A[i+1][j][1])
        cmax = max(cmax,A[i][j][0]+A[i][j][1]-1)
print(cmax)

Submission status: Time Limit Exceeded

Solution code:
def main():
    H, W = map(int, input().split())
    grid = [list(input()) for _ in range(H)]
    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 h in range(H):
        for w in range(W):
            if grid[h][w] == '#':
                continue
            if w == 0:
                L[h][w] = 1
            else:
                L[h][w] = L[h][w-1] + 1
            if h == 0:
                U[h][w] = 1
            else:
                U[h][w] = U[h-1][w] + 1
    for h in range(H - 1, -1, -1):
        for w in range(W - 1, -1, -1):
            if grid[h][w] == '#':
                continue
            if w == W - 1:
                R[h][w] = 1
            else:
                R[h][w] = R[h][w + 1] + 1
            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:
import queue
h, w=map(int, input().split())
grid = []
dydx = [[0, 1], [1,0], [-1, 0], [0, -1]]
done = [[0]*w for _ in range(h)]
que = queue.Queue()
res=0

for i in range(h):
    s = list(input().rstrip())
    grid.append(s)

def search(i , j, vec):
    t=0
    while i+vec[0]<h and j+vec[1]<w and i+vec[0]>-1 and j+vec[1]>-1 and grid[i+vec[0]][j+vec[1]]==".":
        i+=vec[0]
        j+=vec[1]
        t+=1
    return t;

for i in range(h):
    for j in range(w):
        if(grid[i][j]!="#") and not done[i][j]:
            tmp = 1
            for vec in dydx:
                tmp+=search(i, j, vec)

            res = max(res, tmp)

print(res)

Submission status: Time Limit Exceeded

Solution code:
H,W=map(int, input().split())
S=[]
for h in range(H):
    s=list(input())
    S.append(s)
ans=0
for h in range(H):
    for w in range(W):
        if S[h][w]==".":
            count=1
            que=[(h,w,"l"),(h,w,"u"),(h,w,"r"),(h,w,"d")]
            while len(que)>0:
                t=que.pop(0)
                h2,w2,to=t
                if to=="l":
                    nh=h2
                    nw=w2-1
                elif to=="u":
                    nh=h2+1
                    nw=w2
                elif to=="r":
                    nh=h2
                    nw=w2+1
                else:
                    nh=h2-1
                    nw=w2
                if 0<=nh<H and 0<=nw<W:
                    if S[nh][nw]==".":
                        count+=1
                        que.append((nh,nw,to))
            ans=max(ans, count)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
H,W = map(int,input().split())
S = [input().strip() for _ in range(H)]
A = [[[0,0] for _ in range(W)] for _ in range(H)]
for i in range(H):
    cnt = 0
    for j in range(W):
        if S[i][j]=="#":
            cnt = 0
        else:
            cnt += 1
            A[i][j][0] = cnt
for i in range(H):
    for j in range(W-1,-1,-1):
        if S[i][j]=="." and j+1<W:
            A[i][j][0] = max(A[i][j][0],A[i][j+1][0])
for j in range(W):
    cnt = 0
    for i in range(H):
        if S[i][j]=="#":
            cnt = 0
        else:
            cnt += 1
            A[i][j][1] = cnt
for j in range(W):
    for i in range(H-1,-1,-1):
        if S[i][j]=="." and i+1<H:
            A[i][j][1] = max(A[i][j][1],A[i+1][j][1])
B = [[0 for _ in range(W)] for _ in range(H)]
cmax = 0
for i in range(H):
    for j in range(W):
        if S[i][j]==".":
            B[i][j] = A[i][j][0]+A[i][j][1]-1
            cmax = max(cmax,B[i][j])
print(cmax)

Submission status: Time Limit Exceeded

Solution code:
h, w = map(int, input().split())
s = []
for i in range(h):
    s.append(list(input()))
lr = [[0] * w for i in range(h)]
#print(lr)
for i in range(h):
    cnt = 0
    lis = []
    for j in range(w):

        if s[i][j] == '#':
            lis.append(cnt)
            cnt = 0
        elif j == w-1:
            if s[i][j] == '.':
                cnt += 1
            lis.append(cnt)
        else:
            cnt += 1
    p = 0
    #print(lis)
    for j in range(w):
        if s[i][j] == '.':
            #print(p)
            #print(lr)
            lr[i][j] = lis[p]
        else:
            p += 1
#print(lr)

ud = [[0] * w for i in range(h)]
#print(ud)
for j in range(w):
    cnt = 0
    lis = []
    for i in range(h):
        #print(s[i][j])
        if s[i][j] == '#':
            lis.append(cnt)
            cnt = 0
        elif i == h-1:
            if s[i][j] == '.':
                cnt += 1
            lis.append(cnt)
        else:
            cnt += 1
    p = 0
    #print(lis)
    for i in range(h):
        #print(p,i,j,)
        #print(lis[p])
        #print(ud[j][i])
        if s[i][j] == '.':
            #print(p)
            #print(lr)
            ud[i][j] = lis[p]
        else:
            p += 1
        #print(p)
#print(ud)
ans = 0
for i in range(h):
    for j in range(w):
        ans = max(ans, lr[i][j] + ud[i][j] - 1)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
H,W = map(int,input().split())
S = [input().strip() for _ in range(H)]
A = [[[0,0] for _ in range(W)] for _ in range(H)]
for i in range(H):
    cur = 0
    cnt = 0
    for j in range(W):
        if S[i][j]=="#":
            if cnt>0:
                for k in range(cur,j):
                    A[i][k][0] = cnt
            cur=j+1
            cnt = 0
        else:
            cnt += 1
    if cnt>0:
        for k in range(cur,W):
            A[i][k][0] = cnt
for j in range(W):
    cur = 0
    cnt = 0
    for i in range(H):
        if S[i][j]=="#":
            if cnt>0:
                for k in range(cur,i):
                    A[k][j][1] = cnt
            cur = i+1
            cnt = 0
        else:
            cnt += 1
    if cnt>0:
        for k in range(cur,H):
            A[k][j][1] = cnt
B = [[0 for _ in range(W)] for _ in range(H)]
cmax = 0
for i in range(H):
    for j in range(W):
        if S[i][j]==".":
            B[i][j] = A[i][j][0]+A[i][j][1]-1
            cmax = max(cmax,B[i][j])
print(cmax)

Submission status: Time Limit Exceeded

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

m = [[0 for i in range(W)] for j in range(H)]
#yoko
for i in range(H):
  s = S[i]
  mar = 0
  cnt = 0
  for j in range(W):
    if s[j] == ".":
      cnt += 1
    if s[j] == "#":
      for k in range(mar,j):
        m[i][k] += cnt
      cnt = 0
      mar = j+1    
    #グリッドの端の処理
    if j == W-1 and s[j] == ".":
      for k in range(mar,j+1):
        m[i][k] += cnt


#tate      
for i in range(W):
  mar = 0
  cnt = 0
  for j in range(H):
    if S[j][i] == ".":
      cnt += 1
    if S[j][i] == "#":
      for k in range(mar,j):
        m[k][i] += cnt
      cnt = 0
      mar = j+1 
    #グリッドの端の処理
    if j == H-1 and S[j][i] == ".":
      for k in range(mar,j+1):
        m[k][i] += cnt


ans = 0
for i in range(H):
  ans = max(ans,max(m[i]))
print(ans-1)

Submission status: Time Limit Exceeded

Solution code:
h, w = map(int,input().split())
s = [list(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 i in range(h):
    for j in range(w):
        temp_L = s[i][j]
        temp_R = s[i][-(j + 1)]
        temp_U = s[i][j]
        temp_D = s[-(i + 1)][j]

        if temp_L == '#':L[i][j] = 0
        else:
            if j == 0:L[i][j] = 1
            else:L[i][j] = L[i][j - 1] + 1

        if temp_R == '#':R[i][-(j + 1)] = 0
        else:
            if j == 0:R[i][-(j + 1)] = 1
            else:R[i][-(j + 1)] = R[i][-(j + 1) + 1] + 1

        if temp_U == '#':U[i][j] = 0
        else:
            if i == 0:U[i][j] = 1
            else:U[i][j] = U[i - 1][j] + 1

        if temp_D == '#':D[-(i + 1)][j] = 0
        else:
            if i == 0:D[-(i + 1)][j] = 1
            else:D[-(i + 1)][j] = D[-(i + 1) + 1][j] + 1

ans = 0
for i in range(h):
    for j in range(w):
        if s[i][j] == '#':continue
        temp = L[i][j] + R[i][j] + U[i][j] + D[i][j] - 3
        if ans < temp:ans = temp
print(ans)
#print(L,R,U,D,sep ='\n')

Submission status: Time Limit Exceeded

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

dx = [[0]*W for _ in range(H)]
dy = [[0]*H for _ in range(W)]

Sy = [[0]*H for _ in range(W)]
for i in range(H):
    for j in range(W):
        Sy[j][i] = S[i][j]
for i,s in enumerate(S):
    tmp = []
    prev = '#'
    start = -1
    for j,char in enumerate(s):
        if char == '#':
            if prev == '.':
                val = j-start-1
                for k in range(start+1, j):
                    dx[i][k] = val
                prev = char
            start = j
        prev = char
    if start == 0:
        dx[i] = [W]*W
    elif prev == '.':
        val = W - start - 1
        for k in range(start + 1, W):
            dx[i][k] = val
for i,s in enumerate(Sy):
    tmp = []
    prev = '#'
    start = -1
    for j,char in enumerate(s):
        if char == '#':
            if prev == '.':
                val = j-start-1
                for k in range(start+1, j):
                    dy[i][k] = val
                prev = char
            start = j
        prev = char
    if start == 0:
        dy[i] = [H]*H
    elif prev == '.':
        val = H - start - 1
        for k in range(start + 1, H):
            dy[i][k] = val

res = 0
for i in range(H):
    for j in range(W):
        tmp = dx[i][j] + dy[j][i]
        if tmp > res:
            res = tmp
print(res-1)