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:
from collections import Counter 
from collections import defaultdict
from collections import deque
import math
import itertools
import heapq
import numpy as np
import sys
sys.setrecursionlimit(10**6)

#n=int(input())
#n,m=list(map(int,input().split()))
#a=list(map(int,input().split()))
input_list = lambda : list(map(int,input().split()))

h,w=input_list()
a=[[s for s in  input()] for _ in range(h)]

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

a=np.array(a)
l=np.array(l)
r=np.array(r)
u=np.array(u)
d=np.array(d)
#left
for w_i in range(1,w):
    for h_i in range(h):

        if a[h_i][w_i]=="#" or a[h_i][w_i-1]=="#":
            continue
        l[h_i][w_i]=l[h_i][w_i-1]+1
    
#right
for w_i in range(w-2,-1,-1):
    for h_i in range(h):

        if a[h_i][w_i]=="#" or a[h_i][w_i+1]=="#":
            continue
        r[h_i][w_i]=r[h_i][w_i+1]+1


#up
for w_i in range(w):
    for h_i in range(1,h):

        if a[h_i][w_i]=="#" or a[h_i-1][w_i]=="#":
            continue
        u[h_i][w_i]=u[h_i-1][w_i]+1


#down
for w_i in range(w):
    for h_i in range(h-2,-1,-1):

        if a[h_i][w_i]=="#" or a[h_i+1][w_i]=="#":
            continue
        d[h_i][w_i]=d[h_i+1][w_i]+1
ans=0
for w_i in range(w):
    for h_i in range(h):
        if a[h_i][w_i]=="#":
            continue
        cal=l[h_i][w_i]+r[h_i][w_i]+u[h_i][w_i]+d[h_i][w_i]+1
        ans=max(ans,cal)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys


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


MOD = 998244353


