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: Accepted

Solution code:
H, W=map(int, input().split())
frame=["".join(["#" for _ in range(W+2)])]
S=frame+["#"+input()+"#" for _ in range(H)]+frame
score=[[0 for _ in range(W+2)] for _ in range(H+2)]
for i in range(H+2):
  sharp_j=0
  for j in range(W+2):
    if S[i][j]=="#":
      for k in range(sharp_j+1, j):
        score[i][k]+=j-sharp_j-1
      sharp_j=j
for j in range(W+2):
  sharp_i=0
  for i in range(H+2):
    if S[i][j]=="#":
      for k in range(sharp_i+1, i):
        score[k][j]+=i-sharp_i-2
      sharp_i=i
ans=0
for i in range(H+2):
  for j in range(W+2):
    ans=max(ans, score[i][j])
print(ans)

Submission status: Accepted

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

a = [[0]*w for _ in range(h)]
for i in range(h):
    k = 0
    for j in range(w):
        if s[i][j] == ".":
            k += 1
        else:
            k = 0
        a[i][j] += k
for i in range(h):
    k = 0
    for j in reversed(range(w)):
        if s[i][j] == ".":
            k += 1
        else:
            k = 0
        a[i][j] += k
for j in range(w):
    k = 0
    for i in range(h):
        if s[i][j] == ".":
            k += 1
        else:
            k = 0
        a[i][j] += k
for j in range(w):
    k = 0
    for i in reversed(range(h)):
        if s[i][j] == ".":
            k += 1
        else:
            k = 0
        a[i][j] += k
b = max([max(i) for i in a])
print(0 if b == 0 else b-3)

Submission status: Accepted

Solution code:
def search_right(h, w):
    if h < 0 or h >= H or w < 0 or w >= W or S[h][w] == "#":
        return
    if S[h][w] == "." and answer_right[h][w-1] > 0:
        answer_right[h][w] = answer_right[h][w-1]
        return

    for i in range(w, W):
        if S[h][i] == ".":
            answer_right[h][w] += 1
        else:
            break


def search_down(h, w):
    if h < 0 or h >= H or w < 0 or w >= W or S[h][w] == "#":
        return
    if S[h][w] == "." and answer_down[h-1][w] > 0:
        answer_down[h][w] = answer_down[h-1][w]
        return

    for i in range(h, H):
        if S[i][w] == ".":
            answer_down[h][w] += 1
        else:
            break

H, W = map(int, input().split())
S = [0] * H
answer_right = [[0] * W for _ in range(H)]
answer_down = [[0] * W for _ in range(H)]
answer = 0
for i in range(H):
    S[i] = input()
for h in range(H):
    for w in range(W):
        search_right(h, w)
        search_down(h, w)
        answer = max(answer, answer_down[h][w]+answer_right[h][w] - 1)

print(answer)

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
S = [input() for _ in range(H)]
l = [[0] * (W+2) for _ in range(H+2)]
u = [[0] * (W+2) for _ in range(H+2)]
r = [[0] * (W+2) for _ in range(H+2)]
d = [[0] * (W+2) for _ in range(H+2)]

for i in range(1, H+1):
    for j in range(1, W+1):
        if S[i-1][j-1] != '#':
            l[i][j] += l[i][j-1] + 1
            u[i][j] += u[i-1][j] + 1
        if S[-i][-j] != '#':
            r[-i-1][-j-1] += r[-i-1][-j] + 1
            d[-i-1][-j-1] += d[-i][-j-1] + 1

ans = 0
for i in range(1, H+1):
    for j in range(1, W+1):
        ans = max(ans, l[i][j]+u[i][j]+r[i][j]+d[i][j]-3)

print(ans)

Submission status: Accepted

Solution code:
MOD = 10 ** 9 + 7


# def dfs(graph, start):
#     options = [(-1, 0), (0, 1), (1, 0), (0, -1)]
#     W = len(graph)
#     H = len(graph[0])
#     ans = 0

