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())
t=[["#"]*(w+2)]+[list("#"+input()+"#")for i in range(h)]+[["#"]*(w+2)]
p=[[[0,0]for i in range(w+2)] for i in range(h+2)]
def  f(a,b,c):
    d=0
    if c=="h":
        while t[a][b]==".":
            a+=1
            d+=1
    else:
        while t[a][b]==".":
            b+=1
            d+=1
    return d
a=0
for i in range(1,h+1):
    for j in range(1,w+1):
        if t[i][j]!="#":
            if p[i-1][j][0]==0:
                p[i][j][0]=f(i,j,"h")
            else:
                p[i][j][0]=p[i-1][j][0]
            if p[i][j-1][1]==0:
                p[i][j][1]=f(i,j,"w")
            else:
                p[i][j][1]=p[i][j-1][1]
            a=max(a,sum(p[i][j]))
print(a-1)

Submission status: Time Limit Exceeded

Solution code:
def main():
    h,w = map(int,input().split())
    ls = [list(input()) for _ in range(h)]
    r = [[0 for _ in range(w)]  for _ in range(h)]
    l = [[0 for _ in range(w)]  for _ in range(h)]
    o = [[0 for _ in range(w)]  for _ in range(h)]
    u = [[0 for _ in range(w)]  for _ in range(h)]

    for i in range(h):
        for j in reversed(range(w)):
            if j == w-1:
                if ls[i][j]=='#':
                    r[i][j] = 0   
                else:
                    r[i][j] = 1
                    
            else:
                if ls[i][j]=='#':
                    r[i][j] = 0
                else:
                    r[i][j] = r[i][j+1]+1

    for i in range(h):
        for j in range(w):
            if j == 0:
                if ls[i][j]=='#':
                    pass
                else:
                    l[i][j]=1
            else:
                if ls[i][j]=='#':
                    pass
                else:
                    l[i][j] = l[i][j-1]+1
    for j in range(w):
        for i in range(h):
            if i == 0:
                if ls[i][j]=='#':
                    pass
                else:
                    o[i][j]=1
            else:
                if ls[i][j]=="#":
                    pass
                else:
                    o[i][j] = o[i-1][j] + 1
    for j in range(w):
        for i in reversed(range(h)):
            if i == h-1:
                if ls[i][j]=='#':
                    pass
                else:
                    u[i][j]=1
            else:
                if ls[i][j]=='#':
                    pass
                else:
                    u[i][j] = u[i+1][j] + 1

    ans = 0
    for i in range(h):
        for j in range(w):
            can = r[i][j] + l[i][j] + o[i][j] + u[i][j]
            ans = max(ans,can)
    print(ans-3)
if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
# import numpy as np
h,w = map(int,input().split())
field = []
accum=[[0 for i in range(w)] for j in range(h)]
accumrev=[[0 for i in range(w)] for j in range(h)]
stack = [[0 for i in range(w)] for j in range(h)]
stackrev = [[0 for i in range(w)] for j in range(h)]

for i in range(h):
    field.append(list(input()))
# field = np.array(field)

for i in range(h):
    s = field[i]
    temp = 0
    for j in range(w):
        if s[j] == "#":
            accum[i][j] = 0
            temp = 0
        else:
            temp += 1
            accum[i][j] = temp
    accum[i][-1] = temp
for i in range(h):
    s = field[i]
    temp = 0
    for j in range(1,w+1):
        if s[-j] == "#":
            accumrev[i][-j] = 0
            temp = 0
        else:
            temp += 1
            accumrev[i][-j] = temp
    accumrev[i][0] = temp
# print(field)
# print(np.array(accum))
# print(np.array(accumrev))

nf = list(zip(*field))

for i in range(w):
    s = nf[i]
    temp = 0
    for j in range(h):
        if s[j] == "#":
            stack[j][i] = 0
            temp = 0
        else:
            temp += 1
            stack[j][i] = temp
    stack[-1][i] = temp

for i in range(w):
    s = nf[i]
    temp = 0
    for j in range(1,h+1):
        if s[-j] == "#":
            stackrev[-j][i] = 0
            temp = 0
        else:
            temp += 1
            stackrev[-j][i] = temp
    stackrev[0][i] = temp

# print(field)
# print(np.array(stack))
# print(np.array(stackrev))

# accum = np.array(accum)
# accumrev = np.array(accumrev)
# stack = np.array(stack)
# stackrev = np.array(stackrev)



