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:
#!/usr/bin/env python3
import sys
import numpy as np

def solve(H: int, W: int, S: "List[str]"):
    S = np.array(S)
    sh = np.zeros((H, W),dtype=np.int)
    sw = np.zeros((W, H),dtype=np.int)

    for i, a in enumerate(S):
        left = np.arange(1, len(a)+1)
        left[a==0] = 0
        left = np.maximum.accumulate(left)
        a_ = a[::-1]
        right = np.arange(1,len(a)+1)
        right[a_==0] = 0
        right = np.maximum.accumulate(right)
        right = len(a)+1 - right[::-1]
        k = ((right-left) - 1)
        k[a==1] =0
        sh[i] = k

    for i, a in enumerate(S.T):
        left = np.arange(1, len(a)+1)
        left[a==0] = 0
        left = np.maximum.accumulate(left)
        a_ = a[::-1]
        right = np.arange(1,len(a)+1)
        right[a_==0] = 0
        right = np.maximum.accumulate(right)
        right = len(a)+1 - right[::-1]
        k = ((right-left) - 1)
        k[a==1] =0
        sw[i] = k

    print(np.max(sw+sh.T)-1)



    # for w in range(W):
        # for h in range(H):

    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    H = int(next(tokens))  # type: int
    W = int(next(tokens))  # type: int
    S = [[ int(_.replace(".", "0").replace("#", "1")) for _ in next(tokens)] for _ in range(H)]  # type: "List[str]"
    solve(H, W, S)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
import numpy as np

def read():
    return sys.stdin.readline().rstrip()

def main():
    h, w = map(int, read().split())
    s = [list(read()) for _ 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 i in range(h):
        for j in range(w):
            if s[i][j] == ".":
                l[i][j] = l[i][j - 1] + 1 if j > 0 else 1
                u[i][j] = u[i - 1][j] + 1 if i > 0 else 1
            if s[h - 1 - i][w - 1 - j] == ".":
                r[h - 1 - i][w - 1 - j] = r[h - 1 - i][w - j] + 1 if j > 0 else 1
                d[h - 1 - i][w - 1 - j] = d[h - i][w - 1 - j] + 1 if i > 0 else 1
    print((l + r + u + d).max() - 3)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys

def read():
    return sys.stdin.readline().rstrip()

def main():
    h, w = map(int, read().split())
    s = [list(read()) for _ in range(h)]
    l = [[0] * w for _ in range(h)]
    r = [[0] * w for _ in range(h)]
    u = [[0] * w for _ in range(h)]
    d = [[0] * w for _ in range(h)]
    for i in range(h):
        for j in range(w):
            if s[i][j] == ".":
                l[i][j] = l[i][j - 1] + 1 if j > 0 else 1
                u[i][j] = u[i - 1][j] + 1 if i > 0 else 1
            if s[h - 1 - i][w - 1 - j] == ".":
                r[h - 1 - i][w - 1 - j] = r[h - 1 - i][w - j] + 1 if j > 0 else 1
                d[h - 1 - i][w - 1 - j] = d[h - i][w - 1 - j] + 1 if i > 0 else 1
    ans = 0
    for i in range(h):
        for j in range(w):
            ans = max(l[i][j] + r[i][j] + u[i][j] + d[i][j] - 3, ans)
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys

def read():
    return sys.stdin.readline().rstrip()

def main():
    h, w = map(int, read().split())
    s = [list(read()) for _ in range(h)]
    row = [[0 for _ in range(w)] for _ in range(h)]
    col = [[0 for _ in range(w)] for _ in range(h)]
    for i in range(h):
        c = 0
        for j in range(w):
            if s[i][j] == "#":
                for k in range(j - c, j):
                    row[i][k] = c
                c = 0
            else:
                c += 1
        for k in range(w - c, w):
            row[i][k] = c
    for i in range(w):
        c = 0
        for j in range(h):
            if s[j][i] == "#":
                for k in range(j - c, j):
                    col[k][i] = c
                c = 0
            else:
                c += 1
        for k in range(h - c, h):
            col[k][i] = c
    ans = 0
    for i in range(h):
        for j in range(w):
            ans = max(row[i][j] + col[i][j], ans)
    print(ans - 1)

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 j in range(w)] for i in range(h)]
R = [[0 for j in range(w)] for i in range(h)]
U = [[0 for j in range(w)] for i in range(h)]
D = [[0 for j in range(w)] for i in range(h)]

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
h, w = map(int, input().split())
s = []
for _ in range(h):
    s.append(input())