def main():
    H, W = map(int, input().split())
    S = [input() for _ in range(H)]
    height = [[0] * W for _ in range(H)]
    width = [[0] * W for _ in range(H)]

    for h in range(H):
        cnt = 0
        for w in range(W):
            if S[h][w] == ".":
                cnt += 1
            else:
                width[h][w - cnt : w] = [cnt] * cnt
                cnt = 0
        width[h][W - cnt : W] = [cnt] * cnt
    for w in range(W):
        cnt = 0
        for h in range(H):
            if S[h][w] == ".":
                cnt += 1
            else:
                for i in range(cnt):
                    height[h - cnt + i][w] = cnt
                cnt = 0
        for i in range(cnt):
            height[H - cnt + i][w] = cnt
    answer = 0
    for h in range(H):
        for w in range(W):
            ans = width[h][w] + height[h][w] - 1
            if ans > answer:
                answer = ans
    print(answer)


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    H, W = map(int, input().split())
    S = [list(input()) for _ in range(H)]
    left = [[0] * W for _ in range(H)]
    right = [[0] * W for _ in range(H)]
    up = [[0] * W for _ in range(H)]
    down = [[0] * W for _ in range(H)]
    for i in range(H):
        cnt = 0
        for j in range(W):
            if S[i][j] == '#':
                cnt = 0
            else:
                cnt += 1
            left[i][j] = cnt
    
    for i in range(H):
        cnt = 0
        for j in range(W-1,-1,-1):
            if S[i][j] == '#':
                cnt = 0
            else:
                cnt += 1
            right[i][j] = cnt
    
    for j in range(W):
        cnt = 0
        for i in range(H):
            if S[i][j] == '#':
                cnt = 0
            else:
                cnt += 1
            up[i][j] = cnt
    
    for j in range(W):
        cnt = 0
        for i in range(H-1,-1,-1):
            if S[i][j] == '#':
                cnt = 0
            else:
                cnt += 1
            down[i][j] = cnt
    
    ans = 0
    for i in range(H):
        for j in range(W):
            tmp = up[i][j] + down[i][j] + left[i][j] + right[i][j] - 3
            ans = max(ans, tmp)


    print(ans)
    


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
# https://atcoder.jp/contests/abc129/tasks/abc129_d
from copy import deepcopy
def main():
    h, w = map(int, input().split())
    matrix = []
    for _ in range(h):
        row = list(input())
        matrix.append(row)

    left = [[0] * w for _ in range(h)] # 左から右
    right = [[0] * w for _ in range(h)] # 右から左
    up = [[0] * w for _ in range(h)] # 上から下
    down = [[0] * w for _ in range(h)] # 下から上

    for i in range(h):
        c = 0
        for j in range(w):
            if matrix[i][j] == '.':
                c += 1
            else:
                c = 0
            left[i][j] = c
    for i in range(h):
        c = 0
        for j in range(w)[::-1]:
            if matrix[i][j] == '.':
                c += 1
            else:
                c = 0
            right[i][j] = c
    for j in range(w):
        c = 0
        for i in range(h):
            if matrix[i][j] == '.':
                c += 1
            else:
                c = 0
            up[i][j] = c
    for j in range(w):
        c = 0
        for i in range(h)[::-1]:
            if matrix[i][j] == '.':
                c += 1
            else:
                c = 0
            down[i][j] = c
    ans = 1
    for i in range(h):
        for j in range(w):
            t = left[i][j] + right[i][j] + up[i][j] + down[i][j] - 3
            ans = max(ans, t)
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
# https://atcoder.jp/contests/abc129/tasks/abc129_d
from copy import deepcopy
def main():
    h, w = map(int, input().split())

    matrix = []
    matrix.append([0] * (w + 1))
    for _ in range(h):
        row = [1 if x == '.' else 0 for x in input()]
        row = [0] + row
        matrix.append(row)
    matrix.append([0] * (w + 1))

    left = deepcopy(matrix) # 左から右
    right = deepcopy(matrix) # 右から左
    up = deepcopy(matrix) # 上から下
    down = deepcopy(matrix) # 下から上

    for i in range(1, h + 1):
        for j in range(1, w + 1):
            if left[i][j]:
                left[i][j] += left[i][j - 1]
    for i in range(1, h + 1):
        for j in range(1, w + 1)[::-1]:
            if right[i][j - 1]:
                right[i][j - 1] += right[i][j]
    for i in range(1, h + 1):
        for j in range(1, w + 1):
            if up[i][j]:
                up[i][j] += up[i - 1][j]
    for i in range(1, h + 1)[::-1]:
        for j in range(1, w + 1):
            if down[i - 1][j]:
                down[i - 1][j] += down[i][j]

    ans = 1
    for i in range(h + 1):
        for j in range(w + 1):
            t = left[i][j] + right[i][j] + up[i][j] + down[i][j] - 3
            ans = max(ans, t)
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
# https://atcoder.jp/contests/abc129/tasks/abc129_d
from copy import deepcopy
h, w = map(int, input().split())

matrix = []
matrix.append([0] * (w + 1))
for _ in range(h):
    row = [1 if x == '.' else 0 for x in input()]
    row = [0] + row
    matrix.append(row)
matrix.append([0] * (w + 1))

left = deepcopy(matrix) # 左から右
right = deepcopy(matrix) # 右から左
up = deepcopy(matrix) # 上から下
down = deepcopy(matrix) # 下から上

for i in range(1, h + 1):
    for j in range(1, w + 1):
        if left[i][j]:
            left[i][j] += left[i][j - 1]
for i in range(1, h + 1):
    for j in range(1, w + 1)[::-1]:
        if right[i][j - 1]:
            right[i][j - 1] += right[i][j]
for i in range(1, h + 1):
    for j in range(1, w + 1):
        if up[i][j]:
            up[i][j] += up[i - 1][j]
for i in range(1, h + 1)[::-1]:
    for j in range(1, w + 1):
        if down[i - 1][j]:
            down[i - 1][j] += down[i][j]

ans = 1
for i in range(h + 1):
    for j in range(w + 1):
        t = left[i][j] + right[i][j] + up[i][j] + down[i][j] - 3
        ans = max(ans, t)
print(ans)

Submission status: Time Limit Exceeded

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

for i in range(h):
    lis.append(input())

mtx = [[[0] * 2 for i in range(w)] for j in range(h)]