#     for x in range(len(graph)):
#         for y in range(len(graph[x])):
#             if graph[x][y] == '#':
#                 continue

#             val = 1
#             path = []
#             stack = [(x, y, options)]
#             # print(x, y)

#             while stack != []:
#                 v = stack.pop()

#                 if v not in path:
#                     path.append(v)

#                 for op in v[2]:
#                     n = (v[0] + op[0], v[1] + op[1], [op])

#                     if n[0] >= 0 and n[0] < W and n[1] >= 0 and n[1] < H:
#                         f = graph[n[0]][n[1]]
#                         if n not in path and f == '.':
#                             stack.append(n)
#                             val += 1

#             ans = max(ans, val)
#             # print(ans)

#             if ans == 3999:
#                 return ans

#     return ans


def main():
    H, W = map(int, input().split())
    S = [None] * H
    w_score = [[0 for i in range(W)] for i in range(H)]
    h_score = [[0 for i in range(W)] for i in range(H)]
    stack = []

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

    for x in range(H):
        v = 0
        stack = []
        for y in range(W+1):
            if x >= 0 and x < H and y >= 0 and y < W and S[x][y] == '.':
                v += 1
                stack.append((x, y))
            else:
                if len(stack) == 0:
                    # w_score[x][y] = 0
                    stack = []
                elif len(stack) == 1:
                    first = stack.pop(0)
                    w_score[x][first[1]] = v
                    stack = []
                    v = 0
                else:
                    first = stack.pop(0)
                    last = stack.pop()
                    for y in range(first[1], last[1]+1):
                        w_score[x][y] = v
                    stack = []
                    v = 0

    for y in range(W):
        v = 0
        stack = []
        for x in range(H+1):
            if x >= 0 and x < H and y >= 0 and y < W and S[x][y] == '.':
                v += 1
                stack.append((x, y))
            else:
                if len(stack) == 0:
                    # w_score[x][y] = 0
                    stack = []
                elif len(stack) == 1:
                    first = stack.pop(0)
                    h_score[first[0]][y] = v
                    stack = []
                    v = 0
                else:
                    first = stack.pop(0)
                    last = stack.pop()
                    for x in range(first[0], last[0]+1):
                        h_score[x][y] = v
                    stack = []
                    v = 0

    ans = 0
    for x in range(H):
        for y in range(W):
            ans = max(ans, w_score[x][y] + h_score[x][y])

    print(ans-1)
    # print(w_score)
    # print(h_score)
    # print(dfs(S, S[0][0]))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**9)


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


def main():
    H, W = map(int, input().split())
    S = [list(input()) for i in range(H)]
    ans = float(0)
    memo_y = [[-float('inf')]*W for i in range(H)]
    memo_x = [[-float('inf')]*W for i in range(H)]
    for y in range(H):
        for x in range(W):
            c = float(0)
            if S[y][x] == '.':
                c += float(1)
                c_x = float(0)
                c_y = float(0)
                tmp_x = [x]
                tmp_y = [y]
                if memo_x[y][x] == -float('inf'):
                    for i in range(1, x+1):
                        if S[y][x-i] == '.':
                            c_x += float(1)
                            tmp_x.append(i)
                        else:
                            break
                    for i in range(x+1, W):
                        if S[y][i] == '.':
                            c_x += float(1)
                            tmp_x.append(i)
                        else:
                            break
                    for i in tmp_x:
                        memo_x[y][i] = c_x
                else:
                    c_x += memo_x[y][x]
                if memo_y[y][x] == -float('inf'):
                    for i in range(1, y+1):
                        if S[y-i][x] == '.':
                            c_y += float(1)
                            tmp_y.append(i)
                        else:
                            break
                    for i in range(y+1, H):
                        if S[i][x] == '.':
                            c_y += float(1)
                            tmp_y.append(i)
                        else:
                            break
                    for i in tmp_y:
                        memo_y[i][x] = c_y
                else:
                    c_y += memo_y[y][x]
                c += c_x + c_y
            ans = max(ans, c)
    print(int(ans))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**9)


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