ans = 0

for i in range(h):
    for j in range (w):
        temp = accum[i][j] + accumrev[i][j] + stackrev[i][j] + stack[i][j]-3
        if temp > ans:
            ans = temp
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
h,w = map(int,input().split())
field = []
accum=[[0 for i in range(w)] for j in range(h)]
accumrev=[[0 for i in range(w)] for j in range(h)]
stack = [[0 for i in range(w)] for j in range(h)]
stackrev = [[0 for i in range(w)] for j in range(h)]

for i in range(h):
    field.append(list(input()))
field = np.array(field)

for i in range(h):
    s = field[i]
    temp = 0
    for j in range(w):
        if s[j] == "#":
            accum[i][j] = 0
            temp = 0
        else:
            temp += 1
            accum[i][j] = temp
    accum[i][-1] = temp
for i in range(h):
    s = field[i]
    temp = 0
    for j in range(1,w+1):
        if s[-j] == "#":
            accumrev[i][-j] = 0
            temp = 0
        else:
            temp += 1
            accumrev[i][-j] = temp
    accumrev[i][0] = temp
# print(field)
# print(np.array(accum))
# print(np.array(accumrev))

nf = np.transpose(field)

for i in range(w):
    s = nf[i]
    temp = 0
    for j in range(h):
        if s[j] == "#":
            stack[j][i] = 0
            temp = 0
        else:
            temp += 1
            stack[j][i] = temp
    stack[-1][i] = temp

for i in range(w):
    s = nf[i]
    temp = 0
    for j in range(1,h+1):
        if s[-j] == "#":
            stackrev[-j][i] = 0
            temp = 0
        else:
            temp += 1
            stackrev[-j][i] = temp
    stackrev[0][i] = temp

# print(field)
# print(np.array(stack))
# print(np.array(stackrev))

accum = np.array(accum)
accumrev = np.array(accumrev)
stack = np.array(stack)
stackrev = np.array(stackrev)

tot = accum+accumrev+stack+stackrev-3
# print(tot)
print(np.max(tot))

Submission status: Time Limit Exceeded

Solution code:
h, w  = map(int, input().split())
slist = [input() for i in range(h)]
## 1.横→縦
# 横方向maxとなるj
max_empty_yoko = 0
max_idx_yoko = [None, None, None] #[istart, iend, j]
for j, s in enumerate(slist):
  tmp_max_empty = 0
  tmp_max_idxj =[None, None, None]
  for index in range(len(s)):
    if s[index] == '.':
      if tmp_max_empty == 0:
        tmp_max_idxj[0] = index
      tmp_max_empty += 1
    else:
      if max_empty_yoko < tmp_max_empty:
        max_empty_yoko = tmp_max_empty
        max_idx_yoko[0] = tmp_max_idxj[0]
        max_idx_yoko[1] = index-1
        max_idx_yoko[2] = j
      tmp_max_empty = 0
      tmp_max_idxj = [None, None, None]
        

  if max_empty_yoko < tmp_max_empty:
    max_empty_yoko = tmp_max_empty
    max_idx_yoko[0] = tmp_max_idxj[0]
    max_idx_yoko[1] = index-1
    max_idx_yoko[2] = j

max_empty_tate = 0
max_idx_tate = [None, None, None]
for i in range(max_idx_yoko[0],max_idx_yoko[1]):
  tmp_max_empty = 0
  tmp_max_idxi = [None, None, None]
  for j in range(h):
    if slist[j][i] == '.':
      if tmp_max_empty == 0:
        tmp_max_idxi[0] = j
      tmp_max_empty += 1
    else:
      if max_empty_tate < tmp_max_empty:
        max_empty_tate = tmp_max_empty
        max_idx_tate[0] = tmp_max_idxi[0]
        max_idx_tate[1] = j-1
        max_idx_tate[2] = i
      tmp_max_empty = 0
      tmp_max_idxi = [None, None, None]

  if max_empty_tate < tmp_max_empty:
    max_empty_tate = tmp_max_empty
    max_idx_tate[0] = tmp_max_idxi[0]
    max_idx_tate[1] = j
    max_idx_tate[2] = i

# ans1
ans1 = max_empty_yoko+max_empty_tate-1