for i in range(len(lis)):
    cnt = 0
    start = 0
    for j in range(w):
        if lis[i][j] == '.':
            cnt += 1
            if j == w - 1:
                for k in range(start, w):
                    mtx[i][k][0] = cnt
        else:
            for k in range(start, j):
                mtx[i][k][0] = cnt
            start = j + 1
            cnt = 0

for i in range(w):
    cnt = 0
    start = 0
    for j in range(h):
        if lis[j][i] == '.':
            cnt += 1
            if j == h - 1:
                for k in range(start, h):
                    mtx[k][i][1] = cnt
        else:
            for k in range(start, j):
                mtx[k][i][1] = cnt
            start = j + 1
            cnt = 0


ret = 0
for i in range(h):
    for j in range(w):
        ret = max(ret, mtx[i][j][0] + mtx[i][j][1] - 1)

print(ret)

Submission status: Time Limit Exceeded

Solution code:
H,W = map(int,input().split())
maze = []
for i in range(H):
  s = input()
  maze.append(s)
def distance(maze,H,W):
  wall = []
  for i in range(H):
    w = [-1]
    for j in range(W):
      if maze[i][j] == '#':
        w.append(j)
    w.append(W)
    wall.append(w)

  wd = [[0]*W for _ in range(H)]
  for i in range(H):
    w1 = 0
    for j in range(W):
      if maze[i][j] == '#':
        w1 = min(w1+1,len(wall[i])-1)
        continue
      wd[i][j] = j-wall[i][w1]-1+wall[i][w1+1]-j-1

  return wd

wd = distance(maze,H,W)
maze = [list(x) for x in zip(*maze)]
hd = distance(maze,W,H)
max_dist = 1

for i in range(H):
  for j in range(W):
    max_dist = max(max_dist,wd[i][j] + hd[j][i]+1)
print(max_dist)

Submission status: Time Limit Exceeded

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

for i in range(H):
    done = [0] * W
    for j in range(W):
        if S[i][j] == '#':
            continue
        if done[j]:
            continue
        l = 0
        while j + l < W:
            if S[i][j+l] == '#':
                break
            l += 1
        for k in range(l):
            cnt[i][j+k] += l
            done[j+k] = 1

for j in range(W):
    done = [0] * H
    for i in range(H):
        if S[i][j] == '#':
            continue
        if done[i]:
            continue
        l = 0
        while i + l < H:
            if S[i+l][j] == '#':
                break
            l += 1
        for k in range(l):
            cnt[i+k][j] += l
            done[i+k] = 1
ans = 0
for i in range(H):
    for j in range(W):
        ans = max(ans, cnt[i][j]-1)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import pprint
def main():
    H, W = (int(_) for _ in input().split())
    S = [input() for _ in range(H)]
    yoko = [[0] * W for _ in range(H)]
    tate = [[0] * W for _ in range(H)]

    for i in range(H):
        right = 0
        cc = 0
        for left in range(W):
            if S[i][left] == '#':
                cc = 0
                right += 1
                continue
            while right < W and S[i][right] == '.':
                right += 1
            cc = right - left
            if left > 0 and S[i][left-1] == '.': yoko[i][left] = yoko[i][left-1]
            else: yoko[i][left] = cc
    for i in range(W):
        bottom = 0
        cc = 0
        for top in range(H):
            if S[top][i] == '#':
                cc = 0
                bottom += 1
                continue
            while bottom < H and S[bottom][i] == '.':
                bottom += 1
            cc = bottom - top
            if top > 0 and S[top-1][i] == '.': tate[top][i] = tate[top-1][i]
            else: tate[top][i] = cc
    output = 0
    for i in range(H):
        for j in range(W):
            output = max(output, yoko[i][j]+tate[i][j]-1)
    print(output)
    return

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()
def main():
    h,w=map(int,input().split())
    S=[input() for _ in range(h)]
    L=[[0]*w for _ in range(h)]
    R=[[0]*w for _ in range(h)]
    U=[[0]*w for _ in range(h)]
    D=[[0]*w for _ in range(h)]
    for i in range(h):
        for j in range(w):
            if S[i][j]=='#':
                L[i][j]=0
                U[i][j]=0
                continue
            if i==0:U[i][j]=1
            else:U[i][j]=U[i-1][j]+1
            if j==0:L[i][j]=1
            else:L[i][j]=L[i][j-1]+1
    for i in range(h-1,-1,-1):
        for j in range(w-1,-1,-1):
            if S[i][j]=='#':
                R[i][j]=0
                D[i][j]=0
                continue
            if i==h-1:D[i][j]=1
            else:D[i][j]=D[i+1][j]+1
            if j==w-1:R[i][j]=1
            else:R[i][j]=R[i][j+1]+1 
    X=[[l+r+u+d-3 for l,r,u,d in zip(ll,rr,uu,dd)] for ll,rr,uu,dd in zip(L,R,U,D)]
    print(max([max(x) for x in X]))