hs = [[0]*w for _ in range(h)]
ws = [[0]*w for _ in range(h)]
for y in range(h):
    for x in range(w):
        if s[y][x] == ".":
            if 1<=x<w and ws[y][x-1]:
                ws[y][x] = ws[y][x-1]
                continue
            if x<w and s[y][x]==".":
                for x_ in range(x, w):
                    if s[y][x_] == ".": 
                        ws[y][x] += 1
                    else:
                        break
for x in range(w):
    for y in range(h):
        if s[y][x] == ".":
            if 1<=y<h and hs[y-1][x]:
                hs[y][x] = hs[y-1][x]
                continue
            if y<h and s[y][x]==".":
                for y_ in range(y, h):
                    if s[y_][x] == ".": 
                        hs[y][x] += 1
                    else:
                        break
ans = hs[0][0] + ws[0][0] -1 
for i in range(h):
    for j in range(w):
        ans = max(ans, hs[i][j] + ws[i][j] -1)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10**5)
h,w =  map(int,input().split())
ls = [input() for i in range(h)]
cy = [[-1]*w for j in range(h)]
cx = [[-1]*w for j in range(h)]

d = (-1,1)
def cony(y,x,yp,l):
    if y<0 or y>=h:return l-1
    if ls[y][x] == "#":return l-1
    if cy[y][x] !=-1: return cy[y][x]

    for dd in d:
        if  y+dd!=yp :
            cy[y][x] = cony(y+dd,x,y,l+1)
    return cy[y][x]
def conx(y,x,xp,l):
    if x<0 or x>=w:return l-1
    if ls[y][x] == "#":return l-1
    if cx[y][x] !=-1:return cx[y][x]
    
    for dd in d:
        if x+dd!=xp:
            cx[y][x] = conx(y,x+dd,x,l+1)
    return cx[y][x]
ans = 0
for i in range(h):
    for j in range(w):
        if ls[i][j] ==".":
            ans = max(ans,cony(i,j,i,1) + conx(i,j,j,1) -1)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
IS = lambda: sys.stdin.readline().rstrip()
II = lambda: int(IS())
MII = lambda: list(map(int, IS().split()))

def main():
    h, w = MII()
    ss = []
    for i in range(h):
        ss.append(IS())
    

    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):
        for j in range(w):
            if ss[i][j] == '#': L[i][j] = 0; continue
            if j == 0: L[i][j] = 1; continue
            if j > 0: L[i][j] = L[i][j-1] + 1; continue

    for i in range(h):
        for j in reversed(range(w)):
            if ss[i][j] == '#': R[i][j] = 0; continue
            if j == w-1: R[i][j] = 1; continue
            if j < w-1: R[i][j] = R[i][j+1] + 1; continue

    for i in range(h):
        for j in range(w):
            if ss[i][j] == '#': D[i][j] = 0; continue
            if i == 0: D[i][j] = 1; continue
            if i > 0: D[i][j] = D[i-1][j] + 1; continue

    for i in reversed(range(h)):
        for j in range(w):
            if ss[i][j] == '#': U[i][j] = 0; continue
            if i == h-1: U[i][j] = 1; continue
            if i < h-1: U[i][j] = U[i+1][j] + 1; continue

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

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
H, W  = map(int, input().split())
grid = [input() for _ in range(H)]
ans = [[0] * W for _ in range(H)]
for i in range(H):
    tmp = 0
    for k in range(W):
        if grid[i][k] == '.':
            tmp += 1
        else:
            ans[i][k] = 0
            for t in range(k-tmp,k):
                ans[i][t] = tmp
            tmp = 0
    for t in range(W-tmp,W):
        ans[i][t] = tmp
