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())
G = [[1 if s == '#' else 0 for s in input().strip()] for _ in range(H)]
T = [[0]*W for _ in range(H)]
for j, g in enumerate(G):
    S = [0]*W
    pre = 0
    for ig, g in enumerate(g):
        if g:
            pre = 0
            continue
        pre += 1
        S[ig] = pre
    pre = 0
    for k, s in enumerate(S[::-1]):
        k = W - 1 - k
        if not s:
            pre = 0
            continue
        pre = max(pre, s)
        T[j][k] = pre

G = list(map(list, zip(*G)))
T2 = [[0]*H for _ in range(W)]

for j, g in enumerate(G):
    S = [0]*H
    pre = 0
    for ig, g in enumerate(g):
        if g:
            pre = 0
            continue
        pre += 1
        S[ig] = pre
    pre = 0
    for k, s in enumerate(S[::-1]):
        k = H - 1 - k
        if not s:
            pre = 0
            continue
        pre = max(pre, s)
        T2[j][k] = pre
T2 = list(map(list, zip(*T2)))
ans = 0
for i in range(H):
    for j in range(W):
        ans = max(ans, T[i][j] + T2[i][j])
print(ans - 1)

Submission status: Accepted

Solution code:
H,W = map(int,input().split())
S = ["#"*(W+2)]+["#"+input()+"#" for _ in [0]*H] + ["#"*(W+2)]

h = [[-int(c=="#") for c in row] for row in S]
v = [[-int(c=="#") for c in row] for row in S]

for i in range(1,H+1):
    l = 1
    cnt = 0
    for j in range(1,W+2):
        if S[i][j] == "#" and S[i][j-1] == ".":
            h[i][l:j] = [cnt]*(j-l)
        elif S[i][j] == "." and S[i][j-1] == "#":
            l = j
            cnt = 1
        elif S[i][j] == "." and S[i][j-1] == ".":
            cnt += 1
            
for j in range(1,W+1):
    l = 1
    cnt = 0
    for i in range(1,H+2):
        if S[i][j] == "#" and S[i-1][j] == ".":
            for k in range(l,i):
                v[k][j] = cnt
        elif S[i][j] == "." and S[i-1][j] == "#":
            l = i
            cnt = 1
        elif S[i][j] == "." and S[i-1][j] == ".":
            cnt += 1

print(max(h[i][j]+v[i][j] for i in range(1,H+1) for j in range(1,W+2))-1)

Submission status: Accepted

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

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

for i in range(h):
    for j in range(w-1):
        if s[i][j]=='.':
            l[i][j+1] = l[i][j]+1

for i in range(h):
    for j in range(1,w):
        if s[i][w-j]=='.':
            r[i][w-j-1] = r[i][w-j]+1

for i in range(h-1):
    for j in range(w):
        if s[i][j]=='.':
            u[i+1][j] = u[i][j]+1

for i in range(1,h):
    for j in range(w):
        if s[h-i][j]=='.':
            d[h-i-1][j] = d[h-i][j]+1

ans = 0
for i in range(h):
    for j in range(w):
        if s[i][j]=='.':
            ans = max(ans, l[i][j]+r[i][j]+u[i][j]+d[i][j]+1)

print(ans)

Submission status: Accepted

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

U = [[0] * W for j in range(H)]
L = [[0] * W for j in range(H)]
R = [[0] * W for j in range(H)]
D = [[0] * W for j in range(H)]
for i in range(H):
    for j in range(W):
        if i > 0:
            if T[i][j] != "#":
                U[i][j] = U[i - 1][j] + 1
        else:
            U[i][j] = int(T[i][j] != "#")

        if j > 0:
            if T[i][j] != "#":
                L[i][j] = L[i][j - 1] + 1
        else:
            L[i][j] = int(T[i][j] != "#")

