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:
# -*- coding:utf-8 -*-
"""解説
https://www.youtube.com/watch?v=L8grWxBlIZ4

PDFに書いてある実装をする。

....#.
..#...
.#..#.

それぞれのマスについて、

・左マスを照らせる個数: L[i][j]
・右マスを照らせる個数: R[i][j]
・上マスを照らせる個数: U[i][j]
・下マスを照らせる個数: D[i][j]

をそれぞれ計算する。

左マスを照らす漸化式L[i][j]は、

・i行j列に障害物がある場合はL[i][j]
・そうでないとき、j=1ならば、L[i][j] = 1
・そうでないとき、j>1ならば、L[i][j] = L[i][j-1] + 1

求める答えは、L[i][j]+R[i][j]+U[i][j]+D[i][j]-3 の最大値


計算量はO(HW)。

H,W = 2000なので、O(HW)はO(4*10**6)。 Pythonでこの計算量は無理だろと思って実装を諦めたが、いけるっぽい。認識を改めた。というかPyPyもあるし。

"""
import numpy as np

def solve():
    H, W = list(map(int, input().split()))
    grid = []
    for i in range(H):
        grid.append(list(input()))

    L = np.zeros((H,W), dtype=int)
    R = np.zeros((H,W), dtype=int)
    U = np.zeros((H,W), dtype=int)
    D = np.zeros((H,W), dtype=int)
    
    for i in range(H):
        for j in range(W):
            if grid[i][j] == "#":
                L[i][j] = 0
                continue
            if j == 0:
                L[i][j] = 1
                continue
            L[i][j] = L[i][j-1] + 1
    
    for i in range(H):
        for j in range(W-1, -1, -1):
            if grid[i][j] == "#":
                R[i][j] = 0
                continue
            if j == W-1:
                R[i][j] = 1
                continue
            R[i][j] = R[i][j+1] + 1

    for j in range(W):
        for i in range(H):
            if grid[i][j] == "#":
                D[i][j] = 0
                continue
            if i == 0:
                D[i][j] = 1
                continue
            D[i][j] = D[i-1][j] + 1

    for j in range(W):
        for i in range(H-1, -1, -1):
            if grid[i][j] == "#":
                U[i][j] = 0
                continue
            if i == H-1:
                U[i][j] = 1
                continue
            U[i][j] = U[i+1][j] + 1

    ans = np.max(L+R+D+U-3)

    print(ans)

if __name__ == "__main__":
    solve()

Submission status: Time Limit Exceeded

Solution code:
#上下方向の探索と左右方向の探索をまとめる
#まず表を完成させる-->全マスの探索
H, W = map(int, input().split())
lst = [['#'] * (W+2)]
lst += [['#'] + list(input()) + ['#'] for i in range(H)]
lst += [['#'] * (W+2)]


lst_ue = [[0] * (W+2) for i in range(H+2)]
lst_shita = [[0] * (W+2) for i in range(H+2)]
lst_migi = [[0] * (W+2) for i in range(H+2)]
lst_hidari = [[0] * (W+2) for i in range(H+2)]

for i in range(1, H+1):
    for j in range(1, W+1):
        lst_hidari[i][j] = 0 if lst[i][j] == '#' else 1 + lst_hidari[i][j-1]
    for j in range(W, 0, -1):
        lst_migi[i][j] = 0 if lst[i][j] == '#' else 1 + lst_migi[i][j+1]

for j in range(1, W+1):
    for i in range(1, H+1):
        lst_ue[i][j] = 0 if lst[i][j] == '#' else 1 + lst_ue[i-1][j]
    for i in range(H+1, 0, -1):
        lst_shita[i][j] = 0 if lst[i][j] == '#' else 1 + lst_shita[i+1][j]

ans = 0
for i in range(1,H+1):
    for j in range(1, W+1):
        ans = max(ans, lst_ue[i][j]+lst_shita[i][j]+lst_migi[i][j]+lst_hidari[i][j]-3)

print (ans)

Submission status: Time Limit Exceeded

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

S = [input() for i in range(H)]