a = 0
for i in range(W):
    tmp = 0
    for k in range(H):
        if grid[k][i] == '.':
            tmp += 1
        else:
            for t in range(k-tmp,k):
                a = max(ans[t][i]+tmp-1,a)
            tmp = 0
    for t in range(H-tmp,H):
        a = max(ans[t][i]+tmp-1,a)
print(a)

Submission status: Time Limit Exceeded

Solution code:
H, W = map(int, input().split())
board = [[False]*W for _ in range(H)]
L = [[0]*W for _ in range(H)]
R = [[0]*W for _ in range(H)]
U = [[0]*W for _ in range(H)]
D = [[0]*W for _ in range(H)]

for i in range(H):
    s = input()
    for j, c in enumerate(s):
        if c == '.':
            board[i][j] = True

for i in range(H):
    for j in range(W):
        if board[i][j]:
            if j==0:
                L[i][j] = 1
            else:
                L[i][j] = L[i][j-1] + 1
    for j in range(W-1, -1, -1):
        if board[i][j]:
            if j == W-1:
                R[i][j] = 1
            else:
                R[i][j] = R[i][j+1] + 1
        else:
            R[i][j] = 0

for j in range(W):
    for i in range(H):
        if board[i][j]:
            if i == 0:
                U[i][j] = 1
            else:
                U[i][j] = U[i-1][j] + 1
        else:
            U[i][j] = 0
    for i in range(H-1, -1, -1):
        if board[i][j]:
            if i == H-1:
                D[i][j] = 1
            else:
                D[i][j] = D[i+1][j] + 1
        else:
            D[i][j] = 0

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)
        
print(ans)

Submission status: Time Limit Exceeded

Solution code:
H,W=map(int,input().split())
Sall=''
migi=[]
hidari=[]
for i in range(H):
  S=input()
  Sall+=S
  _migi=[]
  _hidari=[]
  x,y=0,0
  for j in range(W):
    k=W-1-j
    x=x+1 if S[j]=='.' else 0
    y=y+1 if S[k]=='.' else 0
    _migi.append(x)
    _hidari.append(y)
  migi.append(_migi)
  hidari.append(_hidari)
ue=[]
sita=[]
for i in range(W):
  S=Sall[i::W]
  _sita=[]
  _ue=[]
  x,y=0,0
  for j in range(H):
    k=H-1-j
    x=x+1 if S[j]=='.' else 0
    y=y+1 if S[k]=='.' else 0
    _sita.append(x)
    _ue.append(y)
  sita.append(_sita)
  ue.append(_ue)

ans=0
for i in range(H):
  for j in range(W):
    a=migi[i][j]+hidari[i][W-1-j]+sita[j][i]+ue[j][H-1-i]-3
    ans=ans if ans>a else a
print(ans)

Submission status: Time Limit Exceeded

Solution code:
H,W=map(int,input().split())
Sall=''
yoko=[]
for i in range(H):
  S=input()
  Sall+=S
  _=[0]*W
  x,y=0,0
  for j in range(W):
    k=W-1-j
    x=x+1 if S[j]=='.' else 0
    y=y+1 if S[k]=='.' else 0
    _[j]+=x
    _[k]+=y
  yoko.append(_)
tate=[]
for i in range(W):
  S=Sall[i::W]
  _=[0]*H
  x,y=0,0
  for j in range(H):
    k=H-1-j
    x=x+1 if S[j]=='.' else 0
    y=y+1 if S[k]=='.' else 0
    _[j]+=x
    _[k]+=y
  tate.append(_)

ans=0
for i in range(H):
  for j in range(W):
    ans=max(ans,yoko[i][j]+tate[j][i]-1-2)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
H,W=map(int,input().split())
import itertools
Sall=''
yoko=[]
for i in range(H):
  S=input()
  Sall+=S
  gr = itertools.groupby(S)
  _=[0]*W
  j=0
  for key,group in gr:
    g=list(group)
    a=0 if key=='#' else len(g)
    for k in g:
      _[j]=a
      j+=1
  yoko.append(_)