for i in range(H - 1, -1, -1):
    for j in range(W - 1, -1, -1):
        if i < H - 1:
            if T[i][j] != "#":
                D[i][j] = D[i + 1][j] + 1
        else:
            D[i][j] = int(T[i][j] != "#")

        if j < W - 1:
            if T[i][j] != "#":
                R[i][j] = R[i][j + 1] + 1
        else:
            R[i][j] = int(T[i][j] != "#")


num = 0
for i in range(H):
    for j in range(W):
        tmp = 0
        if T[i][j] == '.':
            tmp += U[i][j] + D[i][j] + R[i][j] + L[i][j] - 3
        num = max(num, tmp)
print(num)

Submission status: Accepted

Solution code:
from itertools import accumulate

def getNums(Ss):
    lenS = len(Ss)
    nums = [0] * lenS
    Ss += '#'
    num = -1
    for i, S in enumerate(Ss):
        if S == '.':
            num += 1
        else:
            if num > 0:
                nums[i-(num+1)] = num
                if i < lenS:
                    nums[i] = -num
            num = -1
    return accumulate(nums)


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

numHss = [list(getNums(Ss)) for Ss in Sss]
Sss_tr = list(map(''.join, zip(*Sss))) # 転置
numVss_tr = [getNums(Ss) for Ss in Sss_tr]
numVss = list(map(list, zip(*numVss_tr))) # 転置

ans = 0
for i in range(H):
    for j in range(W):
        num = numHss[i][j] + numVss[i][j] + 1
        if num > ans:
            ans = num

print(ans)

Submission status: Accepted

Solution code:
import sys
def input():
	return sys.stdin.readline()[:-1]

H, W = map(int, input().split())
s = [input() for _ in range(H)]
tate = [[0 for _ in range(W)] for _ in range(H)]
yoko = [[0 for _ in range(W)] for _ in range(H)]
for h in range(H):
	ba, fr = 0, 0
	while ba < W:
		if s[h][ba] == "#":
			ba += 1
			fr += 1
		while fr < W and s[h][fr] == ".":
			fr += 1
		for w in range(ba, fr):
			yoko[h][w] = fr-ba
		ba = fr
#print(yoko, sep="\n")
for w in range(W):
	ba, fr = 0, 0
	while ba < H:
		if s[ba][w] == "#":
			ba += 1
			fr += 1
		while fr < H and s[fr][w] == ".":
			fr += 1
		for h in range(ba, fr):
			tate[h][w] = fr-ba
		ba = fr
#print(tate, sep="\n")
ans = 1
for h in range(H):
	for w in range(W):
		ans = max(ans, yoko[h][w]+tate[h][w]-1)
print(ans)

Submission status: Accepted

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

T = [[0 if S[j][i] == '#' else 1 for i in range(W)] for j in range(H)]

for h in range(H):
    s = 0
    for w in range(W):
        if S[h][w] == '#':
            s = 0
            continue
        T[h][w] += s
        s += 1

for h in range(H):
    s = 0
    for w in range(W - 1, -1, -1):
        if S[h][w] == '#':
            s = 0
            continue
        T[h][w] += s
        s += 1

for w in range(W):
    s = 0
    for h in range(H):
        if S[h][w] == '#':
            s = 0
            continue
        T[h][w] += s
        s += 1

for w in range(W):
    s = 0
    for h in range(H - 1, -1, -1):
        if S[h][w] == '#':
            s = 0
            continue
        T[h][w] += s
        s += 1

print(max(max(t) for t in T))

Submission status: Accepted

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

# あるマスから（右・下・左・上）にいくつ障害物ないマスが続くか
right_space = [[-1] * (W + 2) for i in range(H + 2)]
down_space = [[-1] * (W + 2) for i in range(H + 2)]
left_space = [[-1] * (W + 2) for i in range(H + 2)]
up_space = [[-1] * (W + 2) for i in range(H + 2)]

# 左上から更新（上・左に対応）
for h in range(1, H + 1):
    for w in range(1, W + 1):
        if G[h - 1][w - 1] == '.':
            up_space[h][w] = up_space[h - 1][w] + 1
            left_space[h][w] = left_space[h][w - 1] + 1