def main():
    H, W = map(int, input().split())
    S = [list(input()) for i in range(H)]
    ans = 0
    memo_y = [[-1]*W for i in range(H)]
    memo_x = [[-1]*W for i in range(H)]
    for y in range(H):
        for x in range(W):
            c = 0
            if S[y][x] == '.':
                c += 1
                c_x = 0
                c_y = 0
                tmp_x = [x]
                tmp_y = [y]
                if memo_x[y][x] == -1:
                    for i in range(1, x+1):
                        if S[y][x-i] == '.':
                            c_x += 1
                            tmp_x.append(i)
                        else:
                            break
                    for i in range(x+1, W):
                        if S[y][i] == '.':
                            c_x += 1
                            tmp_x.append(i)
                        else:
                            break
                    for i in tmp_x:
                        memo_x[y][i] = c_x
                else:
                    c_x += memo_x[y][x]
                if memo_y[y][x] == -1:
                    for i in range(1, y+1):
                        if S[y-i][x] == '.':
                            c_y += 1
                            tmp_y.append(i)
                        else:
                            break
                    for i in range(y+1, H):
                        if S[i][x] == '.':
                            c_y += 1
                            tmp_y.append(i)
                        else:
                            break
                    for i in tmp_y:
                        memo_y[i][x] = c_y
                else:
                    c_y += memo_y[y][x]
                c += c_x + c_y
            ans = max(ans, c)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**9)


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


def main():
    H, W = map(int, input().split())
    S = [list(input()) for i in range(H)]
    ans = 0
    memo_y = [[float(-1)]*W for i in range(H)]
    memo_x = [[float(-1)]*W for i in range(H)]
    for y in range(H):
        for x in range(W):
            c = 0
            if S[y][x] == '.':
                c += 1
                c_x = 0
                c_y = 0
                tmp_x = [x]
                tmp_y = [y]
                if memo_x[y][x] == float(-1):
                    for i in range(1, x+1):
                        if S[y][x-i] == '.':
                            c_x += 1
                            tmp_x.append(i)
                        else:
                            break
                    for i in range(x+1, W):
                        if S[y][i] == '.':
                            c_x += 1
                            tmp_x.append(i)
                        else:
                            break
                    for i in tmp_x:
                        memo_x[y][i] = c_x
                else:
                    c_x += memo_x[y][x]
                if memo_y[y][x] == float(-1):
                    for i in range(1, y+1):
                        if S[y-i][x] == '.':
                            c_y += 1
                            tmp_y.append(i)
                        else:
                            break
                    for i in range(y+1, H):
                        if S[i][x] == '.':
                            c_y += 1
                            tmp_y.append(i)
                        else:
                            break
                    for i in tmp_y:
                        memo_y[i][x] = c_y
                else:
                    c_y += memo_y[y][x]
                c += c_x + c_y
            ans = max(ans, c)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import numpy as np
 
H, W = map(int, input().split())
S = np.array([list(input()) for _ in range(H)]) == '.'

#上下左右方向に塗られるマスの数を格納する配列
up = np.zeros((H, W), dtype=int)
down = np.zeros((H, W), dtype=int)
left = np.zeros((H, W), dtype=int)
right = np.zeros((H, W), dtype=int)

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

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

Submission status: Accepted

Solution code:
import sys