tate=[]
for i in range(W):
  s=Sall[i::W]
  gr = itertools.groupby(s)
  _=[0]*H
  j=0
  for key,group in gr:
    g=list(group)
    a=0 if key=='#' else len(g)
    for k in g:
      _[j]=a
      j+=1
  tate.append(_)

ans=0
for i in range(H):
  for j in range(W):
    ans=max(ans,yoko[i][j]+tate[j][i]-1)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
H,W=map(int,input().split())
import itertools
Sall=''
yoko=[]
for i in range(H):
  S=input()
  Sall+=S
  gr = itertools.groupby(S)
  _=[]
  for key,group in gr:
    g=list(group)
    a=0 if key=='#' else len(g)
    _+=[a for j in g]
  yoko.append(_)
tate=[]
for i in range(W):
  s=Sall[i::W]
  gr = itertools.groupby(s)
  _=[]
  for key,group in gr:
    g=list(group)
    a=0 if key=='#' else len(g)
    _+=[a for j in g]
  tate.append(_)

ans=0
for i in range(H):
  for j in range(W):
    ans=max(ans,yoko[i][j]+tate[j][i]-1)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8

import sys
import math
import collections
import itertools
from functools import lru_cache
INF = 10 ** 13
MOD = 10 ** 9 + 7
def input() : return sys.stdin.readline().strip()
def gcd(x, y) : return y if x % y == 0 else gcd(y, x % y)
def lcm(x, y) : return (x * y) // gcd(x, y)
def I() : return int(input())
def LI() : return [int(x) for x in input().split()]
def RI(N) : return [int(input()) for _ in range(N)]
def LRI(N) : return [[int(x) for x in input().split()] for _ in range(N)]
def LS() : return input().split()
def RS(N) : return [input() for _ in range(N)]
def LRS(N) : return [input().split() for _ in range(N)]
def PL(L) : print(*L, sep="\n")

H, W = LI()
S = RS(H)

@lru_cache(10**5)
def dfs(x, y):
    stack = [[x, y-1, 1], [x+1, y, 2], [x, y+1, 3], [x-1, y, 4]]
    ret = 1

    while stack:
        x, y, dist = stack.pop()

        if not(0 <= x < W) or not(0 <= y < H) or S[y][x] == "#":
            continue

        ret += 1

        if dist == 1:
            stack.append([x, y-1, 1])
        elif dist == 2:
            stack.append([x+1, y, 2])
        elif dist == 3:
            stack.append([x, y+1, 3])
        else:
            stack.append([x-1, y, 4])
    
    return ret


ans = 0
for x in range(W):
    for y in range(H):
        if S[y][x] == ".":
            ans = max(ans, dfs(x, y))

print(ans)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8

import sys
import math
import collections
import itertools
from functools import lru_cache
INF = 10 ** 13
MOD = 10 ** 9 + 7
def input() : return sys.stdin.readline().strip()
def gcd(x, y) : return y if x % y == 0 else gcd(y, x % y)
def lcm(x, y) : return (x * y) // gcd(x, y)
def I() : return int(input())
def LI() : return [int(x) for x in input().split()]
def RI(N) : return [int(input()) for _ in range(N)]
def LRI(N) : return [[int(x) for x in input().split()] for _ in range(N)]
def LS() : return input().split()
def RS(N) : return [input() for _ in range(N)]
def LRS(N) : return [input().split() for _ in range(N)]
def PL(L) : print(*L, sep="\n")

H, W = LI()
S = RS(H)

@lru_cache()
def dfs(x, y):
    stack = [[x, y-1, 1], [x+1, y, 2], [x, y+1, 3], [x-1, y, 4]]
    ret = 1

    while stack:
        x, y, dist = stack.pop()

        if not(0 <= x < W) or not(0 <= y < H) or S[y][x] == "#":
            continue

        ret += 1

        if dist == 1:
            stack.append([x, y-1, 1])
        elif dist == 2:
            stack.append([x+1, y, 2])
        elif dist == 3:
            stack.append([x, y+1, 3])
        else:
            stack.append([x-1, y, 4])
    
    return ret