if __name__=='__main__':
    main()

Submission status: Time Limit Exceeded

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

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

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

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

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

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

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

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

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

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

ans=0

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()
def main():
    h,w=map(int,input().split())
    S=[input() for _ in range(h)]
    L=[[0]*w for _ in range(h)]
    R=[[0]*w for _ in range(h)]
    U=[[0]*w for _ in range(h)]
    D=[[0]*w for _ in range(h)]
    for i in range(h):
        for j in range(w):
            if S[i][j]=='#':
                L[i][j]=0
                U[i][j]=0
                continue
            if i==0:U[i][j]=1
            else:U[i][j]=U[i-1][j]+1
            if j==0:L[i][j]=1
            else:L[i][j]=L[i][j-1]+1
    for i in range(h-1,-1,-1):
        for j in range(w-1,-1,-1):
            if S[i][j]=='#':
                R[i][j]=0
                D[i][j]=0
                continue
            if i==h-1:D[i][j]=1
            else:D[i][j]=D[i+1][j]+1
            if j==w-1:R[i][j]=1
            else:R[i][j]=R[i][j+1]+1 
    X=[[l+r+u+d-3 for l,r,u,d in zip(ll,rr,uu,dd)] for ll,rr,uu,dd in zip(L,R,U,D)]
    print(max([max(x) for x in X]))


if __name__=='__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
#標準入力
H, W = map(int, input().split())
grid = []
for i in range(H):
    tmp = input()
    grid.append(tmp)

#グリッドの縦横を別々に調べる
import numpy as np
grid_int = np.zeros((H, W))
for i in range(H):
    count = 0
    for j in range(W):
        if grid[i][j] == '.':
            count += 1
            if j == W - 1:
                for l in range(count):
                    grid_int[i][j-l] = count
        elif grid[i][j] == '#':
            if j == 0:
                continue
            for k in range(count):
                grid_int[i][j- k - 1] = count
            count = 0


for j in range(W):
    count = 0
    for i in range(H):
        if grid[i][j] == '.':
            count += 1
            if i == H - 1:
                for m in range(count):
                    grid_int[i - m][j] += count
        elif grid[i][j] == '#':
            if i == 0:
                continue
            for k in range(count):
                grid_int[i-k-1][j] += count
            count = 0

ans = 0
for i in range(H):
    tmp_max = max(grid_int[i]) - 1
    if ans < tmp_max:
        ans = tmp_max
print(int(ans))

Submission status: Time Limit Exceeded

Solution code:
#標準入力
H, W = map(int, input().split())
grid = []
for i in range(H):
    tmp = input()
    grid.append(tmp)

#グリッドの縦横を別々に調べる
grid_int = [[0] * W for i in range(H)]
for i in range(H):
    count = 0
    for j in range(W):
        if grid[i][j] == '.':
            count += 1
            if j == W - 1:
                for l in range(count):
                    grid_int[i][j-l] = count
        elif grid[i][j] == '#':
            if j == 0:
                continue
            for k in range(count):
                grid_int[i][j- k - 1] = count
            count = 0


for j in range(W):
    count = 0
    for i in range(H):
        if grid[i][j] == '.':
            count += 1
            if i == H - 1:
                for m in range(count):
                    grid_int[i - m][j] += count
        elif grid[i][j] == '#':
            if i == 0:
                continue
            for k in range(count):
                grid_int[i-k-1][j] += count
            count = 0