# 右下から更新（下・右に対応）
for h in reversed(range(1, H + 1)):
    for w in reversed(range(1, W + 1)):
        if G[h - 1][w - 1] == '.':
            down_space[h][w] = down_space[h + 1][w] + 1
            right_space[h][w] = right_space[h][w + 1] + 1

ans = 0
for h in range(1, H + 1):
    for w in range(1, W + 1):
        if G[h - 1][w - 1] == '.':
            tmp_ans = right_space[h][w] + down_space[h][w] + left_space[h][w] + up_space[h][w] + 1
            ans = max(ans, tmp_ans)

print(ans)

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!


#n = int(input())
#s = input()
#a = [int(i) for i in input().split()]
#m = [[int(i) for i in input().split()] for j in range(n)] # 縦「n」行


h,w = [int(i) for i in input().split()]
b = [input() for i in range(h)]

ans = [[0]*w for i in range(h)]

for i in range(h):
    res = 0
    for j in range(w):
        if b[i][j] == ".":
            ans[i][j] += res
        if b[i][j] == ".":
            res += 1
        else:
            res = 0

#print(ans)

for i in range(h):
    res = 0
    for j in range(w-1,-1,-1):
        if b[i][j] == ".":
            ans[i][j] += res
        if b[i][j] == ".":
            res += 1
        else:
            res = 0

#print(ans)

for j in range(w):
    res = 0
    for i in range(h):
        if b[i][j] == ".":
            ans[i][j] += res
        if b[i][j] == ".":
            res += 1
        else:
            res = 0

#print(ans)

for j in range(w):
    res = 0
    for i in range(h-1,-1,-1):
        if b[i][j] == ".":
            ans[i][j] += res
        if b[i][j] == ".":
            res += 1
        else:
            res = 0

#print(ans)

print(max(max(row) for row in ans) + 1)

Submission status: Accepted

Solution code:
def main():
    H, W = map(int, input().split())
    S = [input() for _ in range(H)]
    grid = [[0] * W for _ in range(H)]
    for i in range(H):
        seq = 0
        start = 0
        for j in range(W):
            if S[i][j] == "#":
                for x in range(start, j):
                    grid[i][x] += seq
                seq = 0
                start = j+1
            else:
                seq += 1
        if seq:
            for x in range(start, W):
                grid[i][x] += seq
    for j in range(W):
        seq = 0
        start = 0
        for i in range(H):
            if S[i][j] == "#":
                for x in range(start, i):
                    grid[x][j] += seq
                seq = 0
                start = i+1
            else:
                seq += 1
        if seq:
            for x in range(start, H):
                grid[x][j] += seq
    ans = 1    
    for i in range(H):
        for j in range(W):
            if grid[i][j]:
                ans = max(grid[i][j] - 1, ans)
    print(ans)



if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
H,W=map(int,input().split())
S=[list(input()) for i in range(H)]
Iplus=[[-1 for j in range(W)] for i in range(H)]
Iminus=[[-1 for j in range(W)] for i in range(H)]
Jplus=[[-1 for j in range(W)] for i in range(H)]
Jminus=[[-1 for j in range(W)] for i in range(H)]
for j in range(W):
    if S[0][j]==".":
        Iplus[0][j]=0
for i in range(1,H):
    for j in range(W):
        if S[i][j]==".":
            Iplus[i][j]=Iplus[i-1][j]+1
        else:
            Iplus[i][j]=-1
for j in range(W):
    if S[-1][j]==".":
        Iminus[-1][j]=0
for i in range(H-1)[::-1]:
    for j in range(W):
        if S[i][j]==".":
            Iminus[i][j]=Iminus[i+1][j]+1
        else:
            Iminus[i][j]=-1
for i in range(H):
    if S[i][0]==".":
        Jplus[i][0]=0