ans = 0
for x in range(W):
    for y in range(H):
        if S[y][x] == ".":
            ans = max(ans, dfs(x, y))

print(ans)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8

import sys
import math
import collections
import itertools
INF = 10 ** 13
MOD = 10 ** 9 + 7
def input() : return sys.stdin.readline().strip()
def gcd(x, y) : return y if x % y == 0 else gcd(y, x % y)
def lcm(x, y) : return (x * y) // gcd(x, y)
def I() : return int(input())
def LI() : return [int(x) for x in input().split()]
def RI(N) : return [int(input()) for _ in range(N)]
def LRI(N) : return [[int(x) for x in input().split()] for _ in range(N)]
def LS() : return input().split()
def RS(N) : return [input() for _ in range(N)]
def LRS(N) : return [input().split() for _ in range(N)]
def PL(L) : print(*L, sep="\n")

H, W = LI()
S = RS(H)

def dfs(x, y):
    stack = [[x, y-1, 1], [x+1, y, 2], [x, y+1, 3], [x-1, y, 4]]
    ret = 1

    while stack:
        x, y, dist = stack.pop()

        if not(0 <= x < W) or not(0 <= y < H) or S[y][x] == "#":
            continue

        ret += 1

        if dist == 1:
            stack.append([x, y-1, 1])
        elif dist == 2:
            stack.append([x+1, y, 2])
        elif dist == 3:
            stack.append([x, y+1, 3])
        else:
            stack.append([x-1, y, 4])
    
    return ret


ans = 0
for x in range(W):
    for y in range(H):
        if S[y][x] == ".":
            ans = max(ans, dfs(x, y))

print(ans)

Submission status: Time Limit Exceeded

Solution code:
H, W = map(int, input().split())
Map = []
for _ in range(H):
  List = list(input())
  Map.append(List)
L = [[0]*W for _ in range(H)]
D = [[0]*W for _ in range(H)]
R = [[0]*W for _ in range(H)]
U = [[0]*W for _ in range(H)]
for i in range(H):
  for j in range(W):
    if Map[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):
    if Map[i][j] == '#':
      D[i][j] = 0
    elif i == 0:
      D[i][j] = 1
    else:
      D[i][j] = D[i-1][j] + 1
