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
def input():
  return sys.stdin.readline()[:-1]

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

l=[[0]*(w+2) for _ in range(h+2)]
r=[[0]*(w+2) for _ in range(h+2)]
u=[[0]*(w+2) for _ in range(h+2)]
d=[[0]*(w+2) for _ in range(h+2)]
for i in range(1, h+1):
  for j in range(1, w+1):
    if s[i][j]=='.':
      l[i][j]=l[i][j-1]+1
  for j in range(w, 0, -1):
    if s[i][j]=='.':
      r[i][j]=r[i][j+1]+1
for j in range(1, w+1):
  for i in range(1, h+1):
    if s[i][j]=='.':
      u[i][j]=u[i-1][j]+1
  for i in range(h, 0, -1):
    if s[i][j]=='.':
      d[i][j]=d[i+1][j]+1
m=0
for i in range(1, h+1):
  for j in range(1, w+1):
    c=l[i][j]+r[i][j]+u[i][j]+d[i][j]-3
    if m<c:
      m=c
print(m)

Submission status: Time Limit Exceeded

Solution code:
h, w = map(int, input().split())
s = ['']*(h+2)
s[0] = '#'*(w+2)
for i in range(1, h+1):
  s[i] = '#'+input()+'#'
s[h+1] = '#'*(w+2)
m=0
for i in range(1, h+1):
  for j in range(1, w+1):
    if s[i][j]=='.':
      c=1
      px, nx=0, 0
      while s[i][j+px+1]=='.':
        px+=1
      while s[i][j-nx-1]=='.':
        nx+=1
      py, ny=0, 0
      while s[i+py+1][j]=='.':
        py+=1
      while s[i-ny-1][j]=='.':
        ny+=1
      c+=px+py+nx+ny
      if c>m:
        m=c
print(m)

Submission status: Time Limit Exceeded

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

l=[[0]*(w+2) for _ in range(h+2)]
r=[[0]*(w+2) for _ in range(h+2)]
u=[[0]*(w+2) for _ in range(h+2)]
d=[[0]*(w+2) for _ in range(h+2)]
for i in range(1, h+1):
  for j in range(1, w+1):
    if s[i][j]=='.':
      l[i][j]=l[i][j-1]+1
  for j in range(w, 0, -1):
    if s[i][j]=='.':
      r[i][j]=r[i][j+1]+1
for j in range(1, w+1):
  for i in range(1, h+1):
    if s[i][j]=='.':
      u[i][j]=u[i-1][j]+1
  for i in range(h, 0, -1):
    if s[i][j]=='.':
      d[i][j]=d[i+1][j]+1
m=0
for i in range(1, h+1):
  for j in range(1, w+1):
    c=l[i][j]+r[i][j]+u[i][j]+d[i][j]-3
    if m<c:
      m=c
print(m)

Submission status: Time Limit Exceeded

Solution code:
h, w = map(int, input().split())
s = ['']*(h+2)
s[0] = '#'*(w+2)
for i in range(1, h+1):
  s[i] = '#'+input()+'#'
s[h+1] = '#'*(w+2)
m=0
for i in range(1, h+1):
  for j in range(1, w+1):
    if s[i][j]=='.':
      c=1
      px, nx=0, 0
      while s[i][j+px+1]=='.':
        px+=1
      while s[i][j-nx-1]=='.':
        nx+=1
      py, ny=0, 0
      while s[i+py+1][j]=='.':
        py+=1
      while s[i-ny-1][j]=='.':
        ny+=1
      c+=px+py+nx+ny
      if c>m:
        m=c
    if m==h+w-1:
      break
      break
print(m)

Submission status: Time Limit Exceeded

Solution code:
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):
        # l
        if s[i][j]=="#":
            l[i][j] = 0
        elif j==0:
            l[i][j] = 1
        else:
            l[i][j] = l[i][j-1] + 1
        # u
        if s[i][j]=="#":
            u[i][j] = 0
        elif i==0:
            u[i][j] = 1
        else:
            u[i][j] = u[i-1][j] + 1