def solve():
    readline = sys.stdin.buffer.readline
    mod = 10 ** 9 + 7
    h, w = list(map(int, readline().split()))
    s = [list(str(readline().rstrip().decode('utf-8'))) for _ in range(h)]
    ul = [[0] * w for _ in range(h)]
    dl = [[0] * w for _ in range(h)]
    ll = [[0] * w for _ in range(h)]
    rl = [[0] * w for _ in range(h)]

    for i in range(h):
        for j in range(w):
            if i == 0:
                ul[i][j] = 1 if s[i][j] != "#" else 0
                dl[-i-1][j] = 1 if s[-i-1][j] != "#" else 0
            else:
                ul[i][j] = 1 + ul[i-1][j] if s[i][j] != "#" else 0
                dl[-i-1][j] = 1 + dl[-i][j] if s[-i-1][j] != "#" else 0
            if j == 0:
                ll[i][j] = 1 if s[i][j] != "#" else 0
                rl[i][-j-1] = 1 if s[i][-j-1] != "#" else 0
            else:
                ll[i][j] = 1 + ll[i][j-1] if s[i][j] != "#" else 0
                rl[i][-j-1] = 1 + rl[i][-j] if s[i][-j-1] != "#" else 0
    mt = 0
    for i in range(h):
        for j in range(w):
            mt = max(mt, ul[i][j] + dl[i][j] + ll[i][j] + rl[i][j] - 3)
    print(mt)


if __name__ == '__main__':
    solve()

Submission status: Accepted

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

sll = []
for _ in range(h):
    sl = list(input())
    sll.append(sl)

yoko = [[] for i in range(h)]
for idx, sl in enumerate(sll):
    point = 0
    for s in sl:
        if s == "#":
            if point > 0:
                for _ in range(point):
                    yoko[idx].append(point)
            yoko[idx].append(0)
            point = 0
        else:
            point += 1
    if point > 0:
        for _ in range(point):
            yoko[idx].append(point)


tate = [[0 for i in range(w)] for i in range(h)]
for i in range(w):
    point = 0
    for idx,sl in enumerate(sll):
        if sl[i] == "#":
            if point > 0:
                for j in range(1, point+1):
                    tate[idx-j][i] = point
            tate[idx][i] = 0
            point = 0
        else:
            point += 1
    if point > 0:
        for j in range(1, point+1):
            tate[idx-j+1][i] = point


ans = 0
for yokol, tatel in zip(yoko,tate):
    for y, t in zip(yokol, tatel):
        if y+t > ans:
            ans = y+t

print(ans-1)

Submission status: Accepted

Solution code:
from numpy import*
h,w=map(int,input().split())
a=array([[0 if i!="." else 1 for i in input()]for j in range(h)])
e=zeros((4,h,w))
for i in range(h):
    e[0,i]=(e[0,i-1]+1)*a[i]
    e[1,-i-1]=(e[1,-i]+1)*a[-i-1]  
for i in range(w):
    e[2,:,i]=(e[2,:,i-1]+1)*a[:,i]
    e[3,:,-i-1]=(e[3,:,-i]+1)*a[:,-i-1] 
print(int(amax(sum(e, axis=0))-3))

Submission status: Accepted

Solution code:
import numpy as np
h,w=map(int,input().split())
a=np.array([[0 if i!="." else 1 for i in input()]for j in range(h)])
e=np.zeros((4,h,w))
for i in range(h):
    e[0,i]=(e[0,i-1]+1)*a[i]
    e[1,-i-1]=(e[1,-i]+1)*a[-i-1]  
for i in range(w):
    e[2,:,i]=(e[2,:,i-1]+1)*a[:,i]
    e[3,:,-i-1]=(e[3,:,-i]+1)*a[:,-i-1] 
print(int(np.amax(np.sum(e, axis=0))-3))

Submission status: Accepted