for i in reversed(range(H)):
  for j in reversed(range(W)):
    if Map[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 reversed(range(H)):
  for j in reversed(range(W)):
    if Map[i][j] == '#':
      U[i][j] = 0
    elif i == H - 1:
      U[i][j] = 1
    else:
      U[i][j] = U[i+1][j] + 1

Answer = 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 > Answer:
      Answer = tmp
print(Answer)

Submission status: Time Limit Exceeded

Solution code:
H, W = map(int, input().split())
Map = []
for _ in range(H):
  List = list(input())
  Map.append(List)
points = [[0]*W for i in range(H)]
directions = [(1,0),(-1,0),(0,1),(0,-1)]
def countpoints(Map,i,j):
  ans = 1
  for direction in directions:
    p= i
    q = j
    while (0 <= p <= H-1) and (0 <= q <= W -1):
      p,q = p+direction[0],q+direction[1]
      if (p < 0 or H -1 < p) or (q < 0 or W-1 <q):
        break
      if Map[p][q] != '#':
        ans += 1
      else:
        break
  return ans
for i in range(H):
  for j in range(W):
    if Map[i][j] != '#':
      points[i][j] = countpoints(Map,i,j)
import numpy as np
results = np.array(points)
idx = np.unravel_index(np.argmax(results), results.shape)
print(results[idx])

Submission status: Time Limit Exceeded

Solution code:
H, W = map(int, input().split())
Map = []
for _ in range(H):
  List = list(input())
  Map.append(List)
points = [[0]*W for i in range(H)]
directions = [(1,0),(-1,0),(0,1),(0,-1)]
def countpoints(Map,i,j):
  ans = 1
  for direction in directions:
    p= i
    q = j
    while (0 <= p <= H-1) and (0 <= q <= W -1):
      p,q = p+direction[0],q+direction[1]
      if (p < 0 or H -1 < p) or (q < 0 or W-1 <q):
        break
      if Map[p][q] != '#':
        ans += 1
      else:
        break
  return ans
for i in range(H):
  for j in range(W):
    if Map[i][j] != '#':
      points[i][j] = countpoints(Map,i,j)
maxdata = []
for line in points:
  maxdata.append(max(line))
Answer = max(maxdata)
print(Answer)

Submission status: Time Limit Exceeded

Solution code:
# python 3.4.3
import sys
input = sys.stdin.readline

# -------------------------------------------------------------
# function
# -------------------------------------------------------------


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

LR = [[0 for _ in range(W)] for _ in range(H)]
UD = [[0 for _ in range(W)] for _ in range(H)]

# 横方向
for h in range(H):
    cnt = 0
    for w in range(W):
        if S[h][w] == "#":
            cnt = 0
        else:
            cnt += 1
        LR[h][w] = cnt
for h in range(H):
    cnt = 0
    for w in range(W):
        if LR[h][W-1-w] == 0:
            cnt = 0
        else:
            if cnt==0:
                cnt = LR[h][W-1-w]
            else:
                LR[h][W-1-w] = cnt

# 縦方向
for w in range(W):
    cnt = 0
    for h in range(H):
        if S[h][w] == "#":
            cnt = 0
        else:
            cnt += 1
        UD[h][w] = cnt
for w in range(W):
    cnt = 0
    for h in range(H):
        if UD[H-1-h][w] == 0:
            cnt = 0
        else:
            if cnt==0:
                cnt = UD[H-1-h][w]
            else:
                UD[H-1-h][w] = cnt

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

print(ans)

Submission status: Time Limit Exceeded

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

l = [[0] * w for i in range(h)]
r = [[0] * w for i in range(h)]
u = [[0] * w for i in range(h)]
d = [[0] * w for i in range(h)]

st = ""
std = ""

for i in range(h):
    st = s[i]
    std = s[h-i-1]
    for j in range(w):
        if st[j] == ".":
            if j>0:
                l[i][j] = l[i][j-1]+1
            elif j==0:
                l[i][j] = 1
        else:
            l[i][j] = 0
        if st[w-j-1] == ".":
            if j>0:
                r[i][w-j-1] = r[i][w-j] + 1
            elif j==0:
                r[i][w-j-1] = 1
        else:
            r[i][w-j-1] = 0
        if st[j] == ".":
            if i>0:
                u[i][j] = u[i-1][j] + 1
            elif i == 0:
                u[i][j] = 1
        else:
            u[i][j] = 0
        if std[j] == ".":
            if i>0:
                d[h-i-1][j] = d[h-i][j] + 1
            elif i == 0:
                d[h-i-1][j] = 1
        else:
            d[h-i-1][j] = 0

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]-3)

print(ans)

Submission status: Time Limit Exceeded

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

hw = []
for _ in range(H):
    tmp = list(input())
    hw.append(tmp)

L = []
for i in range(H):
    tmp = []
    if hw[i][0] == '.':
        tmp.append(1)
    else:
        tmp.append(0)

    for j in range(W-1):
        if hw[i][j+1] == '.':
            tmp.append(tmp[j]+1)
        else:
            tmp.append(0)
    L.append(tmp)

R = []
for i in range(H):
    tmp = [0]*W
    if hw[i][W-1] == '.':
        tmp[W-1] = 1
    else:
        tmp[W-1] = 0

    for j in reversed(range(W-1)):
        if hw[i][j] == '.':
            tmp[j] = tmp[j+1]+1
        else:
            tmp[j] = 0
    R.append(tmp)