for i in reversed(range(h)):
    for j in reversed(range(w)):
        # r
        if s[i][j]=="#":
            r[i][j] = 0
        elif j==(w-1):
            r[i][j] = 1
        else:
            r[i][j] = r[i][j+1] + 1
        # d
        if s[i][j]=="#":
            d[i][j] = 0
        elif i==(h-1):
            d[i][j] = 1
        else:
            d[i][j] = d[i+1][j] + 1

# print(l)
# print(r)
# print(u)
# print(d)

M = 0
for i in range(h):
    for j in range(w):
        lamp = l[i][j] + r[i][j] + u[i][j] + d[i][j] - 3
        M = max(M,lamp)

print(M)

Submission status: Time Limit Exceeded

Solution code:
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
        elif j==0:
            l[i][j] = 1
        else:
            l[i][j] = l[i][j-1] + 1

for i in range(h):
    for j in reversed(range(w)):
        if s[i][j]=="#":
            r[i][j] = 0
        elif j==(w-1):
            r[i][j] = 1
        else:
            r[i][j] = r[i][j+1] + 1
    
for i in range(h):
    for j in range(w):
        if s[i][j]=="#":
            u[i][j] = 0
        elif i==0:
            u[i][j] = 1
        else:
            u[i][j] = u[i-1][j] + 1

for i in reversed(range(h)):
    for j in range(w):
        if s[i][j]=="#":
            d[i][j] = 0
        elif i==(h-1):
            d[i][j] = 1
        else:
            d[i][j] = d[i+1][j] + 1

# print(l)
# print(r)
# print(u)
# print(d)

M = 0
for i in range(h):
    for j in range(w):
        lamp = l[i][j] + r[i][j] + u[i][j] + d[i][j] - 3
        M = max(M,lamp)

print(M)

Submission status: Time Limit Exceeded

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

left=[[0]*w for _ in range(h)]
for i in range(h):
  cnt=0
  for j in range(w):
    if grid[i][j]=='.':
      cnt+=1
      left[i][j]=cnt
    else:
      cnt=0
      left[i][j]=cnt
      
right=[[0]*w for _ in range(h)]
for i in range(h):
  cnt=0
  for j in range(w):
    if grid[i][w-j-1]=='.':
      cnt+=1
      right[i][j]=cnt
    else:
      cnt=0
      right[i][j]=cnt

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

down=[[0]*w for i in range(h)]
for i in range(w):
  cnt=0
  for j in range(h):
    if grid[h-j-1][i]=='.':
        cnt+=1
        down[j][i]=cnt
    else:
        cnt=0
        down[j][i]=cnt

ans=0
for i in range(h):
  for j in range(w):
    if grid[i][j]=='.':
      tmp=left[i][j]+right[i][w-j-1]+up[i][j]+down[h-i-1][j]-3
      if tmp>ans:
        ans=tmp
    else:
      continue
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
import numpy as np
H,W = map(int,input().split())
HL=range(H)
WL=range(W)
Gd = [list(input()) for _ in HL]
L=[np.arange(1, W+1)]*H
R=[np.arange(W, 0, -1)]*H
U=[np.arange(H, 0, -1)]*W
D=[np.arange(1, H+1)]*W
L=np.array(L)
R=np.array(R)
U=np.array(U)
D=np.array(D)
def ma():
  for h in HL:
    for w in WL:
      if Gd[h][w]=='#':
        L[h][w:]=L[h][w:]-L[h][w]
      wr=W-w-1
      if Gd[h][wr]=='#':
        R[h][:wr+1]=R[h][:wr+1]-R[h][wr]
  for w in WL:
    for h in HL:
      if Gd[h][w]=='#':
        D[w][h:]=D[w][h:]-D[w][h]
      hr=H-h-1
      if Gd[hr][w]=='#':
        U[w][:hr+1]=U[w][:hr+1]-U[w][hr]
  return np.max(D.T+U.T+L+R-3)