## 2.縦→横
# 縦方向maxとなるi
max_empty_tate = 0
max_idx_tate = [None, None, None]
for i in range(w):
  tmp_max_empty = 0
  tmp_max_idxi = [None, None, None]
  for j in range(h):
    if slist[j][i] == '.':
      if tmp_max_empty == 0:
        tmp_max_idxi[0] = j
      tmp_max_empty += 1
    else:
      if max_empty_tate < tmp_max_empty:
        max_empty_tate = tmp_max_empty
        max_idx_tate[0] = tmp_max_idxi[0]
        max_idx_tate[1] = j-1
        max_idx_tate[2] = i
      tmp_max_empty = 0
      tmp_max_idxi = [None, None, None]

  if max_empty_tate < tmp_max_empty:
    max_empty_tate = tmp_max_empty
    max_idx_tate[0] = tmp_max_idxi[0]
    max_idx_tate[1] = j
    max_idx_tate[2] = i

# 横方向maxとなるj
max_empty_yoko = 0
max_idx_yoko = [None, None, None] #[istart, iend, j]
for j, s in enumerate(slist):
  if j < max_idx_tate[0] and  max_idx_tate[1] < j:
    continue
  tmp_max_empty = 0
  tmp_max_idxj =[None, None, None]
  for index in range(len(s)):
    if s[index] == '.':
      if tmp_max_empty == 0:
        tmp_max_idxj[0] = index
      tmp_max_empty += 1
    else:
      if max_empty_yoko < tmp_max_empty:
        max_empty_yoko = tmp_max_empty
        max_idx_yoko[0] = tmp_max_idxj[0]
        max_idx_yoko[1] = index-1
        max_idx_yoko[2] = j
      tmp_max_empty = 0
      tmp_max_idxj = [None, None, None]
        

  if max_empty_yoko < tmp_max_empty:
    max_empty_yoko = tmp_max_empty
    max_idx_yoko[0] = tmp_max_idxj[0]
    max_idx_yoko[1] = index-1
    max_idx_yoko[2] = j
# ans2
ans2 = max_empty_yoko+max_empty_tate-1

## ans
print(max(ans1,ans2))

Submission status: Time Limit Exceeded

Solution code:
H, W = map(int, input().split())
field = [input() for i in range(H)]
verti_lamp = [[0]*W for i in range(H)]
horiz_lamp = [[0]*W for i in range(H)]


def verti_search(y, x):
    count = 0
    down = 1
    while y+down <= H-1 and field[y+down][x] =='.':
        count += 1
        down += 1
    y += down
    return count, y


def horiz_search(y, x):
    count = 0
    right = 1
    while x+right <= W-1 and field[y][x+right] =='.':
        count += 1
        right += 1
    x += right
    return count, x


for y in range(H):
    x = 0
    while x <= W-1:
        if field[y][x] == '#':
            x += 1
            continue
        else:
            horiz, nx = horiz_search(y, x)
            for t in range(x, nx):
                horiz_lamp[y][t] = horiz
            x = nx

for x in range(W):
    y = 0
    while y <= H-1:
        if field[y][x] == '#':
            y += 1
            continue
        else:
            verti, ny = verti_search(y, x)
            for t in range(y, ny):
                verti_lamp[t][x] = verti
            y = ny

ans = 0
for y in range(H):
    for x in range(W):
        candidate = verti_lamp[y][x] + horiz_lamp[y][x] + 1
        ans = max(ans, candidate)

print(ans)

Submission status: Time Limit Exceeded

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

for i in range(h):
    A[i]=[1 if i=='.' else 0 for i in list(input())]

for ip,a in enumerate(A):
    p1=[]
    p2=[]
    if a[0]==1:p1.append(0)
    for i in range(1,len(a)):
        if a[i-1]==0 and a[i]==1:p1.append(i)
        if a[i-1]==1 and a[i]==0:p2.append(i)
    if a[-1]==1:p2.append(len(a))
        
    
    for s1,g1 in zip(p1,p2):
        for j in range(s1,g1):
            s[ip][j]=g1-s1
            
for ip,a in enumerate(zip(*A)):
    p1=[]
    p2=[]
    if a[0]==1:p1.append(0)
    for i in range(1,len(a)):
        if a[i-1]==0 and a[i]==1:p1.append(i)
        if a[i-1]==1 and a[i]==0:p2.append(i)
    if a[-1]==1:p2.append(len(a))
        
    
    for s1,g1 in zip(p1,p2):
        for j in range(s1,g1):
            t[ip][j]=g1-s1