L = [[0 for x in range(W)] for y in range(H)]
R = [[0 for x in range(W)] for y in range(H)]
U = [[0 for x in range(W)] for y in range(H)]
D = [[0 for x in range(W)] for y in range(H)]


for i in range(H) :
    flg = 0
    for j in range(W) :
        if S[i][j] == "#" :
            flg = 1
        elif flg :
            flg = 0
        elif j == 0 :
            L[i][j] = 0
        else :
            L[i][j] = L[i][j-1] + 1
    
    flg = 0
    for j in range(W-1,-1,-1) :
        if S[i][j] == "#" :
            flg = 1
        elif flg :
            flg = 0
        elif j == W-1 :
            R[i][j] = 0
        else :
            R[i][j] = R[i][j+1] + 1

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

ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys

sys.setrecursionlimit(10 ** 9)

H, W = map(int, input().split())
lst = [['#'] * (W+2)]
lst += [['#'] + list(input()) + ['#'] for i in range(H)]
lst += [['#'] * (W+2)]


lst_ue = [[-1] * (W+2) for i in range(H+2)]
lst_shita = [[-1] * (W+2) for i in range(H+2)]
lst_migi = [[-1] * (W+2) for i in range(H+2)]
lst_hidari = [[-1] * (W+2) for i in range(H+2)]

# print (lst_ue)
def ue(i, j):
    if lst_ue[i][j] != -1:
        return lst_ue[i][j]
    if lst[i][j] == '#':
        lst_ue[i][j] = 0
        return 0
    else:
        lst_ue[i][j] = ue(i-1, j) + 1
        return lst_ue[i][j]

def shita(i, j):
    if lst_shita[i][j] != -1:
        return lst_shita[i][j]
    if lst[i][j] == '#':
        lst_shita[i][j] = 0
        return 0
    else:
        lst_shita[i][j] = shita(i+1, j) + 1
        return lst_shita[i][j]        

def hidari(i, j):
    if lst_hidari[i][j] != -1:
        return lst_hidari[i][j]
    if lst[i][j] == '#':
        lst_hidari[i][j] = 0
        return 0
    else:
        lst_hidari[i][j] = hidari(i, j-1) + 1
        return lst_hidari[i][j]        

def migi(i, j):
    if lst_migi[i][j] != -1:
        return lst_migi[i][j]
    if lst[i][j] == '#':
        lst_migi[i][j] = 0
        return 0
    else:
        lst_migi[i][j] = migi(i, j+1) + 1
        return lst_migi[i][j]        

ans = 0

for i in range(1,H+1):
    for j in range(1, W+1):
        if lst[i][j] != '#':
            ans = max(ans, ue(i,j)+shita(i,j)+hidari(i,j)+migi(i,j)-3)
            if ans == H + W -1:
                print (ans)
                sys.exit()
