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 numpy as np 

H,W = map(int,input().split())
S = np.array([[[0,1][i == '.'] for i in input()] for _ in range(H)])

funcA = np.frompyfunc(lambda x,y:y and x+y,2,1)
funcB = np.frompyfunc(lambda x,y:x if y and x and 1 else y,2,1)
Hor = funcA.accumulate(S,dtype=np.object,axis=1).astype(np.int)[:,::-1]
Hor = funcB.accumulate(Hor,dtype=np.object,axis=1).astype(np.int)[:,::-1]
Ver = funcA.accumulate(S,dtype=np.object,axis=0).astype(np.int)[::-1,:]
Ver = funcB.accumulate(Ver,dtype=np.object,axis=0).astype(np.int)[::-1,:]
print(np.max(Hor+Ver-1))

Submission status: Time Limit Exceeded

Solution code:
H, W = map(int, input().split())
Ss = [input() for _ in range(H)]
L = [[0]*W for _ in range(H)]
R = [[0]*W for _ in range(H)]
D = [[0]*H for _ in range(W)]
U = [[0]*H for _ in range(W)]
for i in range(H):
    for j in range(W):
        if Ss[i][j] == '.':
            if j == 0:
                L[i][0] = 1
            else:
                L[i][j] = L[i][j-1] + 1
        if Ss[i][-j-1] == '.':
            if j == 0:
                R[i][-1] = 1
            else:
                R[i][-j-1] = R[i][-j] + 1
                
for j in range(W):
    for i in range(H):
        if Ss[i][j] == '.':
            if i == 0:
                U[j][0] = 1
            else:
                U[j][i] = U[j][i-1]+1
        if Ss[-i-1][j] == '.':
            if i == 0:
                D[j][-1] = 1
            else:
                D[j][-i-1] = D[j][-i] + 1

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

Submission status: Time Limit Exceeded

Solution code:
from itertools import takewhile

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

x = [[0 for _ in range(W)] for _ in range(H)]
y = [[0 for _ in range(W)] for _ in range(H)]

for i, j in ((i, j) for i in range(H) for j in range(W) if S[i][j] == '.'):
    if x[i][j] == 0:
        w = len(list(takewhile(lambda k: S[i][k] == '.', range(j, W))))
        for k in range(j, j + w):
            x[i][k] = w
    if y[i][j] == 0:
        h = len(list(takewhile(lambda k: S[k][j] == '.', range(i, H))))
        for l in range(i, i + h):
            y[l][j] = h

ans = max(
    x[i][j] + y[i][j] - 1
    for i in range(H)
    for j in range(W)
)
# 出力
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
H,W=map(int,input().split())
s=[input() for i in range(H)]
u=np.zeros((H,W))
d=np.zeros((H,W))
l=np.zeros((H,W))
r=np.zeros((H,W))
o=np.zeros((H,W))

for i in range(H):
    ii=H-1-i
    for j in range(W):
        jj=W-1-j
        
        if s[i][j]=='#':
            l[i,j]=0
        elif j==0:
            l[i,j]=1
        else:
            l[i,j]=l[i,j-1]+1

        if s[ii][jj]=='#':
            r[ii,jj]=0
        elif jj==W-1:
            r[ii,jj]=1
        else:
            r[ii,jj]=r[ii,jj+1]+1

        if s[i][j]=='#':
            u[i,j]=0
        elif i==0:
            u[i,j]=1
        else:
            u[i,j]=u[i-1,j]+1

        if s[ii][jj]=='#':
            d[ii,jj]=0
        elif ii==H-1:
            d[ii,jj]=1
        else:
            d[ii,jj]=d[ii+1,jj]+1


print(int(np.max(l+u+r+d-3)))

Submission status: Time Limit Exceeded

Solution code:
#メイン処理
H,W = map(int,input().split())
S = []
 
#二次元配列
for _ in range(H):
    a = input()
    S.append(list(a))
 
maxlamp = 0

L = [[0] * W for i in range(H)] #左
R = [[0] * W for i in range(H)] #右
D = [[0] * W for i in range(H)] #下
U = [[0] * W for i in range(H)] #上

# L = np.zeros((H,W))
# R = np.zeros((H,W))
# D = np.zeros((H,W))
# U = np.zeros((H,W))
 