print(ma())

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
import numpy as np
H,W = map(int,input().split())
Gd = [list(input()) for _ in range(H)]
L=[np.arange(1, W+1)]*H
R=[np.arange(W, 0, -1)]*H
U=[np.arange(H, 0, -1)]*W
D=[np.arange(1, H+1)]*W
L=np.array(L)
R=np.array(R)
U=np.array(U)
D=np.array(D)
HL=range(H)
WL=range(W)
for h in HL:
  for w in WL:
    if Gd[h][w]=='#':
      L[h][w:]=L[h][w:]-L[h][w]
    wr=W-w-1
    if Gd[h][wr]=='#':
      R[h][:wr+1]=R[h][:wr+1]-R[h][wr]
for w in WL:
  for h in HL:
    if Gd[h][w]=='#':
      D[w][h:]=D[w][h:]-D[w][h]
    hr=H-h-1
    if Gd[hr][w]=='#':
      U[w][:hr+1]=U[w][:hr+1]-U[w][hr]
print(np.max(D.T+U.T+L+R-3))

Submission status: Time Limit Exceeded

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

ans = 1
d = [[1, 0], [-1, 0], [0, 1], [0, -1]]
for i in range(H):
    for j in range(W):
        if S[i][j] == "#":
            continue
        cnt = 1
        for dy, dx in d:
            y = i + dy
            x = j + dx
            while 0 <= y < H and 0 <= x < W and S[y][x] == ".":
                cnt += 1
                y += dy
                x += dx
        ans = max(ans, cnt)

print(ans)

Submission status: Time Limit Exceeded

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

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(W):
    if S[0][i] == ".":
        U[0][i] = 1
    if S[H-1][i] == ".":
        D[H-1][i] = 1

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

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

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
import numpy as np
H,W = map(int,input().split())
Gd,L,R,U,D=[None]*H,[None]*H,[None]*H,[None]*H,[None]*H
for _ in range(H):
  Gd[_] = list(input())
  L[_], R[_], D[_], U[_] = [0]*W, [0]*W, [0]*W, [0]*W
L=np.array(L)
R=np.array(R)
U=np.array(U)
D=np.array(D)
HL=range(H)
WL=range(W)
def ma():
  for h in HL:
    for w in WL:
      if Gd[h][w]=='#':
        R[h][w]=0
      else:
        if w==0:
          R[h][w]=1
        else:
          R[h][w]=R[h][w-1]+1
      wr=W-w-1
      if Gd[h][wr]=='#':
        L[h][wr]=0
      else:
        if w==0:
          L[h][wr]=1
        else:
          L[h][wr]=L[h][wr+1]+1

  for w in WL:
    for h in HL:
      if Gd[h][w]=='#':
        D[h][w]=0
      else:
        if h==0:
          D[h][w]=1
        else:
          D[h][w]=D[h-1][w]+1
      hr=H-h-1
      if Gd[hr][w]=='#':
        U[hr][w]=0
      else:
        if h==0:
          U[hr][w]=1
        else:
          U[hr][w]=U[hr+1][w]+1
  return np.max(D+U+L+R-3)
print(ma())

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
import numpy as np
H,W = map(int,input().split())
Gd,L,R,U,D=[None]*H,[None]*H,[None]*H,[None]*H,[None]*H
for _ in range(H):
  Gd[_] = list(input())
  L[_], R[_], D[_], U[_] = [0]*W, [0]*W, [0]*W, [0]*W
L=np.array(L)
R=np.array(R)
U=np.array(U)
D=np.array(D)
def ma():
  for h in range(H):
    for w in range(W):
      if Gd[h][w]=='#':
        R[h][w]=0
      else:
        if w==0:
          R[h][w]=1
        else:
          R[h][w]=R[h][w-1]+1
      wr=W-w-1
      if Gd[h][wr]=='#':
        L[h][wr]=0
      else:
        if w==0:
          L[h][wr]=1
        else:
          L[h][wr]=L[h][wr+1]+1

  for w in range(W):
    for h in range(H):
      if Gd[h][w]=='#':
        D[h][w]=0
      else:
        if h==0:
          D[h][w]=1
        else:
          D[h][w]=D[h-1][w]+1
      hr=H-h-1
      if Gd[hr][w]=='#':
        U[hr][w]=0
      else:
        if h==0:
          U[hr][w]=1
        else:
          U[hr][w]=U[hr+1][w]+1
  return np.max(D+U+L+R-3)
print(ma())