for j in range(1,W):
    for i in range(H):
        if S[i][j]==".":
            Jplus[i][j]=Jplus[i][j-1]+1
        else:
            Jplus[i][j]=-1
for i in range(H):
    if S[i][-1]==".":
        Jminus[i][-1]=0
for j in range(W-1)[::-1]:
    for i in range(H):
        if S[i][j]==".":
            Jminus[i][j]=Jminus[i][j+1]+1
        else:
            Jminus[i][j]=-1
ans=0
for i in range(H):
    for j in range(W):
        tmp=Iplus[i][j]+Iminus[i][j]+Jplus[i][j]+Jminus[i][j]+1
        if ans<tmp:
            ans=tmp
            #print(i,j,tmp)
#print(Iplus[2][5],Iminus[2][5],Jplus[2][5],Jminus[2][5])
print(ans)
#for line in Jminus:
#    print(line)

Submission status: Accepted

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

b = [list(input()) for i in range(h)]
yoko = [[0]*w for i in range(h)]
for i in range(h):
    now = 0
    while now < w:
        if b[i][now] == "#":
            now += 1
            continue
        cnt = 0
        to = now
        while to<w and b[i][to] == ".":
            cnt += 1
            to += 1
        for j in range(now, to):
            yoko[i][j] = cnt
        now = to

for i in range(w):
    now = 0
    while now < h:
        if b[now][i] == "#":
            now += 1
            continue
        cnt = 0
        to = now
        while to<h and b[to][i] == ".":
            cnt += 1
            to += 1
        for j in range(now, to):
            yoko[j][i] += cnt
        now = to

ans = 0
for i in range(h):
    for j in range(w):
        if ans < yoko[i][j]:
            ans = yoko[i][j]
if ans:
    ans -= 1
print(ans)

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
X = [[[0, 1][a=="#"] for a in input()] for i in range(H)]
Y = [[0]*W for _ in range(H)]
Z = [[0]*W for _ in range(H)]

for i in range(H):
    t = 0
    for j in range(W):
        if X[i][j]:
            t = 0
        else:
            t += 1
            Y[i][j] = t

for i in range(H):
    t = 0
    for j in range(W)[::-1]:
        if X[i][j]:
            t = 0
        else:
            Y[i][j] += t
            t += 1

for j in range(W):
    t = 0
    for i in range(H):
        if X[i][j]:
            t = 0
        else:
            t += 1
            Z[i][j] = t

for j in range(W):
    t = 0
    for i in range(H)[::-1]:
        if X[i][j]:
            t = 0
        else:
            Z[i][j] += t
            t += 1

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

print(max(ans-1, 0))

Submission status: Accepted

Solution code:
#ABC129-D
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(input())
def S(): return list(input())
h,w=IL()
G=[S() for i in range(h)]
V=[[0]*w for i in range(h)]
H=[[0]*w for i in range(h)]
c=0
for i in range(h):
    for j in range(w):
        if G[i][j]=="#":
            for k in range(c):
                H[i][j-k-1]=c
            c=0
        elif j==w-1:
            c+=1
            for k in range(c):
                H[i][j-k-1]=c
            c=0
        else:
            c+=1
            
for i in range(w):
    for j in range(h):
        if G[j][i]=="#":
            for k in range(c):
                V[j-k-1][i]=c
            c=0
        elif j==h-1:
            c+=1
            for k in range(c):
                V[j-k-1][i]=c
            c=0
        else:
            c+=1

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

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
S = [input() for i in range(H)]
P = [[0]*W for i in range(H)]
Q = [[0]*W for i in range(H)]
for i in range(H):
    p = -1
    for j in range(W):
        if S[i][j] == '#':
            c = j - p - 1
            for k in range(p+1, j):
                P[i][k] = c
            p = j
    c = W - p - 1
    for k in range(p+1, W):
        P[i][k] = c