#Lの計算
for _h in range(H):
    for _w in range(W):
        if S[_h][_w] != '#':
            if _w == 0:
                L[_h][_w] = 1
            elif _w > 0:
                L[_h][_w] = L[_h][_w-1] + 1
    
            if _h == 0:
                U[_h][_w] = 1
            elif _h > 0:
                U[_h][_w] = U[_h-1][_w] + 1
 
for _h in range(H-1,-1,-1):
    for _w in range(W-1,-1,-1):
        if S[_h][_w] != '#':
            if _w == W-1:
                R[_h][_w] = 1
            elif _w < W-1:
                R[_h][_w] = R[_h][_w+1] + 1
 
            if _h == H-1:
                D[_h][_w] = 1
                
            elif _h < H-1:
                D[_h][_w] = D[_h+1][_w] + 1
        
            tmp = L[_h][_w] + R[_h][_w] + U[_h][_w] + D[_h][_w] - 3
            maxlamp = max(maxlamp,tmp)
 
print(maxlamp)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

#メイン処理
H,W = map(int,input().split())
S = []
 
#二次元配列
for _ in range(H):
    a = input()
    S.append(list(a))
 
maxlamp = 0
L = np.zeros((H,W))
R = np.zeros((H,W))
D = np.zeros((H,W))
U = np.zeros((H,W))
 
#Lの計算
for _h in range(H):
    for _w in range(W):
        if S[_h][_w] != '#':
            if _w == 0:
                L[_h][_w] = 1
            elif _w > 0:
                L[_h][_w] = L[_h][_w-1] + 1
    
            if _h == 0:
                U[_h][_w] = 1
            elif _h > 0:
                U[_h][_w] = U[_h-1][_w] + 1
 
for _h in range(H-1,-1,-1):
    for _w in range(W-1,-1,-1):
        if S[_h][_w] != '#':
            if _w == W-1:
                R[_h][_w] = 1
            elif _w < W-1:
                R[_h][_w] = R[_h][_w+1] + 1
 
            if _h == H-1:
                D[_h][_w] = 1
                
            elif _h < H-1:
                D[_h][_w] = D[_h+1][_w] + 1
        
            tmp = L[_h][_w] + R[_h][_w] + U[_h][_w] + D[_h][_w] - 3
            maxlamp = max(maxlamp,tmp)
 
print(int(maxlamp))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

#メイン処理
H,W = map(int,input().split())
S = []
 
#二次元配列
for _ in range(H):
    a = input()
    S.append(list(a))
 
maxlamp = 0
L = np.zeros((H,W))
R = np.zeros((H,W))
D = np.zeros((H,W))
U = np.zeros((H,W))
 
#Lの計算
for _h in range(H):
    for _w in range(W):
        if S[_h][_w] == '#':
            continue
        elif _w == 0:
            L[_h][_w] = 1
        elif _w > 0:
            L[_h][_w] = L[_h][_w-1] + 1
 
        if S[_h][_w] != '#' and _h == 0:
            U[_h][_w] = 1
        elif _h > 0:
            U[_h][_w] = U[_h-1][_w] + 1
 
for _h in range(H-1,-1,-1):
    for _w in range(W-1,-1,-1):
        if S[_h][_w] == '#':
            continue
        else:
            if _w == W-1:
                R[_h][_w] = 1
            elif _w < W-1:
                R[_h][_w] = R[_h][_w+1] + 1
 
            if S[_h][_w] != '#' and _h == H-1:
                D[_h][_w] = 1
                
            elif _h < H-1:
                D[_h][_w] = D[_h+1][_w] + 1
        
            tmp = L[_h][_w] + R[_h][_w] + U[_h][_w] + D[_h][_w] - 3
            maxlamp = max(maxlamp,tmp)
 
print(int(maxlamp))

Submission status: Time Limit Exceeded

