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())
grid = [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):
        if grid[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
for i in range(H)[::-1]:
    for j in range(W)[::-1]:
        if grid[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
print(max(L[i][j] + R[i][j] + U[i][j] + D[i][j] - 3for i in range(H) for j in range(W)))

Submission status: Time Limit Exceeded

Solution code:
H, W = map(int, input().split())
grid = [input() for i in range(H)]
count = [[[0, 0] for j in range(W)] for i in range(H)]
for i in range(H):
    row = '#' + grid[i] + '#'
    left = 0
    while True:
        right = row.find('#', left+1)
        if right == -1:
            break
        for j in range(left, right-1):
            count[i][j][0] = right - left - 1
        left = right
columns = list(zip(*grid))
for j in range(W):
    column = '#' + ''.join(columns[j]) + '#'
    left = 0
    while True:
        right = column.find('#', left+1)
        if right == -1:
            break
        for i in range(left, right-1):
            count[i][j][1] = right - left - 1
        left = right
print(max(sum(count[i][j]) for i in range(H) for j in range(W)) - 1)

Submission status: Time Limit Exceeded

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

maze = [input() for _ in range(H)]

light_max = 0

def light(xx, yy):
    drc = [(-1, 0), (1, 0), (0, 1), (0, -1)] 
    ans = 1
    for dx, dy in drc:
        xx0 = xx + dx
        yy0 = yy + dy
        while 0 <= xx0 <= W-1 and 0 <= yy0 <= H-1:
            if maze[yy0][xx0] == '#':break
            ans += 1
            xx0 += dx
            yy0 += dy
    return ans

for hh in range(H):
    for ww in range(W):
        if maze[hh][ww] == '#':continue
        area = light(ww, hh)
        light_max = max(light_max, area)

print(light_max)

Submission status: Time Limit Exceeded

Solution code:
import sys
from collections import deque
input = sys.stdin.readline
H, W = [int(x) for x in input().strip().split()]
S = [0 for _ in range(H)]
ans = [[0] * W for h in range(H)]

for h in range(H):
    S[h] = list(input().strip())

for i, s in enumerate(S):
    c = 0
    q = deque([])
    for k, j in enumerate(s):
        if j == '.':
            c += 1
            q.append(k)
            continue
        for qq in q:
            ans[i][qq] = c
        q = deque([])
        c = 0
    else:
        for qq in q:
            ans[i][qq] = c

ans_max = 0
for w in range(W):
    c = 0
    q = []
    for i, s in enumerate(S):
        if s[w] == '.':
            c += 1
            q.append(i)
            continue
        for qq in q:
            ans[qq][w] += c
            ans_max = max(ans_max, ans[qq][w])
        q = []
        c = 0
    else:
        for qq in q:
            ans[qq][w] += c
            ans_max = max(ans_max, ans[qq][w])
        q = []
        c = 0

print(ans_max-1)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
H, W = [int(x) for x in input().strip().split()]
S = [0 for _ in range(H)]
ver = [[0] * W for h in range(H)]
hor = [[0] * W for h in range(H)]

for h in range(H):
    S[h] = list(input().strip())

for i, s in enumerate(S):
    c = 0
    q = []
    for k, j in enumerate(s):
        if j == '.':
            c += 1
            q.append(k)
            continue
        for qq in q:
            ver[i][qq] = c
        q = []
        c = 0
    else:
        for qq in q:
            ver[i][qq] = c

for w in range(W):
    c = 0
    q = []
    for i, s in enumerate(S):
        if s[w] == '.':
            c += 1
            q.append(i)
            continue
        for qq in q:
            hor[qq][w] = c
        q = []
        c = 0
    else:
        for qq in q:
            hor[qq][w] = c
        q = []
        c = 0

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

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

H, W = input().split()

H = int(H)
W = int(W)

S = []
score = np.zeros((H,W),dtype=int)
score_col = np.zeros((H,W),dtype=int)
for i in range(H):
    S.append(input())

    
for i in range(H):
    if S[i][0] == '.':
        score[i][0] = 1

    for j in range(1,W):
        if S[i][j] == '#':
            continue

        score[i][j] = score[i][j-1]+1

    for j in range(W-2,-1,-1):
        if score[i][j+1] != 0 and score[i][j] != 0:
            score[i][j] = score[i][j+1]
    
for i in range(W):
    if S[0][i] == '.':
       score_col[0][i] = 1

    for j in range(1,H):
        if S[j][i] == '#':
            continue

        score_col[j][i] = score_col[j-1][i]+1

    for j in range(H-2,-1,-1):
        if score_col[j+1][i] != 0 and score_col[j][i] != 0:
            score_col[j][i] = score_col[j+1][i]


ss = score + score_col
print(np.max(ss)-1)

Submission status: Time Limit Exceeded

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

H = int(H)
W = int(W)

S = []
score = []
score_col = []
for i in range(H):
    S.append(input())

score=[[0]*W for i in range(H)]
score_col = [[0]*W for i in range(H)]
    
for i in range(H):
    if S[i][0] == '.':
        score[i][0] = 1

    for j in range(1,W):
        if S[i][j] == '#':
            continue

        score[i][j] = score[i][j-1]+1

    for j in range(W-2,-1,-1):
        if score[i][j+1] != 0 and score[i][j] != 0:
            score[i][j] = score[i][j+1]
    
for i in range(W):
    if S[0][i] == '.':
       score_col[0][i] = 1

    for j in range(1,H):
        if S[j][i] == '#':
            continue

        score_col[j][i] = score_col[j-1][i]+1

    for j in range(H-2,-1,-1):
        if score_col[j+1][i] != 0 and score_col[j][i] != 0:
            score_col[j][i] = score_col[j+1][i]


m = 1
for i in range(H):
    for j in range(W):
        if score[i][j] + score_col[i][j] > m:
            m = score[i][j] + score_col[i][j]

print(m-1)

Submission status: Time Limit Exceeded

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

H = int(H)
W = int(W)

S = []
score = []
score_col = []
for i in range(H):
    S.append(input())
    score.append([0]*W)

for i in range(H):
    score_col.append([0]*W)
    
for i in range(H):
    if S[i][0] == '.':
        score[i][0] = 1

    for j in range(1,W):
        if S[i][j] == '#':
            continue

        score[i][j] = score[i][j-1]+1

    for j in reversed(range(0,W-1)):
        if score[i][j+1] != 0 and score[i][j] != 0:
            score[i][j] = score[i][j+1]
    
for i in range(W):
    if S[0][i] == '.':
       score_col[0][i] = 1

    for j in range(1,H):
        if S[j][i] == '#':
            continue

        score_col[j][i] = score_col[j-1][i]+1

    for j in reversed(range(0,H-1)):
        if score_col[j+1][i] != 0 and score_col[j][i] != 0:
            score_col[j][i] = score_col[j+1][i]


m = 0            
for i in range(H):
    for j in range(W):
        if score[i][j] + score_col[i][j]-1 > m:
            m = score[i][j] + score_col[i][j]-1 

print(m)

Submission status: Time Limit Exceeded

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

H = int(H)
W = int(W)

S = []
score = []
score_col = []
for i in range(H):
    S.append(input())
    score.append([0]*W)

for i in range(H):
    score_col.append([0]*W)
    
for i in range(H):
    if S[i][0] == '.':
        score[i][0] = 1

    for j in range(1,W):
        if S[i][j] == '#':
            continue

        score[i][j] = score[i][j-1]+1

    for j in reversed(range(0,W-1)):
        if score[i][j+1] != 0 and score[i][j] != 0:
            score[i][j] = score[i][j+1]
    
for i in range(W):
    if S[0][i] == '.':
       score_col[0][i] = 1

    for j in range(1,H):
        if S[j][i] == '#':
            continue

        score_col[j][i] = score_col[j-1][i]+1

    for j in reversed(range(0,H-1)):
        if score_col[j+1][i] != 0 and score_col[j][i] != 0:
            score_col[j][i] = score_col[j+1][i]


m = []            
for i in range(H):
    for j in range(W):
        m.append(score[i][j] + score_col[i][j]-1)

print(max(m))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
import sys
input = sys.stdin.readline
def main():
    H,W = map(int,input().split())
    S = [list(input().rstrip()) for _ in range(H)]
    L = np.zeros((H,W))
    R = np.zeros((H,W))
    U = np.zeros((H,W))
    D = np.zeros((H,W))
    for i in range(H):
        for j in range(W):
            if j == 0:
                if S[i][j] == "#":
                    L[i,j] = -1
                continue
            if S[i][j] == ".":
                L[i,j] = L[i,j-1]+1
            else:
                L[i,j] = -1
        for j in reversed(range(W)):
            if j == W-1:
                if S[i][j] == "#":
                    R[i,j] = -1
                continue
            if S[i][j] == ".":
                R[i,j] = R[i,j+1]+1
            else:
                R[i,j] = -1
    for j in range(W):
        for i in range(H):
            if i == 0:
                if S[i][j] == "#":
                    U[i,j] = -1
                continue
            if S[i][j] == ".":
                U[i,j] = U[i-1,j]+1
            else:
                U[i,j] = -1
        for i in reversed(range(H)):
            if i == H-1:
                if S[i][j] == "#":
                    D[i,j] = -1
                continue
            if S[i][j] == ".":
                D[i,j] = D[i+1,j]+1
            else:
                D[i,j] = -1
    su = L+R+U+D
    #print(L,R,U,D,sep="\n")
    print(int(np.amax(su)+1))
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
import sys
input = sys.stdin.readline
def main():
    H,W = map(int,input().split())
    S = [list(input().rstrip()) for _ in range(H)]
    L = np.zeros((H,W))
    R = np.zeros((H,W))
    U = np.zeros((H,W))
    D = np.zeros((H,W))
    for i in range(H):
        for j in range(W):
            if j == 0:
                if S[i][j] == "#":
                    L[i,j] = -1
                continue
            if S[i][j] == ".":
                L[i,j] += L[i,j-1]+1
            else:
                L[i,j] = -1
        for j in reversed(range(W)):
            if j == W-1:
                if S[i][j] == "#":
                    R[i,j] = -1
                continue
            if S[i][j] == ".":
                R[i,j] += R[i,j+1]+1
            else:
                R[i,j] = -1
    for j in range(W):
        for i in range(H):
            if i == 0:
                if S[i][j] == "#":
                    U[i,j] = -1
                continue
            if S[i][j] == ".":
                U[i,j] += U[i-1,j]+1
            else:
                U[i,j] = -1
        for i in reversed(range(H)):
            if i == H-1:
                if S[i][j] == "#":
                    D[i,j] = -1
                continue
            if S[i][j] == ".":
                D[i,j] += D[i+1,j]+1
            else:
                D[i,j] = -1
    su = L+R+U+D
    #print(L,R,U,D,sep="\n")
    print(int(np.amax(su)+1))
if __name__ == '__main__':
    main()

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

Submission status: Time Limit Exceeded

Solution code:
h,w=map(int,input().split())
grid=[None]*h
for i in range(h):
  grid[i]=input()
  
L=[[0 for i in range(w)] for j in range(h)]
R=[[0 for i in range(w)] for j in range(h)]
U=[[0 for i in range(w)] for j in range(h)]
D=[[0 for i in range(w)] for j in range(h)]

for i in range(h):
  if grid[i][0]=="#":
    L[i][0]=0
  else:
    L[i][0]=1
  for l in range(1,w):
    if grid[i][l]=="#":
      L[i][l]=0
    else:
      L[i][l]=L[i][l-1]+1
      
  if grid[i][-1]=="#":
    R[i][-1]=0
  else:
    R[i][-1]=1
  for r in range(w-2,-1,-1):
    if grid[i][r]=="#":
      R[i][r]=0
    else:
      R[i][r]=R[i][r+1]+1
      
for j in range(w):
  if grid[0][j]=="#":
    U[0][j]=0
  else:
    U[0][j]=1
  for u in range(1,h):
    if grid[u][j]=="#":
      U[u][j]=0
    else:
      U[u][j]=U[u-1][j]+1
      
  if grid[-1][j]=="#":
    D[-1][j]=0
  else:
    D[-1][j]=1
  for d in range(h-2,-1,-1):
    if grid[d][j]=="#":
      D[d][j]=0
    else:
      D[d][j]=D[d+1][j]+1
      
ans=0
for i in range(h):
  for j in range(w):
    ans=max(ans,L[i][j]+R[i][j]+U[i][j]+D[i][j])

print(ans-3)

Submission status: Time Limit Exceeded

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

for i in range(h):
  grid[i]=input()

yoko=[[0 for i in range(w)] for j in range(h)]
tate=[[0 for i in range(w)] for j in range(h)]

for i in range(h):
  s=grid[i]
  left=0
  right=0
  while left<len(s):
    if s[left]=="#":
      left+=1
      right=left
      continue
    while right+1<w and s[right+1]==".":
      right+=1
  
    for j in range(left,right+1):
      yoko[i][j]=right-left
      
    left=right+1
    right=left
    
for i in range(w):
  up=0
  down=0
  while up<h:
    if grid[down][i]=="#":
      up+=1
      down=up
      continue
    while down+1<h and grid[down+1][i]==".":
      down+=1
    for j in range(up,down+1):
      tate[j][i]=down-up
    up=down+1
    down=up
    
ans=0
for i in range(h):
  for j in range(w):
    if grid[i][j]==".":
      ans=max(ans,tate[i][j]+yoko[i][j])
      
print(ans+1)

Submission status: Time Limit Exceeded

Solution code:
import sys

sys.setrecursionlimit(10000)

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

ans = 0
max_ans = 0
for i in range(h):
    array = input()
    grid.append(array)

def step(i,j, direction):
    global count
    if i < 0 or i >= h or j < 0 or j >= w:
        return count
    if grid[i][j] == '#':
        return count
    else:
        if direction == 'left':
            count += 1
            step(i, j-1, 'left')
        elif direction == 'right':
            count += 1
            step(i, j+1, 'right')
        elif direction == 'up':
            count += 1
            step(i-1, j, 'up')
        elif direction == 'down':
            count += 1
            step(i+1, j, 'down')

def check(y_loc, x_loc):
    global count
    count = 0
    step(y_loc, x_loc-1, 'left')
    (x_loc, y_loc) = (x_loc, y_loc)
    step(y_loc, x_loc+1, 'right')
    (x_loc, y_loc) = (x_loc, y_loc)
    step(y_loc-1, x_loc, 'up')
    (x_loc, y_loc) = (x_loc, y_loc)
    step(y_loc+1, x_loc, 'down')
    return count

for i in range(h):
    for j in range(w):
        if grid[i][j] == '.':
            ans = check(i, j)
            if ans >= max_ans:
                max_ans = ans

print(max_ans+1)

Submission status: Time Limit Exceeded

Solution code:
import sys

sys.setrecursionlimit(10000)

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

ans = 0
max_ans = 0
for i in range(h):
    array = input()
    grid.append(array)
    
def step(i,j, direction):
    global count
    if i < 0 or i >= h or j < 0 or j >= w:
        return count
    if grid[i][j] == '#':
        return count
    else:
        if direction == 'left':
            count += 1
            step(i, j-1, 'left')
        elif direction == 'right':
            count += 1
            step(i, j+1, 'right')
        elif direction == 'up':
            count += 1
            step(i-1, j, 'up')
        elif direction == 'down':
            count += 1
            step(i+1, j, 'down')

def check(y_loc, x_loc):
    global count
    count = 0
    step(y_loc, x_loc-1, 'left')
    (x_loc, y_loc) = (x_loc, y_loc)
    step(y_loc, x_loc+1, 'right')
    (x_loc, y_loc) = (x_loc, y_loc)
    step(y_loc-1, x_loc, 'up')
    (x_loc, y_loc) = (x_loc, y_loc)
    step(y_loc+1, x_loc, 'down')
    return count

for i in range(h):
    for j in range(w):
        if grid[i][j] == '.':
            ans = check(i, j)
            if ans >= max_ans:
                max_ans = ans

print(max_ans+1)

Submission status: Time Limit Exceeded

Solution code:
import bisect
h,w=map(int,input().split())
g = [['#']*(w+2) for i in range(h+2)]
for i in range(h):
  g[i+1][1:-1] = list(input())
L=[[0]*(w+2) for i in range(h+2)]
R=[[0]*(w+2) for i in range(h+2)]
D=[[0]*(w+2) for i in range(h+2)]
U=[[0]*(w+2) for i in range(h+2)]
for i in range(h+2):
  for j in range(w+2):
    if g[i][j]=="#":
      L[i][j]=0
    else:
      L[i][j]=L[i][j-1]+1
for i in range(h+2):
  for j in range(w+1,-1,-1):
    if g[i][j]=="#":
      R[i][j]=0
    else:
      R[i][j]=R[i][j+1]+1
for i in range(h+2):
  for j in range(w+2):
    if g[i][j]=="#":
      D[i][j]=0
    else:
      D[i][j]=D[i-1][j]+1
for i in range(h+1,-1,-1):
  for j in range(w+2):
    if g[i][j]=="#":
      U[i][j]=0
    else:
      U[i][j]=U[i+1][j]+1
Max=0
for i in range(h+2):
  for j in range(w+2):
    a=0
    a=L[i][j]+R[i][j]+D[i][j]+U[i][j]
    if a>Max:
      Max=a
print(Max-3)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

def convert(L):
    dp = np.zeros((L.shape[0],L.shape[1]), dtype='int')
    for i in range(L.shape[0]):
        l=L[i]
        for j in range(L.shape[1]):
            if l[j]:
                if j == 0:
                    dp[i][j] = 1
                else:
                    dp[i][j] = dp[i][j-1] + 1
            else:
                dp[i][j] = 0
    return dp
H,W=map(int,input().split())
L= np.zeros((H,W),dtype='bool')
for i in range(H):
    L[i]=list(map(lambda x: False if x=="#" else True,list(input()) ))

ans = convert(L) + np.fliplr(convert(np.fliplr(L))) + convert(L.T).T + np.flipud(convert(np.flipud(L).T).T) - 3
print(np.max(ans))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

def convert(L):
    dp = np.zeros((L.shape[0],L.shape[1]), dtype='int')
    for i in range(L.shape[0]):
        l=L[i]
        start=0
        end=0
        sum_=0
        for j in range(L.shape[1]):
            if l[j]:
                end += 1
                sum_ += 1
            elif ~l[j]  and sum_ !=0:
                dp[i][start:end] = sum_
                sum_= 0
                start = end + 1
            elif ~l[j] and sum_ ==0:
                start += 1
                end += 1
            #print(i,j,start,end,sum_,l[j])
        if sum_!=0:
            dp[i][start:end+1] = sum_  
    return dp

H,W=map(int,input().split())
L= np.zeros((H,W),dtype='bool')
for i in range(H):
    L[i]=list(map(lambda x: False if x=="#" else True,list(input()) ))

dp1 = convert(L)
dp2 = convert(L.T).T
ans = dp1+dp2-1
print(np.max(ans))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

def convert(L):
    dp = np.zeros((L.shape[0],L.shape[1]), dtype='int')
    for i in range(L.shape[0]):
        l=L[i]
        start=0
        end=0
        sum_=0
        for j in range(L.shape[1]):
            if l[j]:
                end += 1
                sum_ += 1
            elif ~l[j]  and sum_ !=0:
                dp[i][start:end] = sum_
                sum_= 0
                start = end + 1
            elif ~l[j] and sum_ ==0:
                start += 1
                end += 1
            #print(i,j,start,end,sum_,l[j])
        if sum_!=0:
            dp[i][start:end+1] = sum_  
    return dp

H,W=map(int,input().split())
L= np.zeros((H,W),dtype='bool')
for i in range(H):
    L[i]=list(map(lambda x: False if x=="#" else True,list(input()) ))

dp1 = convert(L).astype('int')
dp2 = convert(L.T).T.astype('int')
ans = dp1+dp2-1
print(ans.max())

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

H, W = map(int, input().split())
S = []
for h in range(H):
  S.append(list(map(lambda s:0 if s=='#' else 1, input())))

#l,r,u,d = [[[0 for w in range(W)] for h in range(H)] for _ in range(4)]
l,r,u,d = [np.zeros([H,W], dtype=int) for _ in range(4)]

for h in range(H):
  for w in range(W):
    s = S[h][w]
    l[h][w] = (l[h][w-1]+1)*S[h][w]
    r[h][-w-1] = (r[h][-w]+1)*S[h][-w-1]
    u[h][w] = (u[h-1][w]+1)*S[h][w]
    d[-h-1][w] = (d[-h][w]+1)*S[-h-1][w]
  
#ans = 0
#for h in range(H):
#  for w in range(W):
#    tmp =(l[h][w]+r[h][w]+u[h][w]+d[h][w]-3)*S[h][w]
#    ans = max(ans, tmp)
ans = np.max((l+r+u+d-3)*S)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
H, W = map(int, input().split())
S = []
for h in range(H):
  S.append(list(map(lambda s:0 if s=='#' else 1, input())))

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


for h in range(H):
  for w in range(W):
    s = S[h][w]
    l[h][w] = (l[h][w-1]+1)*S[h][w]
    r[h][-w-1] = (r[h][-w]+1)*S[h][-w-1]
    u[h][w] = (u[h-1][w]+1)*S[h][w]
    d[-h-1][w] = (d[-h][w]+1)*S[-h-1][w]
  
ans = 0
for h in range(H):
  for w in range(W):
    tmp =(l[h][w]+r[h][w]+u[h][w]+d[h][w]-3)*S[h][w]
    ans = max(ans, tmp)
    
print(ans)

Submission status: Time Limit Exceeded

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

for i in range(h):
    cnt = 0
    now = 0
    for j in range(w):
        if s[i][j] == ".":
            cnt += 1
        else:
            for k in range(now,j+1):
                yoko[i][k] = cnt
            cnt = 0
            now = j+1
    for k in range(now,j+1):
        yoko[i][k] = cnt
for i in range(w):
    cnt = 0
    now = 0
    for j in range(h):
        if s[j][i] == ".":
            cnt += 1
        else:
            for k in range(now,j):
                tate[k][i] = cnt
            cnt = 0
            now = j+1
    for k in range(now,j+1):
        tate[k][i] = cnt
ans = 0
for i in range(h):
    for j in range(w):
        ans = max(ans,tate[i][j]+yoko[i][j]-1)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
input=sys.stdin.readline

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

a = []
for i in range(H):
    a.append(str(input()))

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

for i in range(H):
    cur = 0
    for j in range(W):
        if a[i][j] == "#":
            cur = 0
            U[i][j] = 0
        else:
            cur += 1
            if i == 0:
                U[i][j] = 1
            else:
                U[i][j] += U[i-1][j] + 1
        L[i][j] = cur

ans = 0
for i in reversed(range(H)):
    cur = 0
    for j in reversed(range(W)):
        if a[i][j] == "#":
            cur = 0
            D[i][j] = 0
        else:
            cur += 1
            if i == (H-1):
                D[i][j] = 1
            else:
                D[i][j] += D[i+1][j] + 1
        R[i][j] = cur
        if L[i][j] + R[i][j] + D[i][j] + U[i][j] -3 > ans:
            ans = L[i][j] + R[i][j] + D[i][j] + U[i][j] -3

print(ans)

Submission status: Time Limit Exceeded

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

a = []
for i in range(H):
    a.append(str(input()))

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

for i in range(H):
    cur = 0
    for j in range(W):
        if a[i][j] == "#":
            cur = 0
            U[i][j] = 0
        else:
            cur += 1
            if i == 0:
                U[i][j] = 1
            else:
                U[i][j] += U[i-1][j] + 1
        L[i][j] = cur

ans = 0
for i in reversed(range(H)):
    cur = 0
    for j in reversed(range(W)):
        if a[i][j] == "#":
            cur = 0
            D[i][j] = 0
        else:
            cur += 1
            if i == (H-1):
                D[i][j] = 1
            else:
                D[i][j] += D[i+1][j] + 1
        R[i][j] = cur
        if L[i][j] + R[i][j] + D[i][j] + U[i][j] -3 > ans:
            ans = L[i][j] + R[i][j] + D[i][j] + U[i][j] -3

print(ans)

Submission status: Time Limit Exceeded

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

a = []
for i in range(H):
    a.append(str(input()))

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

for i in range(H):
    cur = 0
    for j in range(W):
        if a[i][j] == "#":
            cur = 0
            U[i][j] = 0
        else:
            cur += 1
            if i == 0:
                U[i][j] = 1
            else:
                U[i][j] += U[i-1][j] + 1
        L[i][j] = cur

for i in reversed(range(H)):
    cur = 0
    for j in reversed(range(W)):
        if a[i][j] == "#":
            cur = 0
            D[i][j] = 0
        else:
            cur += 1
            if i == (H-1):
                D[i][j] = 1
            else:
                D[i][j] += D[i+1][j] + 1
        R[i][j] = cur

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

print(ans)

Submission status: Time Limit Exceeded

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

front_is_air = False
count = 0


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

for i in range(h):
    for j in range(w):
        if front_is_air:
            if s[i][j] == ".":
                count += 1
                left[i][j] = count
            else:
                front_is_air = False
                count = 0

        else:
            if s[i][j] == ".":
                front_is_air = True
                count += 1
                left[i][j] = count

        if j == w-1:
            front_is_air = False
            count = 0


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

for i in range(h):
    for j in range(w-1, -1, -1):
        if front_is_air:
            if s[i][j] == ".":
                count += 1
                right[i][j] = count
            else:
                front_is_air = False
                count = 0

        else:
            if s[i][j] == ".":
                front_is_air = True
                count += 1
                right[i][j] = count

        if j == 0:
            front_is_air = False
            count = 0


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

for i in range(w):
    for j in range(h):
        if front_is_air:
            if s[j][i] == ".":
                count += 1
                up[j][i] = count
            else:
                front_is_air = False
                count = 0

        else:
            if s[j][i] == ".":
                count += 1
                front_is_air = True
                up[j][i] = count

        if j == h-1:
            front_is_air = False
            count = 0


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

for i in range(w):
    for j in range(h-1, -1, -1):
        if front_is_air:
            if s[j][i] == ".":
                count += 1
                down[j][i] = count
            else:
                front_is_air = False
                count = 0

        else:
            if s[j][i] == ".":
                count += 1
                front_is_air = True
                down[j][i] = count

        if j == 0:
            front_is_air = False
            count = 0


memo = []

for i in range(h):
    for j in range(w):
        memo.append(left[i][j] + right[i][j] + up[i][j] + down[i][j] - 3)

print(max(memo))

Submission status: Time Limit Exceeded

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

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

front_is_air = False
count = 0

for i in range(h):
    for j in range(w):
        if front_is_air:
            if s[i][j] == ".":
                count += 1
            else:
                for k in range(j-count, j):
                    memo1[i][k] = count
                front_is_air = False
                count = 0

        else:
            if s[i][j] == ".":
                count += 1
                front_is_air = True

        if j == w-1:
            if s[i][j] == ".":
                for k in range(j-count+1, j+1):
                    memo1[i][k] = count
                front_is_air = False
                count = 0


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

for i in range(w):
    for j in range(h):
        if front_is_air:
            if s[j][i] == ".":
                count += 1
            else:
                for k in range(j-count, j):
                    memo2[k][i] = count
                front_is_air = False
                count = 0

        else:
            if s[j][i] == ".":
                count += 1
                front_is_air = True

        if j == h-1:
            if s[j][i] == ".":
                for k in range(j-count+1, j+1):
                    memo2[k][i] = count
                front_is_air = False
                count = 0

memo = []

for i in range(h):
    for j in range(w):
        memo.append(memo1[i][j] + memo2[i][j] - 1)

print(max(memo))