ans = 0
for i in range(H):
    tmp_max = max(grid_int[i]) - 1
    if ans < tmp_max:
        ans = tmp_max
print(ans)

Submission status: Time Limit Exceeded

Solution code:
# D - Lamp

from numpy import zeros, max

H, W = map(int, input().split())
M = [[str(m) for m in input()] for _ in range(H)]
U = zeros((H, W), dtype=int)
L = zeros((H, W), dtype=int)
D = zeros((H, W), dtype=int)
R = zeros((H, W), dtype=int)

for i in range(H):
    for j in range(W):
        if M[i][j] != '#':
            if i == 0:
                U[i][j] = 1
            else:
                U[i][j] = U[i - 1][j] + 1
            if j == 0:
                L[i][j] = 1
            else:
                L[i][j] = L[i][j - 1] + 1

        k, l = H - i - 1, W - j - 1
        if M[k][l] != '#':
            if i == 0:
                D[k][l] = 1
            else:
                D[k][l] = D[k + 1][l] + 1
            if j == 0:
                R[k][l] = 1
            else:
                R[k][l] = R[k][l + 1] + 1

print(max(U + L + D + R - 3))

Submission status: Time Limit Exceeded

Solution code:
# D - Lamp

from numpy import zeros

H, W = map(int, input().split())
M = [[str(m) for m in input()] for _ in range(H)]
U = zeros((H, W), dtype=int)
L = zeros((H, W), dtype=int)
D = zeros((H, W), dtype=int)
R = zeros((H, W), dtype=int)

for i in range(H):
    for j in range(W):
        if M[i][j] != '#':
            if i == 0:
                U[i][j] = 1
            else:
                U[i][j] = U[i - 1][j] + 1
            if j == 0:
                L[i][j] = 1
            else:
                L[i][j] = L[i][j - 1] + 1

        k, l = H - i - 1, W - j - 1
        if M[k][l] != '#':
            if i == 0:
                D[k][l] = 1
            else:
                D[k][l] = D[k + 1][l] + 1
            if j == 0:
                R[k][l] = 1
            else:
                R[k][l] = R[k][l + 1] + 1

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
# D - Lamp