ans=0
for i in range(w):
	for j in range(h):
		ans=max(ans,s[j][i]+t[i][j])
print(ans-1)

Submission status: Time Limit Exceeded

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

from collections import deque

def main(): 
    H,W = map(int,input().split())
    S = [input().split() for _ in range(H)]
    for i in range(H):
        S[i]=list(S[i][0])
    grid = [[0]*W for _ in range(H)]
    
    for i in range(H):
        l=0
        for j in range(W):
            if S[i][j] == "#":
                for k in range(1,l+1):
                    grid[i][j-k]+=l
                l=0
            else:
                l+=1
        for k in range(1,l+1):
                    grid[i][W-k]+=l
    max=0
    for j in range(W):
        l=0
        for i in range(H):
            if S[i][j] == "#":
                for k in range(1,l+1):
                    grid[i-k][j]+=l
                    if grid[i-k][j] > max:
                        max = grid[i-k][j]
                l=0
            else:
                l+=1
        for k in range(1,l+1):
            grid[H-k][j]+=l
            if grid[H-k][j] > max:
                        max = grid[H-k][j]
    print(max-1)
if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
H,W=map(int,input().split())
field=[]
L=[]
R=[]
U=[]
D=[]
tmp=[0]*W
for _ in range(H):
  field.append(input())
  L.append(tmp.copy())
  R.append(tmp.copy())
  U.append(tmp.copy())
  D.append(tmp.copy())
for i in range(H):
  for j in range(W):
    if field[i][j]=='.':
      if j==0:
        L[i][j]=1
      else:
        L[i][j]=L[i][j-1]+1
    j_r=W-1-j
    if field[i][j_r]==".":
      if j_r!=W-1:
        R[i][j_r]=R[i][j_r+1]+1
      else:
        R[i][j_r]=1 
    if field[i][j]=='.':
      if i==0:
        U[i][j]=1
      else:
        U[i][j]=U[i-1][j]+1
    i_u=H-1-i
    if field[i_u][j]=='.':
      if i_u==H-1:
        D[i_u][j]=1
      else:
        D[i_u][j]=D[i_u+1][j]+1
ans=0
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)
i=0
j=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
H,W=map(int,input().split())
field=[]
L=[]
R=[]
U=[]
D=[]
tmp=[0]*W
for _ in range(H):
  field.append(input())
  L.append(tmp.copy())
  R.append(tmp.copy())
  U.append(tmp.copy())
  D.append(tmp.copy())
for i in range(H):
  for j in range(W):
    if field[i][j]=='.':
      if j==0:
        L[i][j]=1
      else:
        L[i][j]=L[i][j-1]+1
    j_r=W-1-j
    if field[i][j_r]==".":
      if j_r!=W-1:
        R[i][j_r]=R[i][j_r+1]+1
      else:
        R[i][j_r]=1 
for j in range(W):
  for i in range(H):
    if field[i][j]=='.':
      if i==0:
        U[i][j]=1
      else:
        U[i][j]=U[i-1][j]+1
    i_u=H-1-i
    if field[i_u][j]=='.':
      if i_u==H-1:
        D[i_u][j]=1
      else:
        D[i_u][j]=D[i_u+1][j]+1
ans=0
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)
i=0
j=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
H,W=map(int,input().split())
field=[]
for _ in range(H):
  field.append(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)]
for i in range(H):
  for j in range(W):
    if field[i][j]=='.':
      if 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,-1,-1):
    if field[i][j]==".":
      if j!=W-1:
        R[i][j]=R[i][j+1]+1
      else:
        R[i][j]=1
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 j in range(W):
  for i in range(H):
    if field[i][j]=='.':
      if i==0:
        U[i][j]=1
      else:
        U[i][j]=U[i-1][j]+1
for j in range(W):
  for i in range(H-1,-1,-1):
    if field[i][j]=='.':
      if 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):
    ans=max(ans,L[i][j]+R[i][j]+D[i][j]+U[i][j]-3)