Solution code:
H,W=map(int,input().split())
s=[input() for i 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)]
l=[[0 for i in range(W)]for j in range(H)]
r=[[0 for i in range(W)]for j 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 ii in range(H):
    i=H-1-ii
    for jj in range(W):
        j=W-1-jj
        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 ii in range(H):
    i=H-1-ii
    for jj in range(W):
        j=W-1-jj
        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

ans=0
for i in range(H):
    for j in range(W):
        ava=l[i][j]+r[i][j]+u[i][j]+d[i][j]-3
        if ava>ans:
            ans=ava

print(ans)

Submission status: Time Limit Exceeded

Solution code:
#メイン処理
H,W = map(int,input().split())
S = []

#二次元配列
for _ in range(H):
    a = input()
    S.append(list(a))

maxlamp = 0

L = [[0] * W for i in range(H)] #左
R = [[0] * W for i in range(H)] #右
D = [[0] * W for i in range(H)] #下
U = [[0] * W for i in range(H)] #上

#Lの計算
for _h in range(H):
    for _w in range(W):
        if S[_h][_w] == '#':
            continue
        elif _w == 0:
            L[_h][_w] = 1
        elif _w > 0:
            L[_h][_w] = L[_h][_w-1] + 1

        if S[_h][_w] != '#' and _h == 0:
            U[_h][_w] = 1
        elif _h > 0:
            U[_h][_w] = U[_h-1][_w] + 1

for _h in range(H-1,-1,-1):
    for _w in range(W-1,-1,-1):
        if S[_h][_w] == '#':
            continue
        else:
            if _w == W-1:
                R[_h][_w] = 1
            elif _w < W-1:
                R[_h][_w] = R[_h][_w+1] + 1

            if S[_h][_w] != '#' and _h == H-1:
                D[_h][_w] = 1
                
            elif _h < H-1:
                D[_h][_w] = D[_h+1][_w] + 1
        
            tmp = L[_h][_w] + R[_h][_w] + U[_h][_w] + D[_h][_w] - 3
            maxlamp = max(maxlamp,tmp)

print(maxlamp)

Submission status: Time Limit Exceeded

Solution code:
#メイン処理
H,W = map(int,input().split())
S = []

#二次元配列
for _ in range(H):
    a = input()
    S.append(list(a))

maxlamp = 0

L = [[0] * W for i in range(H)] #左
R = [[0] * W for i in range(H)] #右
D = [[0] * W for i in range(H)] #下
U = [[0] * W for i in range(H)] #上

#Lの計算
for _h in range(H):
    for _w in range(W):
        if S[_h][_w] == '#':
            continue
        elif _w == 0:
            L[_h][_w] = 1
        elif _w > 0:
            L[_h][_w] = L[_h][_w-1] + 1

        if S[_h][_w] != '#' and _h == 0:
            U[_h][_w] = 1
        elif _h > 0:
            U[_h][_w] = U[_h-1][_w] + 1

for _h in range(H-1,-1,-1):
    for _w in range(W-1,-1,-1):
        if S[_h][_w] == '#':
            continue
        elif _w == W-1:
            R[_h][_w] = 1
        elif _w < W-1:
            R[_h][_w] = R[_h][_w+1] + 1

        if S[_h][_w] != '#' and _h == H-1:
            D[_h][_w] = 1
            
        elif _h < H-1:
            D[_h][_w] = D[_h+1][_w] + 1

#MAXの数を求める
for _h in range(H):
    for _w in range(W):
        tmp = L[_h][_w] + R[_h][_w] + U[_h][_w] + D[_h][_w] -3
        if maxlamp < tmp:
            maxlamp = tmp

print(maxlamp)

Submission status: Time Limit Exceeded

Solution code:
#メイン処理
H,W = map(int,input().split())
S = []

#二次元配列
for _ in range(H):
    a = input()
    S.append(list(a))

maxlamp = 0

L = [[0] * W for i in range(H)] #左
R = [[0] * W for i in range(H)] #右
D = [[0] * W for i in range(H)] #下
U = [[0] * W for i in range(H)] #上

#Lの計算
for _h in range(H):
    for _w in range(W):
        if S[_h][_w] == '#':
            L[_h][_w] = 0
        elif _w == 0:
            L[_h][_w] = 1
        elif _w > 0:
            L[_h][_w] = L[_h][_w-1] + 1

for _h in range(H):
    for _w in range(W-1,-1,-1):
        if S[_h][_w] == '#':
            R[_h][_w] = 0
        elif _w == W-1:
            R[_h][_w] = 1
        elif _w < W-1:
            R[_h][_w] = R[_h][_w+1] + 1

for _h in range(H-1,-1,-1):
    for _w in range(W):
        if S[_h][_w] == '#':
            D[_h][_w] = 0
        elif _h == H-1:
            D[_h][_w] = 1
        elif _h < H-1:
            D[_h][_w] = D[_h+1][_w] + 1

for _h in range(H):
    for _w in range(W):
        if S[_h][_w] == '#':
            U[_h][_w] = 0
        elif _h == 0:
            U[_h][_w] = 1
        elif _h > 0:
            U[_h][_w] = U[_h-1][_w] + 1

#MAXの数を求める
for _h in range(H):
    for _w in range(W):
        tmp = L[_h][_w] + R[_h][_w] + U[_h][_w] + D[_h][_w] -3
        if maxlamp < tmp:
            maxlamp = tmp

print(maxlamp)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

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

row = Counter()
column = Counter()

for j in range(W):
    tmp = set()
    cnt = 0
    for i in range(H):
        if S[i][j] == "#":
            for pi,pj in tmp:
                row[(pi,pj)] = cnt
            tmp = set()
            cnt = 0
        else:
            tmp.add((i,j))
            cnt += 1
    for pi,pj in tmp:
        row[(pi,pj)] = cnt

for i in range(H):
    tmp = set()
    cnt = 0
    for j in range(W):
        if S[i][j] == "#":
            for pi,pj in tmp:
                column[(pi,pj)] = cnt
            tmp = set()
            cnt = 0
        else:
            tmp.add((i,j))
            cnt += 1
    for pi,pj in tmp:
        column[(pi,pj)] = cnt

ans = 0
for i in range(H):
    for j in range(W):
        res = row[(i,j)] + column[(i,j)] - 1
        ans = max(ans,res)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
H, W = map(int, input().split())
S = [input() for _ in range(H)]
ans = 0
for i in range(H):
	for j in range(W):
		if S[i][j] == ".":
			n, e, s, w = 0, 0, 0, 0
			while i - n >= 0 and S[i - n][j] == ".":
				n += 1
			while i + s <= H - 1 and S[i + s][j] == ".":
				s += 1
			while j + e <= W - 1 and S[i][j + e] == ".":
				e += 1
			while j - w >= 0 and S[i][j - w] == ".":
				w += 1
			ans = max(ans, n + s + e + w - 3)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
inf = input().split()
H = int(inf[0])
W = int(inf[1])
sheet = [[] for i in range(H+2)]
score = [[] for i in range(H+2)]
m = 0
 
for h in range(H+2):
  if h==0 or h==H+1:
    a = [False]*(W+2)
    b = [(0,0)]*(W+2)
    sheet[h]=a
    score[h]=b
  else:
    line = input()
    a = []
    b = []
    a += [False]
    b += [(0,0)]
    for w in range(W):
      b+=[(0,0)]
      if line[w]=='.':
        a += [True]
      else:
        a += [False]
    a += [False]
    b += [(0,0)]
    sheet[h]=a
    score[h]=b
      
for h in range(1,H+1):
  for w in range(1,W+1):
    if sheet[h][w]:
      s = 0
      hs = 0
      ws = 0
      if sheet[h-1][w]:
        hs += score[h-1][w][0]
      else:
        i = h+1
        while sheet[i][w]:
          hs+=1
          i+=1
      if sheet[h][w-1]:
        ws += score[h][w-1][1]
      else:
        j = w+1
        while sheet[h][j]:
          ws+=1
          j+=1
      s = 1 + hs + ws
      score[h][w]=(hs, ws)
      if m < s:
        m = s
print(m)

Submission status: Time Limit Exceeded

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

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):
    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 range(W)[::-1]:
        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(W):
    for j in range(H):
        if S[j][i] == '#':
            D[j][i] = 0
        elif j == 0:
            D[j][i] = 1
        else:
            D[j][i] = D[j - 1][i] + 1

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

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