Solution code:
def main():
    h,w = map(int,input().split())
    s=[]
    for _ in range(h):
        s.append(list(input()))
    ans=[[0 for _ in range(w)] for _ in range(h)]
    L=[[0 for _ in range(w)] for _ in range(h)]
    R=[[0 for _ in range(w)] for _ in range(h)]
    U=[[0 for _ in range(w)] for _ in range(h)]
    D=[[0 for _ in range(w)] for _ in range(h)]
    for i in range(h):
        for j in range(w):
            if i==0:
                if s[i][j]==".":
                    U[i][j]=1
                else:
                    U[i][j]=0
            else:
                if s[i][j]==".":
                    U[i][j]=U[i-1][j]+1
                else:
                    U[i][j]=0
            if i==0:
                if s[h-1-i][j]==".":
                    D[h-1-i][j]=1
                else:
                    D[h-1-i][j]=0
            else:
                if s[h-1-i][j]==".":
                    D[h-1-i][j]=D[h-i][j]+1
                else:
                    D[h-1-i][j]=0
    
    
    for i in range(h):
        for j in range(w):
            if j==0:
                if s[i][j]==".":
                    L[i][j]=1
                else:
                    L[i][j]=0
            else:
                if s[i][j]==".":
                    L[i][j]=L[i][j-1]+1
                else:
                    L[i][j]=0
            if j==0:
                if s[i][w-1-j]==".":
                    R[i][w-1-j]=1
                else:
                    R[i][w-1-j]=0
            else:
                if s[i][w-1-j]==".":
                    R[i][w-1-j]=R[i][w-j]+1
                else:
                    R[i][w-1-j]=0
                    
    for i in range(h):
        for j in range(w):
            ans[i][j]=L[i][j]+R[i][j]+U[i][j]+D[i][j]
    a=1
    for i in range(len(ans)):
        b=max(ans[i])
        a=max(a,b)
    print(a-3)
    

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
h,w = map(int,input().split())
s = ["#"*(w+2)]+["#"+input()+"#" for _ in range(h)]+["#"*(w+2)]
res = 0
l = [[0 for _ in range(w+2)] for _ in range(h+2)]
r = [[0 for _ in range(w+2)] for _ in range(h+2)]
d = [[0 for _ in range(w+2)] for _ in range(h+2)]
u = [[0 for _ in range(w+2)] for _ in range(h+2)]
for i in range(1,h+1):
    for j in range(1,w+1):
       if s[i][j]==".":
           l[i][j]=l[i][j-1]+1 
           d[i][j]=d[i-1][j]+1 
for i in range(h,0,-1):
    for j in range(w,0,-1):
       if s[i][j]==".":
           r[i][j]=r[i][j+1]+1 
           u[i][j]=u[i+1][j]+1 


for i in range(1,h+1):
    for j in range(1,w+1):
        if s[i][j] == ".":
            res = max(res,l[i][j]+r[i][j]+u[i][j]+d[i][j]-3)
            if res == h+w-1:break

print(res)

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
s=['#'*(w+2)]+['#'+input()+'#' for _ in range(h)]+['#'*(w+2)]
u=[[0]*(w+2) for _ in range(h+2)]
d=[[0]*(w+2) for _ in range(h+2)]
l=[[0]*(w+2) for _ in range(h+2)]
r=[[0]*(w+2) for _ in range(h+2)]
for i in range(1,h+1):
  for j in range(1,w+1):
    if s[i][j]=='.':
      l[i][j]=l[i][j-1]+1
    if s[i][w+1-j]=='.':  
      r[i][w+1-j]=r[i][w+2-j]+1
for i in range(1,w+1):
  for j in range(1,h+1):
    if s[j][i]=='.':
      u[j][i]=u[j-1][i]+1
    if s[h+1-j][i]=='.':  
      d[h+1-j][i]=d[h+2-j][i]+1
m=0
for i in range(1,h+1):
  for j in range(1,w+1):
    m=max(m,u[i][j]+d[i][j]+l[i][j]+r[i][j]-3)
print(m)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.readline().rstrip()
def resolve():
    h,w=map(int,input().split())
    grid=[0]*h
    for i in range(h):
        grid[i]=input()
    Sl=[[0]*w for _ in range(h)]
    Sr=[[0]*w for _ in range(h)]
    Su=[[0]*w for _ in range(h)]
    Sd=[[0]*w for _ in range(h)]

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

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

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

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

    ans=0
    for i in range(h):
        for j in range(w):
            if(grid[i][j]=='#'): continue
            ans=max(Sl[i][j]+Sr[i][j]+Su[i][j]+Sd[i][j]-3,ans)
    print(ans)