for j in range(W):
    p = -1
    for i in range(H):
        if S[i][j] == '#':
            c = i - p - 1
            for k in range(p+1, i):
                Q[k][j] = c
            p = i
    c = H - p - 1
    for k in range(p+1, H):
        Q[k][j] = c
ans = 0
for i in range(H):
    for j in range(W):
        if S[i][j] != '#':
            ans = max(ans, P[i][j] + Q[i][j])
print(ans - 1)

Submission status: Accepted

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

for i in range(0, h):
    s[i] = input()

xs = [[0 for j in range(0, w)] for i in range(0, h)]
ys = [[0 for j in range(0, w)] for i in range(0, h)]

for i in range(0, h):
    l = 0
    r = 0
    c = 0
    for j in range(0, w + 1):
        if j < w and s[i][j] == ".":
            r = j
            c += 1
        else:
            for k in range(l, r + 1):
                xs[i][k] = c
            l = j
            r = j
            c = 0

for j in range(0, w):
    l = 0
    r = 0
    c = 0
    for i in range(0, h + 1):
        if i < h and s[i][j] == ".":
            r = i
            c += 1
        else:
            for k in range(l, r + 1):
                ys[k][j] = c
            l = i
            r = i
            c = 0

mx = 0

for i in range(0, h):
    for j in range(0, w):
        if s[i][j] == "." and xs[i][j] + ys[i][j] - 1 > mx:
            mx = xs[i][j] + ys[i][j] - 1
print(mx)

Submission status: Accepted

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

num = [[0]*w for i in range(h)]

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

    cnt = 0
    for j in range(w-1,-1,-1):
        if s[i][j] == '#':
            cnt = 0
        else:
            num[i][j] += cnt
            cnt += 1



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

res = -1
for i in range(h):
    for j in range(w):
        res = max(res,num[i][j])
print(res+1)

Submission status: Accepted

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


def solve(H: int, W: int, S: "List[str]"):
    c_h = [[0] * W for _ in range(H)]
    c_w = [[0] * W for _ in range(H)]
    for i in range(H):
        tmp = 0
        cnt = 0
        for j in range(W):
            if S[i][j] == '#':
                for k in range(tmp, j):
                    c_h[i][k] = cnt
                cnt = 0
                tmp = j + 1
            else:
                cnt += 1
        for k in range(tmp, W):
            c_h[i][k] = cnt
    for j in range(W):
        tmp = 0
        cnt = 0
        for i in range(H):
            if S[i][j] == '#':
                for k in range(tmp, i):
                    c_w[k][j] = cnt
                cnt = 0
                tmp = i + 1
            else:
                cnt += 1
        for k in range(tmp, H):
            c_w[k][j] = cnt
    ret = 0
    for i in range(H):
        for j in range(W):
            ret = max(ret, c_h[i][j] + c_w[i][j] - 1)
    print(ret)
    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:
import sys
input = sys.stdin.readline

H,W=map(int,input().split())
MAP=[input().strip() for i in range(H)]

Y=[[0]*W for i in range(H)]
T=[[0]*W for i in range(H)]

count=0

for i in range(H):
    count=0
    for j in range(W):
        if MAP[i][j]=="#":
            count=0
        else:
            count+=1
            Y[i][j]=count

for i in range(H):
    for j in range(W-2,-1,-1):
        if Y[i][j]==0:
            continue
        Y[i][j]=max(Y[i][j],Y[i][j+1])

for i in range(W):
    count=0
    for j in range(H):
        if MAP[j][i]=="#":
            count=0
        else:
            count+=1
            T[j][i]=count

for i in range(W):
    for j in range(H-2,-1,-1):
        if T[j][i]==0:
            continue
        T[j][i]=max(T[j][i],T[j+1][i])

ANS=0
for i in range(H):
    for j in range(W):
        ANS=max(ANS,Y[i][j]+T[i][j]-1)