Submission status: Time Limit Exceeded

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

hdots = [[0]*(w+1) for _ in range(h)]
for j in range(h):
    ndots = 0
    for i in range(1, w+1):
        if s[j][i-1] == '.':
            ndots += 1
        else:
            hdots[j][i-1] = ndots
            ndots = 0
    hdots[j][-1] = ndots if s[j][-1] == '.' else 0
    for i in range(w, 0, -1):
        if s[j][i-1] == '.':
            hdots[j][i] = ndots
        else:
            ndots = hdots[j][i-1]
vdots = [[0]*w for _ in range(h+1)]
for i in range(w):
    ndots = 0
    for j in range(1, h+1):
        if s[j-1][i] == '.':
            ndots += 1
        else:
            vdots[j-1][i] = ndots
            ndots = 0
    vdots[-1][i] = ndots if s[-1][i] == '.' else 0
    for j in range(h, 0, -1):
        if s[j-1][i] == '.':
            vdots[j][i] = ndots
        else:
            ndots = vdots[j-1][i]

ans = 0
for j in range(h):
    for i in range(w):
        nh, nv = hdots[j][i+1], vdots[j+1][i]
        if nh == 0 or nv == 0:
            continue
        ans = max(ans, nh+nv-1)
print(ans)

Submission status: Time Limit Exceeded

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