Submission status: Time Limit Exceeded

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

ans = 1
d = [[1, 0], [-1, 0], [0, 1], [0, -1]]
for i in range(H):
    for j in range(W):
        if S[i][j] == "#":
            continue
        cnt = 1
        for dy, dx in d:
            y = i + dy
            x = j + dx
            while 0 <= y < H and 0 <= x < W and S[y][x] == ".":
                cnt += 1
                y += dy
                x += dx
        ans = max(ans, cnt)

print(ans)

Submission status: Time Limit Exceeded

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

dw = [[0]*W for _ in range(H)]
dh = [[0]*W for _ in range(H)]
maze = [[0]*W for _ in range(H)]

for h in range(H):
    s = input()
    c = 0
    for w, ss in enumerate(s):
        maze[h][w] = ss
        if ss == ".":
            c += 1
            dw[h][w] = c
        else:
            c = 0

for w in range(W):
    c = 0
    for h in range(H):
        ss = maze[h][w]
        if ss == ".":
            c += 1
            dh[h][w] = c
        else:
            c = 0

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

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

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

print(ans)

Submission status: Time Limit Exceeded

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

# Left
Left = [[0] * W for _ in range(H)]
for h in range(H):
    cnt = 0
    for w in range(W):
        if field[h][w] == '#':
            cnt = 0
        else:
            cnt += 1
        Left[h][w] = cnt

# Right
Right = [[0] * W for _ in range(H)]
for h in range(H):
    cnt = 0
    for w in range(W-1, -1, -1):
        if field[h][w] == '#':
            cnt = 0
        else:
            cnt += 1
        Right[h][w] = cnt

# Up
Up = [[0] * W for _ in range(H)]
for w in range(W):
    cnt = 0
    for h in range(H):
        if field[h][w] == '#':
            cnt = 0
        else:
            cnt += 1
        Up[h][w] = cnt

# Down
Down = [[0] * W for _ in range(H)]
for w in range(W):
    cnt = 0
    for h in range(H-1, -1, -1):
        if field[h][w] == '#':
            cnt = 0
        else:
            cnt += 1
        Down[h][w] = cnt

# 集計
ans = -1
for h in range(H):
    for w in range(W):
        ans = max(ans, Left[h][w] + Right[h][w] + Up[h][w] + Down[h][w] - 3)
print(ans)

Submission status: Time Limit Exceeded

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

ans = 1

dx = [1, 0, -1, 0]
dy = [0, 1, 0, -1]

memo = [[[-1]*w for _ in range(h)] for _ in range(4)]

for x in range(h):
    for y in range(w):
        if field[x][y] == '#':
            continue
        lamp = 1
        for i in range(4):
            j = 0
            while True:
                nx = x + dx[i]*(j+1)
                ny = y + dy[i]*(j+1)
                if (nx < 0) or (nx >= h) or (ny < 0) or (ny >= w) or (field[nx][ny] == '#'):
                    break
                j += 1
                lamp += 1
                if memo[i][nx][ny] != -1:
                    j += memo[i][nx][ny]
                    lamp += memo[i][nx][ny]
                    break
            memo[i][x][y] = j
        ans = max(ans, lamp)
print(ans)

Submission status: Time Limit Exceeded

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

ans = 1

dx = [1, 0, -1, 0]
dy = [0, 1, 0, -1]

memo = [[[-1]*w for _ in range(h)] for _ in range(4)]

for x in range(h):
    for y in range(w):
        if field[x][y] == '#':
            continue
        lamp = 1
        for i in range(4):
            j = 0
            while True:
                nx = x + dx[i]*(j+1)
                ny = y + dy[i]*(j+1)
                if (nx < 0) or (nx >= h) or (ny < 0) or (ny >= w) or (field[nx][ny] == '#'):
                    break
                j += 1
                lamp += 1
                if memo[i][nx][ny] != -1:
                    j += memo[i][nx][ny]
                    lamp += memo[i][nx][ny]
                    break
        memo[i][nx][ny] = j
        ans = max(ans, lamp)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

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

ans = 1

dx = [1, 0, -1, 0]
dy = [0, 1, 0, -1]