print(ANS)

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
S = [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):
        l[i][j] = 0 if S[i][j] == '#' else 1 + (0 if j == 0 else l[i][j-1])
    for j in range(W-1, -1, -1):
        r[i][j] = 0 if S[i][j] == '#' else 1 + (0 if j == W-1 else r[i][j+1])
for j in range(W):
    for i in range(H):
        u[i][j] = 0 if S[i][j] == '#' else 1 + (0 if i == 0 else u[i-1][j])
    for i in range(H-1, -1, -1):
        d[i][j] = 0 if S[i][j] == '#' else 1 + (0 if i == H-1 else d[i+1][j])
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: Runtime Error

Solution code:
#!/usr/bin/env python
# coding: utf-8

# In[2]:


import numpy as np


# In[3]:


H, W = map(int, input().split())
S = np.ones((H, W), dtype=np.bool)
for i in range(H):
    S[i] = [s == "." for s in input()]


# In[4]:


up = np.zeros((H, W), dtype=np.int16)
down = np.zeros((H, W), dtype=np.int16)
left = np.zeros((H, W), dtype=np.int16)
right = np.zeros((H, W), dtype=np.int16)

# up
up[0, :] = S[0, :]
for h in range(1, H):
    up[h, :] = up[h - 1, :] + 1
    up[h, :] *= S[h, :]

# down
down[H - 1, :] = S[H - 1, :]
for h in reversed(range(0, H - 1)):
    down[h, :] = down[h + 1, :] + 1
    down[h, :] *= S[h, :]

# left
left[:, 0] = S[:, 0]
for w in range(1, W):
    left[:, w] = left[:, w - 1] + 1
    left[:, w] *= S[:, w]

# right
right[:, W - 1] = S[:, W - 1]
for w in reversed(range(0, W - 1)):
    right[:, w] = right[:, w + 1] + 1
    right[:, w] *= S[:, w]

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


# In[ ]:

Submission status: Runtime Error

Solution code:
h, w = map(int, input().split())
s = [input() for _ in range(h)]
di = [-1, 1, 0, 0]
dj = [0, 0, -1, 1]
res = 0
def dfs(i, j, vec_i, vec_j, footprint):
    global res 
    if i < 0 or i > h-1 or j < 0 or j > w-1 or s[i][j] == "#":
        res += (footprint-1)
        return 
    if vec_i == 0 and vec_j == 0:
        for k in range(4):
            next_i, next_j = i+di[k], j+dj[k]
            dfs(next_i, next_j, di[k], dj[k], 1)
    else:
        next_i, next_j = i+vec_i, j+vec_j
        if 0 <= i < h and 0 <= j < w and s[i][j] != "#":
            dfs(next_i, next_j, vec_i, vec_j, footprint+1)
        else:
            res += (footprint-1)
ans = float("-inf")
cand_i, cand_j = [], []
candi, candj = [], []
max_i, max_j = float("-inf"), float("-inf")
for i, v in enumerate(s):
    num = len([c for c in v if c == "."])
    max_i = max(max_i, num)
    cand_i.append((i, num))
cand_i.sort(key=lambda x:x[1], reverse=True)
for i, v in cand_i:
    if v != max_i:
        break
    candi.append(i)
for i, v in enumerate(zip(*s)):
    num = len([c for c in v if c == "."])
    max_j = max(max_j, num)
    cand_j.append((i, num))
cand_j.sort(key=lambda x:x[1], reverse=True)
for i, v in cand_j:
    if v != max_j:
        break
    candj.append(i)

for i in candi:
    for j in range(w):
        res = 0
        dfs(i, j, 0, 0, 0)
        ans = max(ans, res+1)
for j in candj:
    for i in range(h):
        res = 0
        dfs(i, j, 0, 0, 0)
        ans = max(ans, res+1)
print(ans)

Submission status: Runtime Error