hdots = [[0]*(w+1) for _ in range(h)]
for j in range(h):
    ndots = 0
    for i in range(1, w+1):
        if s[j][i-1] == '.':
            ndots += 1
        else:
            hdots[j][i-1] = ndots
            ndots = 0
    hdots[j][-1] = ndots if s[j][-1] == '.' else 0
    for i in range(w, 0, -1):
        if s[j][i-1] == '.':
            hdots[j][i] = ndots
        else:
            ndots = hdots[j][i-1]
vdots = [[0]*w for _ in range(h+1)]
for i in range(w):
    ndots = 0
    for j in range(1, h+1):
        if s[j-1][i] == '.':
            ndots += 1
        else:
            vdots[j-1][i] = ndots
            ndots = 0
    vdots[-1][i] = ndots if s[-1][i] == '.' else 0
    for j in range(h, 0, -1):
        if s[j-1][i] == '.':
            vdots[j][i] = ndots
        else:
            ndots = vdots[j-1][i]

ans = 0
for j in range(h):
    for i in range(w):
        nh, nv = hdots[j][i+1], vdots[j+1][i]
        if nh == 0 or nv == 0:
            continue
        ans = max(ans, nh+nv-1)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
H,W=map(int,raw_input().split())
S=[]
for _ in range(H):
    S.append(raw_input())
Y=[[0]*W for _ in range(H)]    
T=[[0]*W for _ in range(H)]    
for i,s in enumerate(S):
    c=1
    for j in range(W):
        if S[i][j]=="#":
            c=1
            Y[i][j]=0
        else:
            Y[i][j]=c
            c+=1
        for j in reversed(range(W)):
            if Y[i][j]!=0 and j!=W-1:
                Y[i][j]=max(Y[i][j],Y[i][j+1])
for j in range(W):
    c=1
    for i in range(H):
        if S[i][j]=="#":
            c=1
            T[i][j]=0
        else:
            T[i][j]=c
            c+=1
        for i in reversed(range(H)):
            if T[i][j]!=0 and i!=H-1:
                T[i][j]=max(T[i][j],T[i+1][j])
ans=-1
for i in range(H):
    for j in range(W):
        ans=max(ans,Y[i][j]+T[i][j])
print(ans-1)

Submission status: Time Limit Exceeded

Solution code:
N,W=map(int, input().split())
nm=[0]*W
wm=[0]*W
tm=[0]*W

for i in range(N):
  s = input()
  x = 0
  for j in range(W):
    if s[j]=='.':
        nm[j] += 1
    else:
        for k in range (x, j):
           wm[k] = max(wm[k], j-x)
        tm[j] = max(tm[j], wm[j]+nm[j]-1)
        x =j+1
        wm[j] = nm[j] = 0
  for k in range (x, W):
     wm[k] = max(wm[k],W-x)
for j in range (W):
   tm[j] = max(tm[j], wm[j]+nm[j]-1)
print(max(tm))

Submission status: Time Limit Exceeded

Solution code:
import sys
import numpy as np
sys.setrecursionlimit(1000000)
 
H,W = map(int,input().split())
S = [input() for i in range(H)]

L = [[0 for i in range(W)] for j in range(H)]
R = [[0 for i in range(W+1)] for j in range(H)]

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

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

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

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python
import bisect
from collections import defaultdict

def main():
    H, W = map(int, input().split())
    lines = [input() for i in range(H)]

    row_dict = defaultdict(dict)
    col_dict = defaultdict(dict)

    for i in range(H):
        prev_block = -1
        for j in range(W):
            if lines[i][j] == "#":
                row_dict[i][j] = 0
                for k in range(prev_block+1, j):
                    row_dict[i][k] = j - prev_block - 1
                prev_block = j
            else:
                if j == W-1:
                    for k in range(prev_block+1, W):
                        row_dict[i][k] = j - prev_block

    for j in range(W):
        prev_block = -1
        for i in range(H):
            if lines[i][j] == "#":
                col_dict[i][j] = 0
                for k in range(prev_block+1, i):
                    col_dict[k][j] = i - prev_block - 1
                prev_block = i
            else:
                if i == H-1:
                    for k in range(prev_block+1, H):
                        col_dict[k][j] = i - prev_block

    max_val = 0
    for i in range(H):
        for j in range(W):
            val = row_dict[i][j] + col_dict[i][j] - 1
            if val > max_val:
                max_val = val

    print(max_val)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:

h,w=map(int,input().split())
s=[list(input()) for i in range(h)]
L=[[0 for i in range(w)]for i in range(h)]
R=[[0 for i in range(w)]for i in range(h)]
D=[[0 for i in range(w)]for i in range(h)]
U=[[0 for i in range(w)]for i in range(h)]
ans=0
for i in range(h):
    for j in range(w):
        if s[i][j]=='.':
            if j==0:
                R[i][j]=1
            else:
                R[i][j]=R[i][j-1]+1

        if s[h-i-1][w-j-1]=='.':
            if j==0:
                L[h-i-1][w-j-1]=1
            else:
                L[h-i-1][w-j-1]=L[h-i-1][w-j]+1


        if s[h-i-1][w-j-1]=='.':
            if i==0:
                U[h-i-1][w-j-1]=1
            else:
                U[h-i-1][w-j-1]=U[h-i][w-j-1]+1

        if s[i][j]=='.':
            if i==0:
                D[i][j]=1
            else:
                D[i][j]=D[i-1][j]+1


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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
import sys
import copy
import collections
from bisect import bisect_left
from bisect import bisect_right
from collections import defaultdict
from heapq import heappop, heappush
import numpy as np
import statistics
from statistics import mean, median,variance,stdev
import math

def main():
	H, W = map(int, input().split(" "))
	S = []
	for i in range(H):
		s = list(input())
		S.append(s)
	
	ans = 0
	for i in range(H):
		for j in range(W):
			if S[i][j] == "#":
				continue
			side = get_side(S[i], j)
			
			V = []
			for x in range(H):
				v = S[x][j]
				V.append(v)
			varti = get_side(V, i)
			
			tmp = side + varti
			tmp -= 1
			if tmp > ans:
				ans = tmp
				
			#print("{} {} -> {} {}".format(i, j, side, varti))
				
	print(ans)
	
def get_side(S, k):
	total = ''.join(S)
	total = total.split("#")
	
	index = 0
	for i, val in enumerate(total):
		index += len(val)
		if k <= index:
			return len(val)
		index += 1
	
	'''
	ret = 0
	flg = False
	#print(k)
	
	for i, val in enumerate(S):
		#print(val)
		if i == k:
			flg = True
		
		if val == ".":
			ret += 1
		elif val == "#" and flg == False:
			ret = 0
		elif val == "#" and flg == True:
			return ret
			
	return ret
	'''
	
	
if __name__ == "__main__":
	main()

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
import sys
import copy
import collections
from bisect import bisect_left
from bisect import bisect_right
from collections import defaultdict
from heapq import heappop, heappush
import numpy as np
import statistics
from statistics import mean, median,variance,stdev
import math

def main():
	H, W = map(int, input().split(" "))
	S = []
	for i in range(H):
		s = list(input())
		S.append(s)
	
	ans = 0
	for i in range(H):
		for j in range(W):
			if S[i][j] == "#":
				continue
			side = get_side(S[i], j)
			
			V = []
			for x in range(H):
				v = S[x][j]
				V.append(v)
			varti = get_side(V, i)
			
			tmp = side + varti
			tmp -= 1
			if tmp > ans:
				ans = tmp
				
			#print("{} {} -> {} {}".format(i, j, side, varti))
				
	print(ans)
	
def get_side(S, k):
	ret = 0
	flg = False
	#print(k)
	
	for i, val in enumerate(S):
		#print(val)
		if i == k:
			flg = True
		
		if val == ".":
			ret += 1
		elif val == "#" and flg == False:
			ret = 0
		elif val == "#" and flg == True:
			return ret
			

			
	return ret
	
	
if __name__ == "__main__":
	main()

Submission status: Time Limit Exceeded

Solution code:
hw = list(map(int, input().split()))
# "."は障害物なし、"#"は障害物
s = [input() for l in range(hw[0])]
result = 0

# 各方向ごとの照らせるマス数を座標ごとに少ない計算量で事前計算することで、総計算量を定量的にする
L = [[0 for i in range(hw[1])] for j in range(hw[0])]
R = [[0 for i in range(hw[1])] for j in range(hw[0])]
U = [[0 for i in range(hw[1])] for j in range(hw[0])]
D = [[0 for i in range(hw[1])] for j in range(hw[0])]