resolve()

Submission status: Accepted

Solution code:
def p_d():
    hb, wb = map(int, input().split())
    t = [input() for _ in range(hb)]
    lb = [[0] * (wb + 2) for _ in range(hb + 2)]
    rb = [[0] * (wb + 2) for _ in range(hb + 2)]
    ub = [[0] * (wb + 2) for _ in range(hb + 2)]
    db = [[0] * (wb + 2) for _ in range(hb + 2)]
    for h in range(1, hb + 1):
        for w in range(1, wb + 1):
            if t[h - 1][w - 1] != "#":
                lb[h][w] = lb[h][w - 1] + 1
                ub[h][w] = ub[h - 1][w] + 1
            if t[hb - h][wb - w] != "#":
                rb[hb - h + 1][wb - w + 1] = rb[hb - h + 1][wb - w + 2] + 1
                db[hb - h + 1][wb - w + 1] = db[hb - h + 2][wb - w + 1] + 1
    ans = 0
    for h in range(1, hb + 1):
        for w in range(1, wb + 1):
            tmp = lb[h][w] + rb[h][w] + ub[h][w] + db[h][w]
            ans = max(ans, tmp - 3)
    print(ans)
    
p_d()

Submission status: Accepted

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

for i in range(H):
    start = -1
    cnt = 0
    for j in range(W + 1):
        if j == W or S[i][j] == '#':
            for k in range(start + 1, j):
                X[i][k] = cnt
            cnt = 0
            start = j
        else:
            cnt += 1

for j in range(W):
    start = -1
    cnt = 0
    for i in range(H + 1):
        if i == H or S[i][j] == '#':
            for k in range(start + 1, i):
                Y[k][j] = cnt
            cnt = 0
            start = i
        else:
            cnt += 1

m = 0
for i in range(H):
    for j in range(W):
        m = max(m, X[i][j] + Y[i][j])

print(m - 1)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys

def solve(H: int, W: int, S: "List[str]"):
    ss = [list(s) for s in S]
    answer = 0
    right = [[0]*W for _ in range(H)]
    left = [[0]*W for _ in range(H)]
    up = [[0]*W for _ in range(H)]
    down = [[0]*W for _ in range(H)]

    for h in range(H):
        for j in range(W):
            if ss[h][j] == ".":
                if j>0:
                    right[h][j] = 1+right[h][j-1]
                else:
                    right[h][j] += 1
            else:
                right[h][j] = 0

        for j in range(W-1,-1,-1):
            if ss[h][j] == ".":
                if j<W-1:
                    left[h][j] = 1+left[h][j+1]
                else:
                    left[h][j] += 1
            else:
                left[h][j] = 0
    for w in range(W):                
        for i in range(H-1,-1,-1):
            if ss[i][w] == '.':
                if i < H-1:
                    up[i][w] = 1 + up[i+1][w]
                else:
                    up[i][w] +=1
            else:
                up[i][w] = 0
        for i in range(0,H):
            if ss[i][w] == '.':
                if i > 0:
                    down[i][w] = 1 + down[i-1][w]
                else:
                    down[i][w] +=1
            else:
                down[i][w] = 0

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

    print(answer)
    return


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 = [next(tokens) for _ in range(H)]  # type: "List[str]"
    solve(H, W, S)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
tmp = list(map(int, input().split()))
h = tmp[0]
w = tmp[1] 

s = ['None'] * (h + 1)
for i in range(h):
  s[i] = input()
  s[i] = s[i] + '#'
s[h] = '#' * w

ans = 0