i=0
j=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def main():
    H, W = map(int, input().split())
    matrix = [list(input()) for _ in range(H)]
    L = [[0 for _ in range(W)] for _ in range(H)]
    R = [[0 for _ in range(W)] for _ in range(H)]
    D = [[0 for _ in range(W)] for _ in range(H)]
    U = [[0 for _ in range(W)] for _ in range(H)]
    S = [[0 for _ in range(W)] for _ in range(H)]

    # 左方向についてカウントする
    for i in range(H):
        if matrix[i][0] == '.':
            L[i][0] = 1
            S[i][0] += 1
        for j in range(1, W):
            s = matrix[i][j]
            if s == '.':
                L[i][j] = L[i][j-1] + 1
                S[i][j] += L[i][j]

    # 右方向についてカウントする
    for i in range(H):
        if matrix[i][-1] == '.':
            R[i][-1] = 1
            S[i][-1] += 1
        for j in range(W-2, -1, -1):
            s = matrix[i][j]
            if s == '.':
                R[i][j] = R[i][j+1] + 1
                S[i][j] += R[i][j]
    
    # 上方向についてカウントする
    for i in range(W):
        if matrix[0][i] == '.':
            U[0][i] = 1
            S[0][i] += 1
        for j in range(1, H):
            s = matrix[j][i]
            if s == '.':
                U[j][i] = U[j-1][i] + 1
                S[j][i] += U[j][i]
    
    # 下方向についてカウントする
    for i in range(W):
        if matrix[-1][i] == '.':
            D[-1][i] = 1
            S[-1][i] += 1
        for j in range(H-2, -1, -1):
            s = matrix[j][i]
            if s == '.':
                D[j][i] = D[j+1][i] + 1
                S[j][i] += D[j][i]
    
    
    max_val = -3
    for i in range(H):
        for j in range(W):
            max_val = max(max_val, S[i][j])
    print(max_val-3)
    return

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

def main():
    H, W = map(int, input().split())
    matrix = np.array([np.array(list(input())) for _ in range(H)])
    count_matrix = np.array([np.array([-1 for _ in range(W)]) for _ in range(H)])

    # 横方向についてカウントする
    for i, row in enumerate(matrix):
        start = 0
        count = 0
        for j, s in enumerate(row):
            if s == '.':
                count += 1
                if j == W-1:
                    for k in range(start, j+1):
                        count_matrix[i][k] += count
            else:
                for k in range(start, j):
                    count_matrix[i][k] += count
                start = j+1
                count = 0

    # 縦方向についてカウントする
    for i in range(W):
        column = matrix[:, i]
        start = 0
        count = 0
        for j, s in enumerate(column):
            if s == '.':
                count += 1
                if j == H-1:
                    for k in range(start, j+1):
                        count_matrix[k][i] += count
            else:
                for k in range(start, j):
                    count_matrix[k][i] += count
                start = j+1
                count = 0
    
    print(np.amax(count_matrix))
    return

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
def main():
  _range, _readline = range, stdin.readline
  h, w = map(int, _readline().split())
  s = [_readline().rstrip() + '#' for _ in _range(h)]
  s.append('#' * (w + 1))
  t = [[-1] * w for _ in _range(h + 1)]
  tstart = [-1] * (w + 1)
  for i in _range(h + 1):
    ystart = -1
    si = s[i]
    ti = t[i]
    for j in _range(w + 1):
      tj = tstart[j]
      if si[j] == '#':
        if ystart != -1:
          n = j - ystart
          for k in _range(ystart, j):
            ti[k] += n
          ystart = -1
        if tj != -1:
          n = i - tj
          for k in _range(tj, i):
            t[k][j] += n
          tstart[j] = -1
      else:
        if ystart == -1:
          ystart = j
        if tj == -1:
          tstart[j] = i
  print(max(map(max, t)))
main()

Submission status: Time Limit Exceeded

Solution code:
def main():
  _range = range
  h, w = map(int, input().split())
  s = [input() + '#' for _ in _range(h)]
  s.append('#' * (w + 1))
  t = [[-1] * w for _ in _range(h + 1)]
  tstart = [-1] * (w + 1)
  for i in _range(h + 1):
    ystart = -1
    si = s[i]
    ti = t[i]
    for j in _range(w + 1):
      tj = tstart[j]
      if si[j] == '#':
        if ystart != -1:
          n = j - ystart
          for k in _range(ystart, j):
            ti[k] += n
          ystart = -1
        if tj != -1:
          n = i - tj
          for k in _range(tj, i):
            t[k][j] += n
          tstart[j] = -1
      else:
        if ystart == -1:
          ystart = j
        if tj == -1:
          tstart[j] = i
  print(max(map(max, t)))
main()

Submission status: Time Limit Exceeded