U = [[0] * W for i in range(H)]
for i in range(W):
    if hw[0][i] == '.':
        U[0][i] = 1
    else:
        U[0][i] = 0

    for j in range(1, H):
        if hw[j][i] == '.':
            U[j][i] = U[j-1][i]+1
        else:
            U[j][i] = 0

D = [[0] * W for i in range(H)]
for i in range(W):
    if hw[H-1][i] == '.':
        D[H-1][i] = 1
    else:
        D[H-1][i] = 0

    for j in reversed(range(H-1)):
        if hw[j][i] == '.':
            D[j][i] = D[j+1][i]+1
        else:
            D[j][i] = 0

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]-3)
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
H,W=map(int,input().split())
S=[[i for i in list(input())]for _ in range(H)]
L=[[1]*W for _ in range(H)]
for i in range(H):
    count=1
    for j in range(W):
        if S[i][j]=="#":
            L[i][j]=0
            count=0
        elif 0<j:
            count=count+1
            L[i][j]=count
R=[[1]*W for _ in range(H)]
for i in range(H):
    count=1
    for j in range(W)[::-1]:
        if S[i][j]=="#":
            R[i][j]=0
            count=0
        elif j<W-1:
            count=count+1
            R[i][j]=count
U=[[1]*W for _ in range(H)]
for i in range(W):
    count=1
    for j in range(H):
        if S[j][i]=="#":
            U[j][i]=0
            count=0
        elif 0!=j:
            count=count+1
            U[j][i]=count
D=[[1]*W for _ in range(H)]
for i in range(W):
    count=1
    for j in range(H)[::-1]:
        if S[j][i]=="#":
            D[j][i]=0
            count=0
        elif j!=H-1:
            count=count+1
            D[j][i]=D[j+1][i]+1
ans=0
for i in range(H):
    for j in range(W):
        count=L[i][j]+R[i][j]+U[i][j]+D[i][j]-3
        ans=max(ans,count)
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
def main():
    h,w=map(int,input().split())
    s=[list(input()) for _ in range(h)]
    l=[[0]*w for _ in range(h)]
    r=[[0]*w for _ in range(h)]
    u=[[0]*w for _ in range(h)]
    d=[[0]*w for _ in range(h)]
    for i in range(h):
        for j in range(w):
            if s[i][j]=='.':
                if j==0:
                    l[i][j]=1
                else:
                    l[i][j]=l[i][j-1]+1
                if i==0:
                    d[i][j]=1
                else:
                    d[i][j]=d[i-1][j]+1
            if s[i][w-1-j]=='.':
                if j==0:
                    r[i][w-1-j]=1
                else:
                    r[i][w-1-j]=r[i][w-j]+1
            if s[h-1-i][j]=='.':
                if i==0:
                    u[h-1-i][j]=1
                else:
                    u[h-1-i][j]=u[h-i][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]-3)
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    h,w=map(int,input().split())
    s=[list(input()) for _ in range(h)]
    xyw=[[0]*w for _ in range(h)]
    xyh=[[0]*w for _ in range(h)]
    chk=[]
    for i in range(h):
        cnt=0
        for j in range(w):
            if s[i][j]=='#':
                while chk:
                    a=chk.pop()
                    xyw[i][a]=cnt
                cnt=0
                continue
            elif s[i][j]=='.' and j==w-1:
                cnt+=1
                chk.append(j)
                while chk:
                    a=chk.pop()
                    xyw[i][a]=cnt
                continue
            else:
                cnt+=1
                chk.append(j)
    ans=0
    for i in range(w):
        cnt=0
        for j in range(h):
            if s[j][i]=='#':
                while chk:
                    a=chk.pop()
                    xyh[a][i]=cnt
                    ans=max(ans,xyh[a][i]+xyw[a][i]-1)
                cnt=0
                continue
            elif s[j][i]=='.' and j==h-1:
                cnt+=1
                chk.append(j)
                while chk:
                    a=chk.pop()
                    xyh[a][i]=cnt
                    ans=max(ans,xyh[a][i]+xyw[a][i]-1)
                continue
            else:
                cnt+=1
                chk.append(j)
    print(ans)
    
if __name__ == '__main__':
    main()