memo = np.zeros([4, h, w], dtype='int')
memo[:, :, :] = -1

for x in range(h):
    for y in range(w):
        if field[x][y] == '#':
            continue
        lamp = 1
        for i in range(4):
            j = 0
            while True:
                nx = x + dx[i]*(j+1)
                ny = y + dy[i]*(j+1)
                if (nx < 0) or (nx >= h) or (ny < 0) or (ny >= w) or (field[nx][ny] == '#'):
                    break
                j += 1
                lamp += 1
                if memo[i, nx, ny] != -1:
                    j += memo[i, nx, ny]
                    lamp += memo[i, nx, ny]
                    break
        memo[i, x, y] = j
        ans = max(ans, lamp)
print(ans)

Submission status: Time Limit Exceeded

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

ans = 1

dx = [1, 0, -1, 0]
dy = [0, 1, 0, -1]

for x in range(h):
    for y in range(w):
        if field[x][y] == '#':
            continue
        lamp = 1
        for i in range(4):
            j = 1
            while True:
                nx = x + dx[i]*j
                ny = y + dy[i]*j
                if (nx < 0) or (nx >= h) or (ny < 0) or (ny >= w) or (field[nx][ny] == '#'):
                    break
                j += 1
                lamp += 1
        ans = max(ans, lamp)
print(ans)

Submission status: Time Limit Exceeded

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

ans = 1

dx = [1, 0, -1, 0]
dy = [0, 1, 0, -1]

for x in range(h):
    for y in range(w):
        if field[x][y] == '#':
            continue
        lamp = 1
        for i in range(4):
            j = 1
            while True:
                nx = x + dx[i]*j
                ny = y + dy[i]*j
                if (0 <= nx < h) and (0 <= ny < w) and (field[nx][ny] == '.'):
                    j += 1
                    lamp += 1
                else:
                    break
        ans = max(ans, lamp)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
import numpy as np
H,W = map(int,input().split())
Gd,L,R,U,D=[None]*H,[None]*H,[None]*H,[None]*H,[None]*H
for _ in range(H):
  Gd[_] = list(input())
  L[_], R[_], D[_], U[_] = [0]*W, [0]*W, [0]*W, [0]*W
L=np.array(L)
R=np.array(R)
U=np.array(U)
D=np.array(D)

for h in range(H):
  for w in range(W):
    if Gd[h][w]=='#':
      R[h][w]=0
    else:
      if w==0:
        R[h][w]=1
      else:
        R[h][w]=R[h][w-1]+1
    wr=W-w-1
    if Gd[h][wr]=='#':
      L[h][wr]=0
    else:
      if w==0:
        L[h][wr]=1
      else:
        L[h][wr]=L[h][wr+1]+1

for w in range(W):
  for h in range(H):
    if Gd[h][w]=='#':
      D[h][w]=0
    else:
      if h==0:
        D[h][w]=1
      else:
        D[h][w]=D[h-1][w]+1
    hr=H-h-1
    if Gd[hr][w]=='#':
      U[hr][w]=0
    else:
      if h==0:
        U[hr][w]=1
      else:
        U[hr][w]=U[hr+1][w]+1
ret = np.max(D+U+L+R-3)
print(ret)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
H,W = map(int,input().split())
Gd,L,R,U,D=[None]*H,[None]*H,[None]*H,[None]*H,[None]*H
for _ in range(H):
  Gd[_] = list(input())
  L[_], R[_], D[_], U[_] = [0]*W, [0]*W, [0]*W, [0]*W
for h in range(H):
  for w in range(W):
    if Gd[h][w]=='#':
      R[h][w]=0
    else:
      if w==0:
        R[h][w]=1
      else:
        R[h][w]=R[h][w-1]+1
    wr=W-w-1
    if Gd[h][wr]=='#':
      L[h][wr]=0
    else:
      if w==0:
        L[h][wr]=1
      else:
        L[h][wr]=L[h][wr+1]+1