Solution code:
def main():
  _range = range
  h, w = map(int, input().split())
  s = [input() + '#' for _ in _range(h)]
  s.append('#' * (w + 1))
  t = [[-1] * w for _ in _range(h + 1)]
  tstart = [-1] * (w + 1)
  for i in _range(h + 1):
    ystart = -1
    si = s[i]
    ti = t[i]
    for j in _range(w + 1):
      if si[j] == '#':
        if ystart != -1:
          n = j - ystart
          for k in _range(ystart, j):
            ti[k] += n
          ystart = -1
        if tstart[j] != -1:
          n = i - tstart[j]
          for k in _range(tstart[j], i):
            t[k][j] += n
          tstart[j] = -1
      else:
        if ystart == -1:
          ystart = j
        if tstart[j] == -1:
          tstart[j] = i
  print(max(map(max, t)))
main()

Submission status: Time Limit Exceeded

Solution code:
def main():
  _range = range
  h, w = map(int, input().split())
  s = [input() + '#' for _ in _range(h)]
  s.append('#' * (w + 1))
  t = [[-1] * w for _ in _range(h + 1)]
  tstart = [-1] * (w + 1)
  for i in _range(h + 1):
    ystart = -1
    si = s[i]
    ti = t[i]
    for j in _range(w + 1):
      if si[j] == '#':
        if ystart != -1:
          for k in _range(ystart, j):
            ti[k] += j - ystart
          ystart = -1
        if tstart[j] != -1:
          for k in _range(tstart[j], i):
            t[k][j] += i - tstart[j]
          tstart[j] = -1
      else:
        if ystart == -1:
          ystart = j
        if tstart[j] == -1:
          tstart[j] = i
  print(max(map(max, t)))
main()

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

def main():
	H,W = map(int,input().split())
	dis_list1 = []
	dis_list2 = []
	for i in range(H):
		st = input()
		dis_list1.append([0 if i == "#" else 1 for i in list(st)])
		dis_list2.append([0 if i == "#" else 1 for i in list(st)])
	w_list = dis_list1
	h_list = dis_list2
	for i in range(H):
		for j in range(W-1):
			if w_list[i][j+1] != 0:
				w_list[i][j+1] = w_list[i][j]+1
	for i in range(H):
		for j in range(W-1):
			if w_list[i][-j-2] != 0 and w_list[i][-j-1] != 0:
				w_list[i][-j-2] = w_list[i][-j-1]

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

	w_list = np.array(w_list)
	h_list = np.array(h_list)

	sum_list = w_list+h_list

	print(sum_list.max()-1)		

if __name__ == "__main__":
	main()

Submission status: Time Limit Exceeded

Solution code:
H, W = map(int,input().split())
S = [list(input()) for _ in range(H)]
table = [[0 for _ in range(W)] for _ in range(H)]
def dfsW(n):
  global table
  l = -1
  r = -1
  i = 0
  while i < W:
    if S[n][i] == ".":
      if l == -1:
        l = i
      r = i
    else:
      if l != -1:
        for j in range(l, r + 1):
          table[n][j] += r - l + 1
      l = -1
      r = -1
    i += 1
  if l != -1:
    for j in range(l, r + 1):
      table[n][j] += r - l + 1


def dfsH(n):
  global table
  l = -1
  r = -1
  i = 0
  while i < H:
    if S[i][n] == ".":
      if l == -1:
        l = i
      r = i
    else:
      if l != -1:
        for j in range(l, r + 1):
          table[j][n] += r - l + 1
      l = -1
      r = -1
    i += 1
    
  if l != -1:
    for j in range(l, r + 1):
      table[j][n] += r - l + 1


for i in range(H):
  dfsW(i)
for i in range(W):
  dfsH(i)
print(max(map(max, table)) - 1)

Submission status: Time Limit Exceeded

Solution code:
from itertools import groupby

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

res = 0
count = [[-1 * int(Ss[i][j] == ".") for j in range(W)] for i in range(H)]
for i in range(H):
    j = 0
    for x in groupby(Ss[i]):
        k = len(list(x[1]))
        for _ in range(k):
            count[i][j] += k * (x[0] == ".")
            j += 1

for j in range(W):
    i = 0
    for x in groupby([Ss[i][j] for i in range(H)]):
        k = len(list(x[1]))
        for _ in range(k):
            new_res = count[i][j] + k * (x[0] == ".")
            if res < new_res:
                res = new_res
            i += 1