print (ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

H, W = map(int, input().split())
S = [input() for _ in range(H)]
 
up =    np.zeros((H+2, W+2), dtype=int)
down =  np.zeros((H+2, W+2), dtype=int)
left =  np.zeros((H+2, W+2), dtype=int)
right = np.zeros((H+2, W+2), dtype=int)

for i in range(1, H+1):
    for j in range(1, W+1):
        if S[i-1][j-1] == '.':
            up[i][j] = up[i-1][j] + 1
            left[i][j] = left[i][j-1] + 1
        
        if S[-i][-j] == '.':
            down[-1-i][-1-j] = down[-i][-1-j] + 1
            right[-1-i][-1-j] = right[-1-i][-j] + 1

print(np.max(up+down+left+right)-3)

Submission status: Time Limit Exceeded

Solution code:
H, W = map(int, input().split())
S = [input() for _ in range(H)]
 
up =    [[0]*(W+2) for _ in range(H+2)]
down =  [[0]*(W+2) for _ in range(H+2)]
left =  [[0]*(W+2) for _ in range(H+2)]
right = [[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-1][j-1] == '.':
            up[i][j] = up[i-1][j] + 1
            left[i][j] = left[i][j-1] + 1
        
        if S[-i][-j] == '.':
            down[-1-i][-1-j] = down[-i][-1-j] + 1
            right[-1-i][-1-j] = right[-1-i][-j] + 1

ans = 0
for i in range(1, H+1):
    for j in range(1, W+1):
        ans = max(ans, up[i][j]+down[i][j]+left[i][j]+right[i][j]-3)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
h, w = map(int, input().split())
s=[]
for _ in range(h):
    s.append(input())
def torf(i, x, y):
    counter=0
    if i==0:
        while x>=0 and s[x][y]==".":
            counter+=1
            x-=1
    elif i==1:
        while y<w and s[x][y]==".":
            counter+=1
            y+=1
    elif i==2:
        while x<h and s[x][y]==".":
            counter+=1
            x+=1
    else :
        while y>=0 and s[x][y]==".":
            counter+=1
            y-=1
    return counter-1

def search(x, y):
    d=0
    for i in range(4):
        d+=torf(i, x, y)
    return d+1
max = 0
for i in range(h):
    for j in range(w):

        if max < search(i, j):
            max = search(i, j)
print (max)

Submission status: Time Limit Exceeded

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

s=[]
for _ in range(h):
    s.append(input())

def torf(i, x, y):
    counter=0
    if i==0:
        while x>=0 and s[x][y]==".":
            counter+=1
            x-=1
    elif i==1:
        while y<w and s[x][y]==".":
            counter+=1
            y+=1
    elif i==2:
        while x<h and s[x][y]==".":
            counter+=1
            x+=1
    else :
        while y>=0 and s[x][y]==".":
            counter+=1
            y-=1
    return counter-1

def search(x, y):
    d=0
    if s[x][y] == "#":
        return d
    else :
        for i in range(4):
            d+=torf(i, x, y)
        return d+1
max = 0
for i in range(h):
    for j in range(w):
        if max < search(i, j):
            max = search(i, j)
print (max)

Submission status: Time Limit Exceeded

Solution code:
h,w=map(int,input().split())
s=''
for i in range(h):
  s+=input()
n=len(s)
def down(k):
  num=1
  K=k
  while K+w<n:
    if s[K+w]=='.':
      num+=1
      K=K+w
    else:
      break
  return num
def up(k):
  num=1
  K=k
  while K-w>=0:
    if s[K-w]=='.':
      num+=1
      K=K-w
    else:
      break
  return num
def left(k):
  num=1
  K=k
  while K%w!=0:
    if s[K-1]=='.':
      num+=1
      K=K-1
    else:
      break
  return num
def right(k):
  num=1
  K=k
  while K%w!=w-1:
    if s[K+1]=='.':
      num+=1
      K=K+1
    else:
      break
  return num
check=[[up(i),down(i),right(i),left(i)]for i in range(n)]
ans=0
for j in range(n):
  if s[j]=='.':
    ans=max(ans,check[j][0]+check[j][1]+check[j][2]+check[j][3]-3)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
h,w=map(int,input().split())
s=''
for i in range(h):
  s+=input()
n=len(s)
def down(k):
  num=1
  K=k
  while K+w<n:
    if s[K+w]=='.':
      num+=1
      K=K+w
    else:
      break
  return num
def up(k):
  num=1
  K=k
  while K-w>=0:
    if s[K-w]=='.':
      num+=1
      K=K-w
    else:
      break
  return num
def left(k):
  num=1
  K=k
  while K%w!=0:
    if s[K-1]=='.':
      num+=1
      K=K-1
    else:
      break
  return num
def right(k):
  num=1
  K=k
  while K%w!=w-1:
    if s[K+1]=='.':
      num+=1
      K=K+1
    else:
      break
  return num
check=[[up(i),down(i),right(i),left(i)]for i in range(n)]
ans=0
for j in range(n):
  if s[j]=='.':
    ans=max(ans,check[j][0]+check[j][1]+check[j][2]+check[j][3]-3)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
import numpy as np

h, w = list(map(int, input().split()))
field = [line.rstrip() for line in sys.stdin]

l = [[0] * (w + 2) for _ in [0] * (h + 2)]
r = [[0] * (w + 2) for _ in [0] * (h + 2)]
u = [[0] * (w + 2) for _ in [0] * (h + 2)]
d = [[0] * (w + 2) for _ in [0] * (h + 2)]

for i in range(1, h + 1):
    fi = field[i - 1]
    l_tmp = 0
    li = l[i]
    for j in range(1, w + 1):
        c = fi[j - 1]
        u[i][j] = u[i - 1][j] + 1 if c == '.' else 0
        li[j] = l_tmp = l_tmp + 1 if c == '.' else 0
for i in range(h, 0, -1):
    fi = field[i - 1]
    r_tmp = 0
    ri = r[i]
    for j in range(w, 0, -1):
        c = fi[j - 1]
        d[i][j] = d[i + 1][j] + 1 if c == '.' else 0
        ri[j] = r_tmp = r_tmp + 1 if c == '.' else 0

print(sum(map(np.array, [l, r, u, d])).max() - 3)

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:[y,x][(y and x and 1)],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:
import numpy as np 

H,W = map(int,input().split())
S = np.array(
    list(map(lambda x:list(map(
        lambda x:[0,1][x == "."],list(input())
    )),range(H)
)))

funcA = np.frompyfunc(lambda x,y:y and x+y,2,1)
funcB = np.frompyfunc(lambda x,y:[y,x][(y and x and 1)],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:
import sys
from itertools import accumulate

import numpy as np

h, w = list(map(int, input().split()))
field = [[int(c == '.') for c in line.rstrip()] for line in sys.stdin]
func = lambda x, a: (x + a) * a
ans = np.zeros([h, w], dtype=np.int)

for i, row in enumerate(field):
    ans[i] += list(accumulate(row, func=func))
    ans[i] += list(reversed(list(accumulate(reversed(row), func=func))))

ans = ans.T
field = np.array(field).T.tolist()
for i, row in enumerate(field):
    ans[i] += list(accumulate(row, func=func))
    ans[i] += list(reversed(list(accumulate(reversed(row), func=func))))

print(ans.max() - 3)

Submission status: Time Limit Exceeded

Solution code:
def main():
    h,*s=open(0)
    h,w=map(int,h.split())
    a=tuple([0]*w for _ in range(h))
    b=tuple([0]*w for _ in range(h))
    c=tuple([0]*w for _ in range(h))
    d=tuple([0]*w for _ in range(h))
    for i in range(h*w):
        i,j=divmod(i,w)
        a[i][j]=a[i][j-1]+1if s[i][j]=='.'else 0
        b[i][~j]=b[i][-j]+1if s[i][-j-2]=='.'else 0
        c[i][j]=c[i-1][j]+1if s[i][j]=='.'else 0
        d[~i][j]=d[-i][j]+1if s[~i][j]=='.'else 0
    print(max(w+x+y+z-3for t in zip(a,b,c,d)for w,x,y,z in zip(*t)))
main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    h,*s=open(0)
    h,w=map(int,h.split())
    a=tuple([0]*w for _ in range(h))
    b=tuple([0]*w for _ in range(h))
    c=tuple([0]*w for _ in range(h))
    d=tuple([0]*w for _ in range(h))
    for i in range(h*w):
        i,j=divmod(i,w)
        a[i][j]=a[i][j-1]+1if s[i][j]=='.'else 0
        b[i][~j]=b[i][-j]+1if s[i][-j-2]=='.'else 0
        c[i][j]=c[i-1][j]+1if s[i][j]=='.'else 0
        d[~i][j]=d[-i][j]+1if s[~i][j]=='.'else 0
    print(max(sum(u)-3for t in zip(a,b,c,d)for u in zip(*t)))
main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    h,*s=open(0)
    h,w=map(int,h.split())
    a=tuple([0]*w for _ in range(h))
    b=tuple([0]*w for _ in range(h))
    c=tuple([0]*w for _ in range(h))
    d=tuple([0]*w for _ in range(h))
    for i in range(h):
        for j in range(w):
            a[i][j]=a[i][j-1]+1if s[i][j]=='.'else 0
            b[i][~j]=b[i][-j]+1if s[i][-j-2]=='.'else 0
            c[i][j]=c[i-1][j]+1if s[i][j]=='.'else 0
            d[~i][j]=d[-i][j]+1if s[~i][j]=='.'else 0
    print(max(sum(u)-3for t in zip(a,b,c,d)for u in zip(*t)))
main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    h,*s=open(0)
    h,w=map(int,h.split())
    a=tuple([0]*w for _ in range(h))
    b=tuple([0]*w for _ in range(h))
    c=tuple([0]*w for _ in range(h))
    d=tuple([0]*w for _ in range(h))
    for i in range(h):
        for j in range(w):
            a[i][j]=a[i][j-1]+1if s[i][j]=='.'else 0
            b[i][~j]=b[i][-j]+1if s[i][-j-2]=='.'else 0
    for j in range(w):
        for i in range(h):
            c[i][j]=c[i-1][j]+1if s[i][j]=='.'else 0
            d[~i][j]=d[-i][j]+1if s[~i][j]=='.'else 0
    print(max(sum(u)-3for t in zip(a,b,c,d)for u in zip(*t)))
main()

Submission status: Time Limit Exceeded

Solution code:
import sys
from itertools import accumulate

import numpy as np

h, w = list(map(int, input().split()))
field = [['#.'.index(c) for c in line.rstrip()] for line in sys.stdin]
field = np.array(field)
cumsum = lambda iterable: np.array([list(accumulate(row, func=lambda x, a: (x + a) * a)) for row in iterable])
l = cumsum(field)
l += cumsum(field.T).T
l += np.fliplr(cumsum(np.fliplr(field)))
l += np.flipud(cumsum(np.flipud(field).T).T)

print(l.max() - 3)

Submission status: Time Limit Exceeded

Solution code:
def main():
    h,*s=open(0)
    h,w=map(int,h.split())
    a=tuple([0]*w for _ in range(h))
    b=tuple([0]*w for _ in range(h))
    c=tuple([0]*h for _ in range(w))
    d=tuple([0]*h for _ in range(w))
    for t,x,y in zip(s,a,b):
        for i,u in enumerate(t[:-1]):
            x[i]=x[i-1]+1if u=='.'else 0
        for i,u in enumerate(t[-2::-1]):
            y[~i]=y[-i]+1if u=='.'else 0
    for t,x,y in zip(zip(*s),c,d):
        for i,u in enumerate(t):
            x[i]=x[i-1]+1if u=='.'else 0
        for i,u in enumerate(t[::-1]):
            y[~i]=y[-i]+1if u=='.'else 0
    print(max(sum(u)-3for t in zip(a,b,zip(*c),zip(*d))for u in zip(*t)))
main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    h,*s=open(0)
    h,w=map(int,h.split())
    a=tuple([0]*w for _ in range(h))
    b=tuple([0]*w for _ in range(h))
    c=tuple([0]*h for _ in range(w))
    d=tuple([0]*h for _ in range(w))
    for t,x,y in zip(s,a,b):
        x[0]=int(t[0]=='.')
        for i,u in enumerate(t[1:-1]):
            x[i+1]=x[i]+1if u=='.'else 0
        y[-1]=int(t[-2]=='.')
        for i,u in enumerate(t[-3::-1],1):
            y[~i]=y[-i]+1if u=='.'else 0
    for t,x,y in zip(zip(*s),c,d):
        x[0]=int(t[0]=='.')
        for i,u in enumerate(t[1:]):
            x[i+1]=x[i]+1if u=='.'else 0
        y[-1]=int(t[-1]=='.')
        for i,u in enumerate(t[-2::-1],1):
            y[~i]=y[-i]+1if u=='.'else 0
    print(max(sum(u)-3for t in zip(a,b,zip(*c),zip(*d))for u in zip(*t)))
main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    h,*s=open(0)
    h,w=map(int,h.split())
    a=tuple([0]*w for _ in range(h))
    b=tuple([0]*w for _ in range(h))
    c=tuple([0]*w for _ in range(h))
    d=tuple([0]*w for _ in range(h))
    for i in range(h):
        a[i][0]=1if s[i][0]=='.'else 0
        b[i][-1]=1if s[i][-2]=='.'else 0
        for j in range(1,w):
            a[i][j]=a[i][j-1]+1if s[i][j]=='.'else 0
            b[i][~j]=b[i][-j]+1if s[i][-j-2]=='.'else 0
    for j in range(w):
        c[0][j]=1if s[0][j]=='.'else 0
        d[-1][j]=1if s[-1][j]=='.'else 0
        for i in range(1,h):
            c[i][j]=c[i-1][j]+1if s[i][j]=='.'else 0
            d[~i][j]=d[-i][j]+1if s[~i][j]=='.'else 0
    print(max(sum(u)-3for t in zip(a,b,c,d)for u in zip(*t)))
main()

Submission status: Time Limit Exceeded

Solution code:
h, w = map(int, input().split())
a = []
for y in range(h):
    s = input()
    line = []
    for x in range(len(s)):
        if s[x] == '#':
            line.append([-1, -1, 1])
        else:
            line.append([-1, -1, 0])
    a.append(line)
 
# Xスコア計算
for y in range(h):
    start = -1
    end = -1
    count = 0
    for x in range(w):
        if a[y][x][2] == 1 or x == w - 1:
            end = x
            if x == w - 1 and a[y][x][2] != 1:
                end  = end + 1
                count = count + 1
            for u in range(start, end):
                a[y][u][0] = count
            start = -1
            count = 0
        else:
            count = count + 1
        if start == -1:
            start = x
 
ans = 0
 
# yスコア計算
for x in range(w):
    start = -1
    end = -1
    count = 0
    for y in range(h):
        if a[y][x][2] == 1 or y == h - 1:
            end = y
            if y == h - 1 and a[y][x][2] != 1:
                end  = end + 1
                count = count + 1
            for u in range(start, end):
                a[u][x][1] = count
                # スコア計算
                if a[u][x][2] != 1:
                    score = a[u][x][0] + a[u][x][1]
                    ans = max(score, ans)
            start = -1
            count = 0
        else:
            count = count + 1
        if start == -1:
            start = y
print(ans - 1)

Submission status: Time Limit Exceeded

Solution code:
def main():
    h,*s=open(0)
    h,w=map(int,h.split())
    a=tuple([0]*w for _ in range(h))
    b=tuple([0]*w for _ in range(h))
    c=tuple([0]*w for _ in range(h))
    d=tuple([0]*w for _ in range(h))
    for i in range(h):
        a[i][0]=int(s[i][0]=='.')
        b[i][-1]=int(s[i][-2]=='.')
        for j in range(1,w):
            a[i][j]=a[i][j-1]+1if s[i][j]=='.'else 0
            b[i][~j]=b[i][-j]+1if s[i][-j-2]=='.'else 0
    for j in range(w):
        c[0][j]=int(s[0][j]=='.')
        d[-1][j]=int(s[-1][j]=='.')
        for i in range(1,h):
            c[i][j]=c[i-1][j]+1if s[i][j]=='.'else 0
            d[~i][j]=d[-i][j]+1if s[~i][j]=='.'else 0
    print(max(sum(u)-3for t in zip(a,b,c,d)for u in zip(*t)))
main()

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10**7)
INTMAX = 9223372036854775807
INTMIN = -9223372036854775808
MOD = 1000000007
def POW(x, y): return pow(x, y, MOD)
def INV(x, m=MOD): return pow(x, m - 2, m)
def DIV(x, y, m=MOD): return (x * INV(y, m)) % m
def LI(): return [int(x) for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LS(): return input().split()
def II(): return int(input())
 
H,W=LI()
MP=[input() for _ in range(H)]
 
mp_wid=[[0]*W for _ in range(H)]
mp_hei=[[0]*W for _ in range(H)]
 
for i in range(H):
    cnt = 0
    for j in range(W):
        if MP[i][j] == '.':
            cnt += 1
        else:
            for k in range(cnt):
                mp_wid[i][j-k-1] = cnt
            cnt=0
    for k in range(cnt):
        mp_wid[i][-k-1] = cnt

for j in range(W):
    cnt = 0
    for i in range(H):
        if MP[i][j] == '.':
            cnt+=1
        else:
            for k in range(cnt):
                mp_hei[i-k-1][j] = cnt
            cnt=0
    for k in range(cnt):
        mp_hei[-k-1][j] = cnt

res = 0
for i in range(H):
    for j in range(W):
        res = max(res, mp_hei[i][j]+mp_wid[i][j]-1)
print(res)

Submission status: Time Limit Exceeded

Solution code:
h, w = map(int, input().split())
s = []
s_conn = []
for i in range(h):
  s.append(input())
  s_conn.append([[0]*2 for j in range(w)])
 
# 縦にみる
for x in range(w):
  start = 0
  count = 0
  for y in range(h):
    if s[y][x] == '.':
      count += 1
      if y == h-1:
        for i in range(start, y+1):
          s_conn[i][x][0] = count
    else:
      for i in range(start, y):
        s_conn[i][x][0] = count
      count = 0
      start = y+1
 
# 横にみる
for y in range(h):
  start = 0
  count = 0
  for x in range(w):
    if s[y][x] == '.':
      count += 1
      if x == w-1:
        for i in range(start, x+1):
          s_conn[y][i][1] = count
    else:
      for i in range(start, x):
        s_conn[y][i][1] = count
      count = 0
      start = x+1
 
max_all = 0
for i in range(h):
  max_tmp = max(map(sum, s_conn[i]))
  if max_tmp > max_all:
    max_all = max_tmp
print(max_all - 1)

Submission status: Time Limit Exceeded

Solution code:
def main():
    h,*s=open(0)
    h,w=map(int,h.split())
    a=tuple([0]*w for _ in range(h))
    b=tuple([0]*w for _ in range(h))
    c=tuple([0]*w for _ in range(h))
    d=tuple([0]*w for _ in range(h))
    for i in range(h):
        a[i][0]=int(s[i][0]=='.')
        for j in range(w-1):
            a[i][j+1]=a[i][j]+1if s[i][j+1]=='.'else 0
        b[i][-1]=int(s[i][-2]=='.')
        for j in range(1,w):
            b[i][~j]=b[i][-j]+1if s[i][-j-2]=='.'else 0
    for j in range(w):
        c[0][j]=int(s[0][j]=='.')
        for i in range(h-1):
            c[i+1][j]=c[i][j]+1if s[i+1][j]=='.'else 0
        d[-1][j]=int(s[-1][j]=='.')
        for i in range(1,h-1):
            d[~i][j]=d[-i][j]+1if s[~i][j]=='.'else 0
    print(max(sum(u)-3for t in zip(a,b,c,d)for u in zip(*t)))
main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    h,*s=open(0)
    s=[tuple(t[:-1])for t in s]
    h,w=map(int,h.split())
    a=tuple([0]*w for _ in range(h))
    b=tuple([0]*w for _ in range(h))
    c=tuple([0]*w for _ in range(h))
    d=tuple([0]*w for _ in range(h))
    for i,t in enumerate(s):
        a[i][0]=int(t[0]=='.')
        for j,u in enumerate(t[1:]):
            a[i][j+1]=a[i][j]+1if u=='.'else 0
        b[i][-1]=int(t[-1]=='.')
        for j,u in enumerate(t[-2::-1],1):
            b[i][~j]=b[i][-j]+1if u=='.'else 0
    for j in range(w):
        c[0][j]=int(s[0][j]=='.')
        for i in range(h-1):
            c[i+1][j]=c[i][j]+1if s[i+1][j]=='.'else 0
        d[-1][j]=int(s[-1][j]=='.')
        for i in range(1,h-1):
            d[~i][j]=d[-i][j]+1if s[~i][j]=='.'else 0
    print(max(sum(u)-3for t in zip(a,b,c,d)for u in zip(*t)))
main()

Submission status: Time Limit Exceeded

Solution code:
def lmi(): return list(map(int, input().split()))

h, w = lmi()
s = [input() for i in range(h)]

cnt = [[0 for i in range(w)] for j in range(h)]
for i in range(h):
    j = 0
    while j < w:
        if s[i][j] == '#':
            j += 1
            continue
        l = 0
        while j+l < w:
            if s[i][j+l] == '#':
                break
            l += 1
        for k in range(l):
            cnt[i][j+k] += l
        j += l+1

for i in range(w):
    j = 0
    while j < h:
        if s[j][i] == '#':
            j += 1
            continue
        l = 0
        while j+l < h:
            if s[j+l][i] == '#':
                break
            l += 1
        for k in range(l):
            cnt[j+k][i] += l
        j += l+1


print(max([max([cnt[i][j] for i in range(h)]) for j in range(w)])-1)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
import numpy as np

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

L = np.zeros([H, W], dtype=int)
R = np.zeros([H, W], dtype=int)
U = np.zeros([H, W], dtype=int)
D = np.zeros([H, W], dtype=int)
for h in range(H):
    for w in range(W):
        if S[h][w] != '#':
            if w == 0:
                L[h, w] = 1
            else: L[h, w] = L[h][w-1] + 1
            if h == 0:
                U[h, w] = 1
            else: U[h, w] = U[h-1][w] + 1
        else:
            L[h, w] = 0
            U[h, w] = 0

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
            else: R[h, w] = R[h][w+1] + 1
            if h == H-1:
                D[h, w] = 1
            else: D[h, w] = D[h+1][w] + 1
        else:
            R[h, w] = 0
            D[h, w] = 0

L = np.array(L)
R = np.array(R)
U = np.array(U)
D = np.array(D)
print(np.max(L+R+U+D)-3)

Submission status: Time Limit Exceeded

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

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

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

for h in range(H):
    for w in range(W):
        if S[h][w] == 1:
            continue
        # up
        if h-1 < 0 or S[h-1][w] == '#':
            U[h][w] = 0
        else:
            U[h][w] = U[h-1][w] + 1
        # left
        if w-1 < 0 or S[h][w-1] == '#':
            L[h][w] = 0
        else:
            L[h][w] = L[h][w-1] + 1

for h in range(H-1, -1, -1):
    for w in range(W-1, -1, -1):
        if S[h][w] == 1:
            continue
        # down
        if h+1 >= H or S[h+1][w] == '#':
            D[h][w] = 0
        else:
            D[h][w] = D[h+1][w] + 1
        # right
        if w+1 >= W or S[h][w+1] == '#':
            R[h][w] = 0
        else:
            R[h][w] = R[h][w+1] + 1

ans = 0
for h in range(H):
    for w in range(W):
        if S[h][w] == '#':
            continue
        cnt = U[h][w] + L[h][w] + D[h][w] + R[h][w] + 1
        if cnt > ans:
            ans = cnt

print(ans)

Submission status: Time Limit Exceeded

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

left = [[-1 for j in range(W)] for i in range(H)]
right = [[W for j in range(W)] for i in range(H)]
up = [[-1 for j in range(W)] for i in range(H)]
down = [[H for j in range(W)] for i in range(H)]

# from left upper to right down
up_wall = [-1 for i in range(W)]
for h in range(H):
    left_wall = -1    
    for w in range(W):
        if S[h][w] == '#':
            left_wall = w
            up_wall[w] = h
        left[h][w] = left_wall
        up[h][w] = up_wall[w]

# from right down to left up
down_wall = [H for i in range(W)]
for h in range(H - 1, -1, -1):
    right_wall = W
    for w in range(W - 1, -1, -1):
        if S[h][w] == '#':
            right_wall = w
            down_wall[w] = h
        right[h][w] = right_wall
        down[h][w] = down_wall[w]

# print(left)
# print(right)
# print(up)
# print(down)

max_score = 0
for h in range(H):
    for w in range(W):
        left_score = (w - left[h][w])
        right_score = (right[h][w] - w)
        up_score = (h - up[h][w])
        down_score = (down[h][w] - h)
        score = left_score + right_score + up_score + down_score - 3
        # print(h, w, score, left_score, right_score, up_score, down_score)
        if max_score < score:
            max_score = score
print(max_score)