for i in range(hw[0]):
    for j in range(hw[1]):
        if s[i][j] != "#":
            if j == 0:
                L[i][j] = 1
            else:
                L[i][j] = L[i][j-1]+1
                
            if i == 0:
                U[i][j] = 1
            else:
                U[i][j] = U[i-1][j]+1

for i in range(hw[0]-1, -1, -1):
    for j in range(hw[1]-1, -1, -1):
        if s[i][j] != "#":
            if j == hw[1]-1:
                R[i][j] = 1
            else:
                R[i][j] = R[i][j+1]+1
                
            if i == hw[0]-1:
                D[i][j] = 1
            else:
                D[i][j] = D[i+1][j]+1

# 4方向の明かりの合計値の最大を抽出
for i in range(hw[0]):
    for j in range(hw[1]):
        if s[i][j] != "#":
            tmp = L[i][j] + R[i][j] + U[i][j] + D[i][j] - 3
            if result < tmp:
                result = tmp
                
print(result)

Submission status: Time Limit Exceeded

Solution code:
def solve(string):
    h, w, *s = string.split()
    h, w = map(int, [h, w])
    ht = [[0] * w for _ in range(h)]
    vt = [[0] * h for _ in range(w)]
    for i in range(h):
        st = 0
        c = 0
        for j in range(w):
            if s[i][j] == "#":
                ht[i][st:j] = [c] * (j - st)
                st = j + 1
                c = 0
                continue
            c += 1
        ht[i][st:w] = [c] * (w - st)
    for i in range(w):
        st = 0
        c = 0
        for j in range(h):
            if s[j][i] == "#":
                vt[i][st:j] = [c] * (j - st)
                st = j + 1
                c = 0
                continue
            c += 1
        vt[i][st:h] = [c] * (h - st)

    ans = 0
    for i in range(h):
        for j in range(w):
            ans = max(ans, ht[i][j] + vt[j][i])
    return str(ans - 1)


if __name__ == '__main__':
    n, m = map(int, input().split())
    print(solve('{} {}\n'.format(n, m) + '\n'.join([input() for _ in range(n)])))

Submission status: Time Limit Exceeded

Solution code:
def solve(string):
    h, w, *s = string.split()
    h, w = map(int, [h, w])
    hg = []
    hi = []
    vg = []
    vi = []

    for _s in s:
        gn = 0
        _hi = [0] * w
        _hg = []
        c = 0
        for i, __s in enumerate(_s):
            if __s == "#":
                _hi[i] = -1
                gn += 1
                _hg.append(c)
                c = 0
                continue
            _hi[i] = gn
            c += 1
        _hg.append(c)
        hg.append(_hg)
        hi.append(_hi)
    for _s in zip(*s):
        gn = 0
        _vi = [0] * h
        _vg = []
        c = 0
        for i, __s in enumerate(_s):
            if __s == "#":
                _vi[i] = -1
                gn += 1
                _vg.append(c)
                c = 0
                continue
            _vi[i] = gn
            c += 1
        _vg.append(c)
        vg.append(_vg)
        vi.append(_vi)

    ans = max([max([hg[i][hi[i][j]] + vg[j][vi[j][i]] for j in range(w)]) for i in range(h)])
    return str(ans - 1)


if __name__ == '__main__':
    n, m = map(int, input().split())
    print(solve('{} {}\n'.format(n, m) + '\n'.join([input() for _ in range(n)])))

Submission status: Time Limit Exceeded

Solution code:
from itertools import accumulate


def solve(string):
    h, w, *s = string.split()
    h, w = map(int, [h, w])
    t = [[1 if __s == "#" else 0 for __s in _s] for _s in s]
    hi = [list(accumulate(_t)) for _t in t]
    vi = [list(accumulate(_t)) for _t in zip(*t)]
    hg = [list(map(len, _s.split("#"))) for _s in s]
    vg = [list(map(len, "".join(_s).split("#"))) for _s in zip(*s)]
    ans = max([max([hg[i][hi[i][j]] + vg[j][vi[j][i]] for j in range(w)]) for i in range(h)])
    return str(ans - 1)


if __name__ == '__main__':
    n, m = map(int, input().split())
    print(solve('{} {}\n'.format(n, m) + '\n'.join([input() for _ in range(n)])))