Solution code:
h, w = map(int, input().split())
s = [input() for _ in range(h)]
di = [-1, 1, 0, 0]
dj = [0, 0, -1, 1]
res = 0
def dfs(i, j, vec_i, vec_j, footprint):
    global res 
    if i < 0 or i > h-1 or j < 0 or j > w-1 or s[i][j] == "#":
        res += (footprint-1)
        return 
    if vec_i == 0 and vec_j == 0:
        for k in range(4):
            next_i, next_j = i+di[k], j+dj[k]
            dfs(next_i, next_j, di[k], dj[k], 1)
    else:
        next_i, next_j = i+vec_i, j+vec_j
        if 0 <= i < h and 0 <= j < w and s[i][j] != "#":
            dfs(next_i, next_j, vec_i, vec_j, footprint+1)
        else:
            res += (footprint-1)
ans = float("-inf")
for i in range(h):
    for j in range(w):
        res = 0
        dfs(i, j, 0, 0, 0)
        ans = max(ans, res+1)
print(ans)

Submission status: Runtime Error

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

ans = 0
tmp = 0
def dfs(i,j,direc):
    global tmp
    if 0<=i<h and 0<=j<w and l[i][j] == ".":
        tmp += 1
    else:
        return
    if direc == 1:
        dfs(i+1,j,direc)
    if direc == 2:
        dfs(i,j+1,direc)
    if direc == 3:
        dfs(i-1,j,direc)
    if direc == 4:
        dfs(i,j-1,direc)
        
ans = 0
for i in range(h):
    for j in range(w):
        if l[i][j] == "#":
            continue
        else:
            tmp = 1
            dfs(i+1,j,1)
            dfs(i,j+1,2)
            dfs(i-1,j,3)
            dfs(i,j-1,4)
            ans = max(ans,tmp)
print(ans)

Submission status: Runtime Error

Solution code:
import psyco 
psyco.full()

n,m = map(int, input().split())

S = [[] for _ in range(n)]

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

ans = 0

for i in range(n):
    for j in range(m):
        cnt = 0
        if(S[i][j] == 'O'):
            for k in range(i,0,-1):
                if(S[k][j] == 'X'):
                    break
                cnt += 1
            
            for k in range(i+1,n):
                if(S[k][j] == 'X'):
                    break
                cnt += 1
            
            for k in range(j,0,-1):
                if(S[i][k] == 'X'):
                    break
                cnt += 1
            
            for k in range(j+1,m):
                if(S[i][k] == 'X'):
                    break
                cnt += 1
        ans = max(ans, cnt)

print(ans)

Submission status: Runtime Error

Solution code:
n,m = map(int, input().split())

S = [[] for _ in range(n)]

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

rows = [[] for _ in range(n)]
cols = [[] for _ in range(n)]

start = [[] for _ in range(n)]
end = [[] for _ in range(n)]

for i in range(n):
    rows[i].append(-1)
    for j in range(m):
        if(S[i][j] == '#'):
            rows[i].append(j)
    rows[i].append(m)

    l = 0
    r = 1
    cnt = 0

    for j in range(m):
        if(S[i][j] != '#'):
            start[i].append(rows[i][r] - rows[i][l])
        else:
            start[i].append(0)
        if(j == rows[i][r]):
            l = r
            r = r + 1
ans = 0
for i in range(m):
    cols[i].append(-1)

    for j in range(n):
        if(S[j][i] == '#'):
            cols[i].append(j)
    cols[i].append(n)

    l = 0
    r = 1
    cnt = 0

    for j in range(n):
        if(S[j][i] != '#'):
            end[j].append(cols[i][r] - cols[i][l])
            # ans = max(ans, start[j][i], end[j][i])
        else:
            end[j].append(0)
        if(j == cols[i][r]):
            l = r
            r = r + 1
        



for i in range(n):
    for j in range(m):
        # if(S[i][j] != 'X'):
        ans = max(ans, start[i][j] + end[i][j])

print(ans - 3)

Submission status: Runtime Error

Solution code:
from collections import deque

h, w = list(map(int, input().split()))

s = [list(input()) for _ in range(h)]