sta = -1
cnt = 0
pt = [[0] * w for i in range(h)]
for i in range(h):
  for j in range(w+1):
    if s[i][j] == '#':
      if sta != -1:
        for k in range(sta, j):
          pt[i][k] = cnt
      cnt = 0
      sta = -1
    else:
      if sta == -1:
        sta = j
      cnt += 1

for i in range(w):
  for j in range(h+1):
    if s[j][i] == '#':
      if sta != -1:
        for k in range(sta, j):
          pt[k][i] += cnt - 1
          if pt[k][i] > ans:
            ans = pt[k][i]
      cnt = 0
      sta = -1
    else:
      if sta == -1:
        sta = j
      cnt += 1

print(ans)

Submission status: Accepted

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

# L, R, U, D方向の照らされるマス数を事前に計算
L = [[0] * W for _ in range(H)]
R = [[0] * W for _ in range(H)]
U = [[0] * W for _ in range(H)]
D = [[0] * W for _ in range(H)]

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

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

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

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

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: Accepted

Solution code:
# coding: utf-8
# Your code here!
H,W=map(int,input().split())
S=[]
ans=0

for _ in range(H):
    S.append(list(input()))

l=[[0]*W for _ in range(H)]

for h in range(H):
    count=0
    start=0
    for i in range(W):
        if S[h][i]=="#":
            count=0
            start=i+1
        else:
            count+=1
            l[h][i]+=count

for h in range(H):
    count=0
    start=W-1
    for i in range(W)[::-1]:
        if S[h][i]=="#":
            count=0
            start=i
        else:
            count+=1
            l[h][i]+=count

for w in range(W):
    count=0
    start=0
    for i in range(H):
        if S[i][w]=="#":
            count=0
            start=i+1
        else:
            count+=1
            l[i][w]+=count

for w in range(W):
    count=0
    start=H-1
    for i in range(H)[::-1]:
        if S[i][w]=="#":
            count=0
            start=i
        else:
            count+=1
            l[i][w]+=count
            ans=max(ans,l[i][w])

print(ans-3)

Submission status: Accepted

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

for i, row in enumerate(s):
    cnt = 0
    for j, x in enumerate(row):
        if x == "#":
            for k in range(j-cnt, j): area[i][k] = cnt
            cnt = 0
        else: cnt += 1
    for k in range(w-cnt, w): area[i][k] = cnt

for i in range(w):
    cnt = 0
    for j in range(h):
        x = s[j][i]
        if x == "#":
            for k in range(j-cnt, j): area[k][i] += cnt - 1
            cnt = 0
        else: cnt += 1
    for k in range(h-cnt, h): area[k][i] += cnt - 1

ans = 0
for i in range(h):
    for j in range(w):
        ans = max(ans, area[i][j])

print(ans)

Submission status: Accepted

Solution code:
import numpy as np
import sys
line = sys.stdin.readline

H, W = map(int, line().split())
grid = np.zeros((H + 2, W + 2), np.int32)
grid[1:-1, 1:-1] = (np.array([list(line().rstrip()) for _ in range(H)]).reshape(H, W) == '.') * 1
left = np.zeros((H + 2, W + 2), np.int32)
right = np.zeros((H + 2, W + 2), np.int32)
up = np.zeros((H + 2, W + 2), np.int32)
down = np.zeros((H + 2, W + 2), np.int32)

# 下方向に照らせるマスの数（自分の場所含む）
down[H] = grid[H]
for i in range(H, 0, -1):
    down[i - 1] = (down[i] + 1) * grid[i - 1]

up[1] = grid[1]
for i in range(1, H):
    up[i + 1] = (up[i] + 1) * grid[i + 1]

right[:, W] = grid[:, W]
for i in range(W, 0, -1):
    right[:, i - 1] = (right[:, i] + 1) * grid[:, i - 1]

left[:, 1] = grid[:, 1]
for i in range(1, W):
    left[:, i + 1] = (left[:, i] + 1) * grid[:, i + 1]

answer = (left + right + up + down - 3) * grid
print(np.max(answer))