H, W = map(int, input().split())
M = [[str(m) for m in 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 i in range(H):
    for j in range(W):
        if M[i][j] != '#':
            if i == 0:
                U[i][j] = 1
            else:
                U[i][j] = U[i - 1][j] + 1
            if j == 0:
                L[i][j] = 1
            else:
                L[i][j] = L[i][j - 1] + 1

        k, l = H - i - 1, W - j - 1
        if M[k][l] != '#':
            if i == 0:
                D[k][l] = 1
            else:
                D[k][l] = D[k + 1][l] + 1
            if j == 0:
                R[k][l] = 1
            else:
                R[k][l] = R[k][l + 1] + 1

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
# D - Lamp

H, W = map(int, input().split())
M = [[str(m) for m in 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 i in range(H):
    for j in range(W):
        if M[i][j] != '#':
            if i == 0:
                U[i][j] = 1
            else:
                U[i][j] = U[i - 1][j] + 1
            if j == 0:
                L[i][j] = 1
            else:
                L[i][j] = L[i][j - 1] + 1

        if M[H - i - 1][W - j - 1] != '#':
            if i == 0:
                D[H - i - 1][W - j - 1] = 1
            else:
                D[H - i - 1][W - j - 1] = D[H - i][W - j - 1] + 1
            if j == 0:
                R[H - i - 1][W - j - 1] = 1
            else:
                R[H - i - 1][W - j - 1] = R[H - i - 1][W - j] + 1

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
# D - Lamp

H, W = map(int, input().split())
M = [[str(m) for m in 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 i in reversed(range(H)):
    for j in reversed(range(W)):
        if M[i][j] == '#':
            continue

        if i == H - 1:
            D[i][j] = 1
        else:
            D[i][j] = D[i + 1][j] + 1

        if j == W - 1:
            R[i][j] = 1
        else:
            R[i][j] = R[i][j + 1] + 1

mx = 0
for i in range(H):
    for j in range(W):
        if M[i][j] == '#':
            continue

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

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

        p = U[i][j] + L[i][j] + D[i][j] + R[i][j] - 3
        mx = max(mx, p)

# mx = 0
# for i in range(H):
#     for j in range(H):
#         p = U[i][j] + L[i][j] + D[i][j] + R[i][j] - 3
#         mx = max(mx, p)

print(mx)

Submission status: Time Limit Exceeded

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

r_list = [[0] * w for _ in range(h)]
l_list = [[0] * w for _ in range(h)]
u_list = [[0] * w for _ in range(h)]
d_list = [[0] * w for _ in range(h)]

for i in range(h * w):
    w_i = i // w
    h_i = i % w
    h_j = i // h
    w_j = i % h
    #left
    if s_list[w_i][h_i] == '.':
        if h_i == 0:
            l_list[w_i][h_i] = 1
        else:
            l_list[w_i][h_i] = l_list[w_i][h_i - 1] + 1
    #right
    if s_list[w_i][w - h_i - 1] == '.':
        if h_i == 0:
            r_list[w_i][w - h_i - 1] = 1
        else:
            r_list[w_i][w - h_i - 1] = r_list[w_i][w - h_i] + 1
    if s_list[w_j][h_j] == '.':
        if w_j == 0:
            d_list[w_j][h_j] = 1
        else:
            d_list[w_j][h_j] = d_list[w_j - 1][h_j] + 1
    if s_list[h - w_j - 1][h_j] == '.':
        if w_j == 0:
            u_list[h - w_j - 1][h_j] = 1
        else:
            u_list[h - w_j - 1][h_j] = u_list[h - w_j][h_j] + 1
ans = 0
for i in range(w):
    for j in range(h):
        ans = max(ans, l_list[j][i] + r_list[j][i] + u_list[j][i] + d_list[j][i] - 3)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
h, w = map(int, input().split())
s = []
for _ in range(h):
    k = list(input())
    s.append(k)
l_list = [[0 for _ in range(w)] for _ in range(h)]
r_list = [[0 for _ in range(w)] for _ in range(h)]
u_list = [[0 for _ in range(w)] for _ in range(h)]
d_list = [[0 for _ in range(w)] for _ in range(h)]
for hh in range(h):
    for ww in range(w):
        if ww == 0:
            if s[hh][ww] == ".":
                l_list[hh][ww] = 1
            if s[hh][w - ww - 1] == ".":
                r_list[hh][w - ww - 1] = 1
        else:
            if s[hh][ww] == ".":
                l_list[hh][ww] = 1 + l_list[hh][ww - 1]
            if s[hh][w - ww - 1] == ".":
                r_list[hh][w - ww - 1] = 1 + r_list[hh][w - ww]
                
for ww in range(w):
    for hh in range(h):
        if hh == 0:
            if s[hh][ww] == ".":
                u_list[hh][ww] = 1
            if s[h - hh - 1][ww] == ".":
                d_list[h - hh - 1][ww] = 1
        else:
            if s[hh][ww] == ".":
                u_list[hh][ww] = 1 + u_list[hh - 1][ww]
            if s[h - hh - 1][ww] == ".":
                d_list[h - hh - 1][ww] = 1 + d_list[h - hh][ww]
ans = 0
for ww in range(w):
    for hh in range(h):
        ans = max(ans, r_list[hh][ww] + l_list[hh][ww] + u_list[hh][ww] + d_list[hh][ww] - 3)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(2147483647)
input = sys.stdin.buffer.readline

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

s=list(input().strip() for _ in range(h))


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

def yoko_check(y,x,cnt):
    global yoko,s
    if x==w-1:
        yoko[y][x]=cnt
        return cnt
    else:
        if s[y][x+1]==35:
            yoko[y][x]=cnt
            return cnt
        else:
            yoko[y][x]=yoko_check(y,x+1,cnt+1)
            return yoko[y][x]


def tate_check(y,x,cnt):
    global tate,s
    if y==h-1:
        tate[y][x]=cnt
        return cnt
    else:
        if s[y+1][x]==35:
            tate[y][x]=cnt
            return cnt
        else:
            tate[y][x]=tate_check(y+1,x,cnt+1)
            return tate[y][x]


for y in range(h):
    for x in range(w):
        #print(s[y][x])
        if yoko[y][x]==0 and s[y][x]==46:
            yoko_check(y,x,1)
        if tate[y][x]==0 and s[y][x]==46:
            tate_check(y,x,1)

#print(tate)

result=0
for y in range(h):
    for x in range(w):
        if s[y][x]==46:
            result=max(result,tate[y][x]+yoko[y][x]-1)  

print(result)

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(2147483647)

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

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

def yoko_check(y,x,cnt):
    global yoko,s
    if x==w-1:
        yoko[y][x]=cnt
        return cnt
    else:
        if s[y][x+1]=="#":
            yoko[y][x]=cnt
            return cnt
        else:
            yoko[y][x]=yoko_check(y,x+1,cnt+1)
            return yoko[y][x]


def tate_check(y,x,cnt):
    global tate,s
    if y==h-1:
        tate[y][x]=cnt
        return cnt
    else:
        if s[y+1][x]=="#":
            tate[y][x]=cnt
            return cnt
        else:
            tate[y][x]=tate_check(y+1,x,cnt+1)
            return tate[y][x]


for y in range(h):
    for x in range(w):
        if yoko[y][x]==0 and s[y][x]==".":
            yoko_check(y,x,1)
        if tate[y][x]==0 and s[y][x]==".":
            tate_check(y,x,1)

#print(tate)

result=0
for y in range(h):
    for x in range(w):
        if s[y][x]==".":
            result=max(result,tate[y][x]+yoko[y][x]-1)  

print(result)

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(2147483647)
input=sys.stdin.readline

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

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

def yoko_check(y,x,cnt):
    global yoko,s
    if x==w-1:
        yoko[y][x]=cnt
        return cnt
    else:
        if s[y][x+1]=="#":
            yoko[y][x]=cnt
            return cnt
        else:
            yoko[y][x]=yoko_check(y,x+1,cnt+1)
            return yoko[y][x]


def tate_check(y,x,cnt):
    global tate,s
    if y==h-1:
        tate[y][x]=cnt
        return cnt
    else:
        if s[y+1][x]=="#":
            tate[y][x]=cnt
            return cnt
        else:
            tate[y][x]=tate_check(y+1,x,cnt+1)
            return tate[y][x]


for y in range(h):
    for x in range(w):
        if yoko[y][x]==0 and s[y][x]==".":
            yoko_check(y,x,1)
        if tate[y][x]==0 and s[y][x]==".":
            tate_check(y,x,1)

#print(tate)

result=0
for y in range(h):
    for x in range(w):
        if s[y][x]==".":
            result=max(result,tate[y][x]+yoko[y][x]-1)  

print(result)

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(2147483647)
INF=float('inf')

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

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

def yoko_check(y,x,cnt):
    global yoko,s
    if x==w-1:
        yoko[y][x]=cnt
        return cnt
    else:
        if s[y][x+1]=="#":
            yoko[y][x]=cnt
            return cnt
        else:
            yoko[y][x]=yoko_check(y,x+1,cnt+1)
            return yoko[y][x]


def tate_check(y,x,cnt):
    global tate,s
    if y==h-1:
        tate[y][x]=cnt
        return cnt
    else:
        if s[y+1][x]=="#":
            tate[y][x]=cnt
            return cnt
        else:
            tate[y][x]=tate_check(y+1,x,cnt+1)
            return tate[y][x]


for y in range(h):
    for x in range(w):
        if yoko[y][x]==0 and s[y][x]==".":
            yoko_check(y,x,1)
        if tate[y][x]==0 and s[y][x]==".":
            tate_check(y,x,1)

#print(tate)

result=0
for y in range(h):
    for x in range(w):
        if s[y][x]==".":
            result=max(result,tate[y][x]+yoko[y][x]-1)  

print(result)