y = [[0] * w for _ in range(h)]
q = deque([])
c = 0

for i in range(h):
    for j in range(w):
        if s[i][j] == ".":
            q.append((i, j))
            c += 1
        
        if s[i][j] == "#" or j == w-1:
            while len(q) > 0:
                x, y = q.pop()
                y[x][y] = c
            c = 0



m = 0

c = 0
for j in range(w):
    for i in range(h):
        if s[i][j] == ".":
            q.append((i, j))
            c += 1

        if s[i][j] == "#" or i == h-1:
            while len(q) > 0:
                x, y = q.pop()
                m = max(m, y[x][y] + c - 1)
            c = 0
print(m)

Submission status: Runtime Error

Solution code:
from itertools import *
from collections import *
h,w=map(int,input().split())
s=[[False]*w for i in range(h)]
for i in range(h):
    t=list(input())
    for j in range(w):
        
        if t[j]==".":
            s[i][j]=True
def dp(i,j):
    if s[i][j]:
        return l(i,j)+r(i,j)+d(i,j)+u(i,j)-6
    else:
        return 0
def l(i,j):
    if s[i][j]:
        if j==0:
            return 1
        else:
            return l(i,j-1)+1
    else:
        return 0
def r(i,j):
    if s[i][j]:
        if j==w-1:
            return 1
        else:
            return l(i,j+1)+1
    else:
        return 0
def d(i,j):
    if s[i][j]:
        if i==h-1:
            return 1
        else:
            return d(i+1,j)+1
    else:
        return 0
def u(i,j):
    if s[i][j]:
        if i==0:
            return 1
        else:
            return u(i-1,j)+1
    else:
        return 0
ans=0
for i in range(h):
    for j in range(w):
        if dp(i,j)>ans:
            ans=dp(i,j)
print(ans)

Submission status: Runtime Error

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

h,w=map(int,input().split())
s=[[] for i in range(h)]
for i in range(h):
    si=input()
    s[i]=si
"""
h,w=4,6
s=[['#', '.', '.', '#', '.', '.'], ['.', '.', '.', '.', '.', '#'], ['.', '.', '.', '.', '#', '.'], ['#', '.', '#', '.', '.', '.']]    
"""
ww=[[0]*w for i in range(h)]
#www=[[0]*w for i in range(h)]
www=[0]*w
for i in range(h):
    jsum=0
    jcnt=1
    jj=[0]*w
    for j in range(w):
        if s[i][j]==".":
#            www[i][j]=jcnt
            www[j]=jcnt
            if j<w-1 and s[i][j+1]=="#":
                jcnt+=1
            jsum+=1
        else:
#            print(jsum,jst,jed)
            if jsum!=0:
                jj[jcnt-1]=jsum
            jsum=0
    jcnt+=1
    jj[jcnt-1]=jsum
            
    for j in range(w):
        if s[i][j]==".":
#            ww[i][j]=jj[www[i][j]]
            ww[i][j]=jj[www[j]]
#    print(jj)
                
hh=[[0]*w for i in range(h)]
hhh=[0]*h
for j in range(w):
    isum=0
    icnt=1
    ii=[0]*h
    for i in range(h):
        if s[i][j]==".":
#            www[i][j]=jcnt
            hhh[i]=icnt
            if i<h-1 and s[i+1][j]=="#":
                icnt+=1
            isum+=1
        else:
#            print(jsum,jst,jed)
            if isum!=0:
                ii[icnt-1]=isum
            isum=0
    icnt+=1
    ii[icnt-1]=isum
            
    for i in range(h):
        if s[i][j]==".":
#            ww[i][j]=jj[www[i][j]]
            hh[i][j]=ii[hhh[i]]
#    print(jj)
            
ijmax=0
for i in range(h):
    for j in range(w):
        if s[i][j]==".":
            ijmax=max(ijmax,ww[i][j]+hh[i][j]-1)

print(ijmax)