for w in range(W):
  for h in range(H):
    if Gd[h][w]=='#':
      D[h][w]=0
    else:
      if h==0:
        D[h][w]=1
      else:
        D[h][w]=D[h-1][w]+1
    hr=H-h-1
    if Gd[hr][w]=='#':
      U[hr][w]=0
    else:
      if h==0:
        U[hr][w]=1
      else:
        U[hr][w]=U[hr+1][w]+1
ret = 0
for w in range(W):
  for h in range(H):
    ret = max(ret, D[h][w]+U[h][w]+R[h][w]+L[h][w]-3)
print(ret)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
H,W = map(int,input().split())
Gd = [list(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 h in range(H):
  for w in range(W):
    if Gd[h][w]=='#':
      R[h][w]=0
    else:
      if w==0:
        R[h][w]=1
      else:
        R[h][w]=R[h][w-1]+1
    wr=W-w-1
    if Gd[h][wr]=='#':
      L[h][wr]=0
    else:
      if w==0:
        L[h][wr]=1
      else:
        L[h][wr]=L[h][wr+1]+1

for w in range(W):
  for h in range(H):
    if Gd[h][w]=='#':
      D[h][w]=0
    else:
      if h==0:
        D[h][w]=1
      else:
        D[h][w]=D[h-1][w]+1
    hr=H-h-1
    if Gd[hr][w]=='#':
      U[hr][w]=0
    else:
      if h==0:
        U[hr][w]=1
      else:
        U[hr][w]=U[hr+1][w]+1
ret = 0
for w in range(W):
  for h in range(H):
    ret = max(ret, D[h][w]+U[h][w]+R[h][w]+L[h][w]-3)
print(ret)

Submission status: Time Limit Exceeded

Solution code:
H,W = map(int,input().split())
Gd = [list(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 h in range(H):
  for w in range(W):
    if Gd[h][w]=='#':
      R[h][w]=0
    else:
      if w==0:
        R[h][w]=1
      else:
        R[h][w]=R[h][w-1]+1
    wr=W-w-1
    if Gd[h][wr]=='#':
      L[h][wr]=0
    else:
      if w==0:
        L[h][wr]=1
      else:
        L[h][wr]=L[h][wr+1]+1

for w in range(W):
  for h in range(H):
    if Gd[h][w]=='#':
      D[h][w]=0
    else:
      if h==0:
        D[h][w]=1
      else:
        D[h][w]=D[h-1][w]+1
    hr=H-h-1
    if Gd[hr][w]=='#':
      U[hr][w]=0
    else:
      if h==0:
        U[hr][w]=1
      else:
        U[hr][w]=U[hr+1][w]+1
ret = 0
for w in range(W):
  for h in range(H):
    ret = max(ret, D[h][w]+U[h][w]+R[h][w]+L[h][w]-3)
print(ret)

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):
  if s[i][j]=='#':
   L[i][j]=U[i][j]=0
   continue
  if i==0 and j==0:
   L[i][j]=1
   U[i][j]=1
  elif i==0:
   L[i][j]=L[i][j-1]+1
   U[i][j]=1
  elif j==0:
   L[i][j]=1
   U[i][j]=U[i-1][j]+1
  else:
   L[i][j]=L[i][j-1]+1
   U[i][j]=U[i-1][j]+1

for i in range(h-1,-1,-1):
 for j in range(w-1,-1,-1):
  if s[i][j]=='#':
   R[i][j]=D[i][j]=0
   continue
  if i==h-1 and j==w-1:
   R[i][j]=1
   D[i][j]=1
  elif i==h-1:
   R[i][j]=R[i][j+1]+1
   D[i][j]=1
  elif j==w-1:
   R[i][j]=1
   D[i][j]=D[i+1][j]+1
  else:
   R[i][j]=R[i][j+1]+1
   D[i][j]=D[i+1][j]+1

ans=0
for i in range(h):
 for j in range(w):
  ans=max(ans,R[i][j]+L[i][j]+U[i][j]+D[i][j]-3)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
import numpy as np 

h, w = map(int, sys.stdin.readline().split())
s = np.array([list(sys.stdin.readline().rstrip()) for _ in range(h)], dtype='U')
s = np.pad(s, 1, mode='constant')

def main():
    l = np.zeros((h+2, w+2), dtype=np.int64)
    r = np.zeros((h+2, w+2), dtype=np.int64)
    u = np.zeros((h+2, w+2), dtype=np.int64)
    d = np.zeros((h+2, w+2), dtype=np.int64)

    for i in range(1, w+1):
        bl = s[:, i] == '#'
        l[~bl, i] = l[~bl, i-1] + 1
        i = w+1-i
        bl = s[:, i] == '#'
        r[~bl, i] = r[~bl, i+1] + 1
    
    for i in range(1, h+1):
        bl = s[i, :] == '#'
        u[i, ~bl] = u[i-1, ~bl] + 1
        i = h+1-i
        bl = s[i, :] == '#'
        d[i, ~bl] = d[i+1, ~bl] + 1
    
    res = l + r + u + d - 3
    return np.amax(res[1:h+1, 1:w+1])

if __name__ == '__main__':
    ans = main()
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
import os, sys, re, math, queue


def run_it():
	H,W = list(map(int,input().split(' ')))
	
	s_ary = []
	l_memo = [[0 for j in range(W)] for i in range(H)]
	r_memo = [[0 for j in range(W)] for i in range(H)]
	u_memo = [[0 for j in range(W)] for i in range(H)]
	b_memo = [[0 for j in range(W)] for i in range(H)]
	
	for _ in range(H):
		s_ary.append(input())
	
	for h in range(H):
		for w in range(W):
			if s_ary[h][w] == '.':
				if w == 0:	
					l_memo[h][w] = 1
				elif w > 0:
					l_memo[h][w] = l_memo[h][w-1] + 1
				if h == 0:
					u_memo[h][w] = 1
				elif h > 0:
					u_memo[h][w] = u_memo[h-1][w] + 1
			
			if s_ary[-h-1][-w-1] == '.':
				if w == 0:
					r_memo[-h-1][-w-1] = 1
				elif w > 0:
					r_memo[-h-1][-w-1] = r_memo[-h-1][-w] + 1	
				if h == 0:
					b_memo[-h-1][-w-1] = 1
				elif h > 0:
					b_memo[-h-1][-w-1] = b_memo[-h][-w-1] + 1
	
	ret = 0
	for h in range(H):
		for w in range(W):
			tmp = l_memo[h][w] + r_memo[h][w] + u_memo[h][w] + b_memo[h][w] - 3
			ret = max(ret, tmp)
	
	print(ret)
				
if sys.platform == 'ios':
	for i in range(2):
		sys.stdin = open('Dtest' + str(i) + '.txt')
		run_it()
else:
	run_it()

Submission status: Time Limit Exceeded

Solution code:
H, W = map(int, input().split())
M = [list(input()) for i in range(H)]
dp = [[[0] * 2 for w in range(W)] for h in range(H)]
ans = 0
#LU
for h in range(H):
  for w in range(W):
    if M[h][w] == "#": continue
    dp[h][w][0] = dp[h][w-1][0] + 1 if w != 0 else 1
    dp[h][w][1] = dp[h-1][w][1] + 1 if h != 0 else 1
#RD
for h in range(H-1, -1, -1):
  for w in range(W-1, -1, -1):
    if M[h][w] == "#": continue
    x = dp[h][w+1][0] + 1 if w != W - 1 else 1
    y = dp[h+1][w][1] + 1 if h != H - 1 else 1
    ans = max(ans, sum(dp[h][w]) + x + y -3)
    dp[h][w] = [x, y]

print(ans)

Submission status: Time Limit Exceeded

Solution code:
H, W = map(int, input().split())
M = [list(input()) for i in range(H)]
dp = [[[0] * 4 for w in range(W)] for h in range(H)]
ans = 0
#LU
for h in range(H):
  for w in range(W):
    if M[h][w] == "#": continue
    dp[h][w][0] = dp[h][w-1][0] + 1 if w != 0 else 1
    dp[h][w][2] = dp[h-1][w][2] + 1 if h != 0 else 1
#RD
for h in range(H)[::-1]:
  for w in range(W)[::-1]:
    if M[h][w] == "#": continue
    dp[h][w][1] = dp[h][w+1][1] + 1 if w != W - 1 else 1
    dp[h][w][3] = dp[h+1][w][3] + 1 if h != H - 1 else 1
    ans = max(ans, sum(dp[h][w])-3)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
H, W = map(int, input().split())
M = [list(input()) for i in range(H)]
ans = [[[0] * 4 for w in range(W)] for h in range(H)]
#LU
for h in range(H):
  for w in range(W):
    if M[h][w] == "#": continue
    ans[h][w][0] = ans[h][w-1][0] + 1 if w != 0 else 1
    ans[h][w][2] = ans[h-1][w][2] + 1 if h != 0 else 1
#RD
for h in range(H)[::-1]:
  for w in range(W)[::-1]:
    if M[h][w] == "#": continue
    ans[h][w][1] = ans[h][w+1][1] + 1 if w != W - 1 else 1
    ans[h][w][3] = ans[h+1][w][3] + 1 if h != H - 1 else 1

print(max([max([sum(ans[h][w]) - 3 for w in range(W)]) for h in range(H)]))

Submission status: Time Limit Exceeded

Solution code:
H, W = map(int, input().split())
M = [list(input()) for i in range(H)]
ans = [[[0] * 4 for w in range(W)] for h in range(H)]
#L
for h in range(H):
  for w in range(W):
    if w == 0:
      ans[h][w][0] = M[h][w] == "."
    else:
      ans[h][w][0] = ans[h][w-1][0] + 1 if M[h][w] == "." else 0
#R
for h in range(H):
  for w in range(W)[::-1]:
    if w == W - 1:
      ans[h][w][1] = M[h][w] == "."
    else:
      ans[h][w][1] = ans[h][w+1][1] + 1 if M[h][w] == "." else 0
#U
for h in range(H):
  for w in range(W):
    if h == 0:
      ans[h][w][2] = M[h][w] == "."
    else:
      ans[h][w][2] = ans[h-1][w][2] + 1 if M[h][w] == "." else 0
#D
for h in range(H)[::-1]:
  for w in range(W):
    if h == H - 1:
      ans[h][w][3] = M[h][w] == "."
    else:
      ans[h][w][3] = ans[h+1][w][3] + 1 if M[h][w] == "." else 0

print(max([max([sum(ans[h][w]) - 3 for w in range(W)]) for h in range(H)]))

Submission status: Time Limit Exceeded

Solution code:
H, W = map(int, input().split())
M = [input() for i in range(H)]
ans = [[[0] * 4 for w in range(W)] for h in range(H)]
#L
for h in range(H):
  for w in range(W):
    if w == 0:
      ans[h][w][0] = M[h][w] == "."
    else:
      ans[h][w][0] = ans[h][w-1][0] + 1 if M[h][w] == "." else 0
#R
for h in range(H):
  for w in range(W)[::-1]:
    if w == W - 1:
      ans[h][w][1] = M[h][w] == "."
    else:
      ans[h][w][1] = ans[h][w+1][1] + 1 if M[h][w] == "." else 0
#U
for h in range(H):
  for w in range(W):
    if h == 0:
      ans[h][w][2] = M[h][w] == "."
    else:
      ans[h][w][2] = ans[h-1][w][2] + 1 if M[h][w] == "." else 0
#D
for h in range(H)[::-1]:
  for w in range(W):
    if h == H - 1:
      ans[h][w][3] = M[h][w] == "."
    else:
      ans[h][w][3] = ans[h+1][w][3] + 1 if M[h][w] == "." else 0

print(max([max([sum(ans[h][w]) - 3 for w in range(W)]) for h in range(H)]))

Submission status: Time Limit Exceeded

Solution code:
h, w = map(int, input().split())
s = [input() for _ in range(h)]
sy = [0] * w
ret = 0
for y in range(h):
    seqx = 0
    seqy = 0
    for x in range(w):
        if s[y][x] == '.':
            if sy[x] == 0:
                yy = y
                while yy < h and s[yy][x] != '#':
                    yy += 1
                sy[x] = yy - y
            seqx += 1
            seqy = max(seqy, sy[x])
            ret = max(ret, seqx + seqy - 1)
        else:
            seqx = 0
            seqy = 0
            sy[x] = 0
print(ret)