print(res)

Submission status: Time Limit Exceeded

Solution code:
from itertools import groupby

[H, W] = map(int, input().split())
Ss = [list(map( lambda x: int(x == "."), list(input()) )) for _ in range(H)]

res = 0
count = [[-Ss[i][j] for j in range(W)] for i in range(H)]
for i in range(H):
    j = 0
    for x in groupby(Ss[i]):
        k = len(list(x[1]))
        for _ in range(k):
            count[i][j] += k * x[0]
            j += 1

for j in range(W):
    i = 0
    for x in groupby([Ss[i][j] for i in range(H)]):
        k = len(list(x[1]))
        for _ in range(k):
            new_res = count[i][j] + k * x[0]
            if res < new_res:
                res = new_res
            i += 1

print(res)

Submission status: Time Limit Exceeded

Solution code:
from itertools import groupby

[H, W] = map(int, input().split())
Ss = [list(map( lambda x: int(x == "."), list(input()) )) for _ in range(H)]

w_count = [[] for _ in range(H)]
for i in range(H):
    for x in groupby(Ss[i]):
        k = len(list(x[1]))
        for _ in range(k):
            w_count[i].append(k * x[0])

h_count = [[0 for _ in range(W)] for _ in range(H)]
for j in range(W):
    i = 0
    for x in groupby([Ss[i][j] for i in range(H)]):
        k = len(list(x[1]))
        for _ in range(k):
            h_count[i][j] = k * x[0]
            i += 1

res = max([max([w_count[i][j] + h_count[i][j] - Ss[i][j] for j in range(W)]) for i in range(H)])

print(res)

Submission status: Time Limit Exceeded

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

L = [[0]*w for _ in range(h)]
R = [[0]*w for _ in range(h)]
D = [[0]*w for _ in range(h)]
U = [[0]*w for _ in range(h)]

for i in range(h):
    cnt_L = 1
    for j in range(w):
        if (lst[i][j] == '#'):
            cnt_L =1
        else:
            L[i][j] = cnt_L
            cnt_L += 1
            
    cnt_R = 1
    for k in range(w)[::-1]:
        if (lst[i][k] == '#'):
            cnt_R = 1
        else:
            R[i][k] = cnt_R
            cnt_R += 1

for i in range(w):
    cnt_U = 1
    for j in range(h):
        if (lst[j][i] == '#'):
            cnt_U = 1
        else:
            U[j][i] = cnt_U
            cnt_U += 1
    
    cnt_D = 1
    for k in range(h)[::-1]:
        if (lst[k][i] == '#'):
            cnt_D = 1
        else:
            D[k][i] = cnt_D
            cnt_D += 1

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

Submission status: Time Limit Exceeded

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

L = [[0]*w for _ in range(h)]
R = [[0]*w for _ in range(h)]
D = [[0]*w for _ in range(h)]
U = [[0]*w for _ in range(h)]

for i in range(h):
    cnt = 1
    for j in range(w):
        if (lst[i][j] == '#'):
            cnt =1
        else:
            L[i][j] = cnt
            cnt += 1

for i in range(h):
    cnt = 1
    for j in range(w)[::-1]:
        if (lst[i][j] == '#'):
            cnt =1
        else:
            R[i][j] = cnt
            cnt += 1

for i in range(w):
    cnt = 1
    for j in range(h):
        if (lst[j][i] == '#'):
            cnt = 1
        else:
            U[j][i] = cnt
            cnt += 1

for i in range(w):
    cnt = 1
    for j in range(h)[::-1]:
        if (lst[j][i] == '#'):
            cnt = 1
        else:
            D[j][i] = cnt
            cnt += 1

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

Submission status: Time Limit Exceeded

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

res = 0
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):
    for j in range(h):
        if lst[j][i] == '.':
            if i == 0:
                L[j][i] = 1
            else:
                L[j][i] = L[j][i-1] + 1

for i in range(h):
    for j in range(w):
        if lst[i][j] == '.':
            if 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)):
        if lst[i][j] == '.':
            if i == h-1:
                D[i][j] = 1
            else:
                D[i][j] = D[i+1][j] + 1

for i in reversed(range(w)):
    for j in reversed(range(h)):
        if lst[j][i] == '.':
            if i == w-1:
                R[j][i] = 1
            else:
                R[j][i] = R[j][i+1] + 1

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