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:
def solve(string):
    h, w, *s = string.split()
    h, w = map(int, [h, w])
    hg = []
    hi = []
    vg = []
    vi = []

    for _s in s:
        gn = 0
        _hi = [0] * w
        _hg = []
        c = 0
        for i, __s in enumerate(_s):
            if __s == "#":
                _hi[i] = -1
                gn += 1
                _hg.append(c)
                c = 0
                continue
            _hi[i] = gn
            c += 1
        _hg.append(c)
        hg.append(_hg)
        hi.append(_hi)
    for _s in zip(*s):
        gn = 0
        _vi = [0] * h
        _vg = []
        c = 0
        for i, __s in enumerate(_s):
            if __s == "#":
                _vi[i] = -1
                gn += 1
                _vg.append(c)
                c = 0
                continue
            _vi[i] = gn
            c += 1
        _vg.append(c)
        vg.append(_vg)
        vi.append(_vi)
    ans = 0
    for i in range(h):
        ans = max(ans, max([hg[i][hi[i][j]] + vg[j][vi[j][i]] for j in range(w)]))
    return str(ans - 1)


if __name__ == '__main__':
    n, m = map(int, input().split())
    print(solve('{} {}\n'.format(n, m) + '\n'.join([input() for _ in range(n)])))

Submission status: Accepted

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

def calc(s):
    res = []
    for v in s:
        v = v + '-'
        tmp = []
        nc = v[0]
        nn = 0
        for c in v:
            if nc == c:
                nn += 1
                continue
            else:
                if nc == '#':
                    tmp.extend([0 for _ in range(nn)])
                else:
                    tmp.extend( [nn for _ in range(nn)])
                nc = c
                nn = 1
        res.append(tmp)
    return res

def calc2(s):
    res = []
    for j in range(w):
        tmp = []
        nc = s[0][j]
        nn = 0
        for i in range(h):
            c = s[i][j]
            if nc == c:
                nn += 1
                continue
            else:
                if nc == '#':
                    tmp.extend([0 for _ in range(nn)])
                else:
                    tmp.extend( [nn for _ in range(nn)])
                nc = c
                nn = 1
        if nc == '#':
            tmp.extend([0 for _ in range(nn)])
        else:
            tmp.extend( [nn for _ in range(nn)])
        res.append(tmp)
    return res

tate = calc2(S)
yoko = calc(S)

print(max([tate[j][i] + yoko[i][j] for i in range(h) for j in range(w) if tate[j][i] * yoko[i][j] != 0]) - 1)

Submission status: Accepted

Solution code:
L = []

H, W = map(int, input().split())
for i in range(H):
	S = list(input())
	L.append(S)

ans1 = [[0 for i in range(W)] for j in range(H)]
ans2 = [[0 for i in range(W)] for j in range(H)]

for i in range(H):
	val = 0
	l = []
	for j in range(W):
		if L[i][j] == "#":
			for k in l:
				ans1[i][k] = val
			val = 0
			l = []
		else:
			val += 1
			l.append(j)
	for k in l:
		ans1[i][k] = val

for i in range(W):
	val = 0
	l = []
	for j in range(H):
		if L[j][i] == "#":
			for k in l:
				ans2[k][i] = val
			val = 0
			l = []
		else:
			val += 1
			l.append(j)
	for k in l:
		ans2[k][i] = val


ans = -1
for i in range(H):
	for j in range(W):
		x = ans1[i][j] + ans2[i][j] - 1
		if x > ans:
			ans = x

print(ans)

Submission status: Accepted

Solution code:
# ABC129 D
H, W = map(int, input().split(' '))

H += 2
W += 2
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)]

grid = []
grid.append(list(('#'*W).strip()))
for i in range(H-2):
    grid.append(list(('#'+input()+'#').strip()))
grid.append(list(('#'*W).strip()))

for i in range(1, H-1):
    for j in range(1, W-1):
        if grid[i][j] == '.':
            L[i][j] = L[i][j-1] + 1
        if grid[i][W - j-1]  == '.':
            R[i][W - j-1] = R[i][W - j-1 + 1] + 1

for j in range(1, W-1):
    for i in range(1, H-1):
        if grid[i][j] == '.':
            U[i][j] = U[i-1][j] + 1
        if grid[H - i-1][j]  == '.':
            D[H - i-1][j] = D[H - i-1 + 1][j] + 1

total = [[0 for _ in range(W)] for _ in range(H)]
for i in range(1, H-1):
    for j in range(1,W-1):
        total[i][j] = L[i][j] + R[i][j] + U[i][j] + D[i][j] - 3

ans = 0
for i in range(1,H-1):
    ans = max(ans, max(total[i]))
print (ans)

Submission status: Accepted

Solution code:
import numpy as np


H, W = map(int, input().split())
s = np.array([[c for c in input()] for i in range(H)]) == '.'
s_t = s.transpose()

dp_l = np.zeros((W, H), dtype=int)
dp_r = np.zeros((W, H), dtype=int)
dp_u = np.zeros((H, W), dtype=int)
dp_d = np.zeros((H, W), dtype=int)

dp_l[0] = s_t[0]
dp_r[W-1] = s_t[W-1]
dp_u[0] = s[0]
dp_d[H-1] = s[H-1]

for i in range(1, W):
    dp_l[i] = s_t[i] * (dp_l[i-1] + 1)

for i in reversed(range(W-1)):
    dp_r[i] = s_t[i] * (dp_r[i+1] + 1)

for i in range(1, H):
    dp_u[i] = s[i] * (dp_u[i-1] + 1)

for i in reversed(range(H-1)):
    dp_d[i] = s[i] * (dp_d[i+1] + 1)

cnts = dp_l + dp_r + dp_d.transpose() + dp_u.transpose()
print(max(np.amax(cnts, 0)) - 3)

Submission status: Accepted

Solution code:
h,w = map(int,input().split())
s = []
for i in range(h):
    s1 = list(input())
    s.append(s1)
p = []
q = []
r = []
t = []
l = 0
for i in range(h):
    count1 = 0
    count2 = 0
    p1 = []
    q1 = []
    for j in range(w):
        if s[i][j] == ".":
            count1 = count1 + 1
            p1.append(count1)
        elif s[i][j] == "#":
            count1 = 0
            p1.append(count1)
        if s[i][w-j-1] == ".":
            count2 = count2 + 1
            q1.append(count2)
        elif s[i][w-j-1] == "#":
            count2 = 0
            q1.append(count2)
    p.append(p1)
    q.append(q1)
for i in range(w):
    count1 = 0
    count2 = 0
    r1 = []
    t1 = []
    for j in range(h):
        if s[j][i] == ".":
            count1 = count1 + 1
            r1.append(count1)
        elif s[j][i] == "#":
            count1 = 0
            r1.append(count1)
        if s[h-j-1][i] == ".":
            count2 = count2 + 1
            t1.append(count2)
        elif s[h-j-1][i] == "#":
            count2 = 0
            t1.append(count2)
    r.append(r1)
    t.append(t1)
for i in range(h):
    for j in range(w):
        l1 = p[i][j]+q[i][w-j-1]+r[j][i]+t[j][h-i-1]
        if l < l1:
            l = l1
print(l-3)

Submission status: Accepted

Solution code:
from functools import reduce
import sys
import numpy as np
MOD = 1000000007

buffer = sys.stdin.buffer
H, W = list(map(int,buffer.readline().split()))
S = np.frombuffer(buffer.read(H*(W+1)), dtype="B") == ord(".")
S = S.reshape((H,W+1))[:,:-1]
L = np.empty((H,W), dtype="i4")
R = np.empty((H,W), dtype="i4")
U = np.empty((H,W), dtype="i4")
D = np.empty((H,W), dtype="i4")
 
L[:,0] = S[:,0]
for i in range(1,W):
    L[:,i] = (L[:,i-1] + 1) * S[:,i]
 
R[:,-1] = S[:,-1]
for i in range(1,W):
    R[:,-1-i] = (R[:,-i] + 1) * S[:,-1-i]
 
U[0] = S[0]
for i in range(1,H):
    U[i] = (U[i-1] + 1) * S[i]
 
D[-1] = S[-1]
for i in range(1,H):
    D[-1-i] = (D[-i] + 1) * S[-1-i]
 
score = L + R + U + D - 3
print(int(score.max()))

Submission status: Accepted

Solution code:
import sys, math, heapq
stdin = sys.stdin
read_int = lambda : list(map(int,stdin.readline().split()))
H, W = read_int()


# nomal
# 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)]
res = [[0 for _ in range(W)] for _ in range(H)]
grid = []

# numpy
import numpy as np
L, R, U, D = [np.zeros((H, W), int) for _ in range(4)]
grid_np = np.zeros((H,W), int)


for i in range(H):
    row = list(" ".join(input()).split())
    grid.append(row)
    grid_np[i] = np.array(row) == '.'

def solve():
    ans = 0
    for i in range(H):
        for j in range(W):
            if grid[i][j] == ".":
                L[i][j] = 1 if j == 0 else L[i][j - 1] + 1
                U[i][j] = 1 if i == 0 else U[i - 1][j] + 1
                res[i][j] += L[i][j] + U[i][j]
                ans = max(res[i][j], ans)

            if grid[H - i - 1][W - j - 1] == ".":
                R[H - i - 1][W - j - 1] = 1 if j == 0 else R[H - i - 1][W - j] + 1
                D[H - i - 1][W - j - 1] = 1 if i == 0 else D[H - i][W - j - 1] + 1
                res[H - i - 1][W - j - 1] += R[H - i - 1][W - j - 1] + D[H - i - 1][W - j - 1]
                ans = max(res[H - i - 1][W - j - 1], ans)
    return ans - 3

def solve_np():
    ans = 0

    for j in range(W):
        # 本当は意味ない条件分岐
        if j == 0:
            L[:, j] = 1 * grid_np[:, j]
            R[:, -j - 1] = 1 * grid_np[:, -j - 1]
        else:
            L[:, j] = (L[:, j - 1] + 1) * grid_np[:, j]
            R[:, -j - 1] = (R[:, -j] + 1) * grid_np[:, -j - 1]

    for i in range(H):
        if i == 0:
            U[i] = 1 * grid_np[i]
            D[-i -1] = 1 * grid_np[-i -1]
        else:
            U[i] = (U[i - 1] + 1) * grid_np[i]
            D[-i - 1] = (D[-i] + 1) * grid_np[-i - 1]
    ans = np.max((L+R+U+D) -3)
    return ans

if __name__ == "__main__":
    # print(solve())
    print(solve_np())

# if __name__ == "__main__":
#     import timeit
#     loop = 1
#     n = 1000
#     time = timeit.timeit(lambda: solve(), number=loop)
#     print(time/loop)

Submission status: Accepted

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

L = [[-1] * W for _ in range(H)]
R = [[-1] * W for _ in range(H)]
D = [[-1] * W for _ in range(H)]
U = [[-1] * W for _ in range(H)]

for r in range(H):
    for c in range(W):
        if s[r][c] == '.':
            if c == 0:
                L[r][c] = 1
            else:
                L[r][c] = L[r][c - 1] + 1
        else:
            L[r][c] = 0

for r in range(H):
    for c in reversed(range(W)):
        if s[r][c] == '.':
            if c == W - 1:
                R[r][c] = 1
            else:
                R[r][c] = R[r][c + 1] + 1
        else:
            R[r][c] = 0

for r in range(H):
    for c in range(W):
        if s[r][c] == '.':
            if r == 0:
                U[r][c] = 1
            else:
                U[r][c] = U[r - 1][c] + 1
        else:
            U[r][c] = 0

for r in reversed(range(H)):
    for c in range(W):
        if s[r][c] == '.':
            if r == H - 1:
                D[r][c] = 1
            else:
                D[r][c] = D[r + 1][c] + 1
        else:
            D[r][c] = 0

ans = 0
for r in range(H):
    for c in range(W):
        ans = max(ans, L[r][c] + R[r][c] + D[r][c] + U[r][c] - 3)
print(ans)

Submission status: Accepted

Solution code:
#ABC129D-Lamp.py

#import list
import sys
import numpy as np

#main function
def main():
    buffer = sys.stdin.buffer
    H, W = list(map(int,buffer.readline().split()))
    S = np.frombuffer(buffer.read(H*(W+1)), dtype="B") == ord(".")
    S = S.reshape((H,W+1))[:,:-1]
    L = np.empty((H,W), dtype="i4")
    R = np.empty((H,W), dtype="i4")
    U = np.empty((H,W), dtype="i4")
    D = np.empty((H,W), dtype="i4")

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

    R[:,-1] = S[:,-1]
    for i in range(1,W):
        R[:,-1-i] = (R[:,-i] + 1) * S[:,-1-i]

    U[0] = S[0]
    for i in range(1,H):
        U[i] = (U[i-1] + 1) * S[i]

    D[-1] = S[-1]
    for i in range(1,H):
        D[-1-i] = (D[-i] + 1) * S[-1-i]

    score = L + R + U + D - 3
    print(int(score.max()))


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
#ref. https://atcoder.jp/contests/abc129/submissions/5906191
import numpy as np
import sys


sin = sys.stdin.buffer
H, W = map(int, sin.readline().split())
grid = [[None] * W for _ in range(H)]
for h in range(H):
    grid[h] = list(input())
grid = (np.array(grid) == ".") * 1

L = np.empty((H, W), dtype="i4")
R = np.empty((H, W), dtype="i4")
T = np.empty((H, W), dtype="i4")
B = np.empty((H, W), dtype="i4")

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

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

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

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

ans = (T + B + L + R - 3).max()
print(ans)

Submission status: Accepted

Solution code:
#ref. https://atcoder.jp/contests/abc129/submissions/5906191
import numpy as np
import sys

def main():
    sin = sys.stdin.buffer
    H, W = map(int, sin.readline().split())
    grid = np.frombuffer(sin.read(H * (W + 1)), dtype="B") == ord(".")
    grid = grid.reshape((H, W + 1))[:, :-1]

    L = np.empty((H, W), dtype="i4")
    R = np.empty((H, W), dtype="i4")
    T = np.empty((H, W), dtype="i4")
    B = np.empty((H, W), dtype="i4")

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

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

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

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

    ans = (T + B + L + R - 3).max()
    print(ans)

main()

Submission status: Accepted

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

def main():
    h, w = map(int, input().split())
    ss = [input() for _ in range(h)]

    matl = [[0 for _ in range(w)] for _ in range(h)]
    matr = [[0 for _ in range(w)] for _ in range(h)]
    matu = [[0 for _ in range(w)] for _ in range(h)]
    matd = [[0 for _ in range(w)] for _ in range(h)]

    for j in range(h):
        for i in range(w):
            if ss[j][i] == '#':
                matd[j][i] = 0
            elif j == 0:
                matd[j][i] = 1
            else:
                matd[j][i] = matd[j - 1][i] + 1

            if ss[h - j - 1][i] == '#':
                matu[h - j - 1][i] = 0
            elif h - j == h:
                matu[h - j - 1][i] = 1
            else:
                matu[h - j - 1][i] = matu[h - j][i] + 1

            if ss[j][i] == '#':
                matr[j][i] = 0
            elif i == 0:
                matr[j][i] = 1
            else:
                matr[j][i] = matr[j][i - 1] + 1

            if ss[j][w - i - 1] == '#':
                matl[j][w - i - 1] = 0
            elif w - i == w:
                matl[j][w - i - 1] = 1
            else:
                matl[j][w - i - 1] = matl[j][w - i] + 1

    maxv = 0
    for j in range(h):
        for i in range(w):
            tmp = matu[j][i] + matd[j][i] + matr[j][i] + matl[j][i]
            if tmp > maxv:
                maxv = tmp
    print(maxv - 3)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys

readline = sys.stdin.readline

H, W = map(int, readline().split())
S = [readline().rstrip() for i in range(H)]

vert = [[0]*W for i in range(H)]
hori = [[0]*W for i in range(H)]

for y in range(H):
    for x in range(W):

        if x > 0 and S[y][x - 1] != '#':
            hori[y][x] = hori[y][x - 1]
        elif S[y][x] == '#':
            hori[y][x] = 0
        else:
            # hori
            count = 1
            while x + count < W and S[y][x + count] == '.':
                count += 1
            hori[y][x] = count

        if y > 0 and S[y - 1][x] != '#':
            vert[y][x] = vert[y - 1][x]
        elif S[y][x] == '#':
            vert[y][x] = 0
        else:
            # hori
            count = 1
            while y + count < H and S[y + count][x] == '.':
                count += 1
            vert[y][x] = count

ans = 0
for y in range(H):
    for x in range(W):
        a = hori[y][x] + vert[y][x] - 1
        ans = max(a, ans)

print(ans)

Submission status: Accepted

Solution code:
h,w = map(int, input().split())
S = [input() for i in range(h)]
U = [[0]*w for i in range(h)]
D = [[0]*w for i in range(h)]
L = [[0]*w for i in range(h)]
R = [[0]*w for i in range(h)]

for i in range(h):
    for j in range(w):
        if S[i][j] == ".":
            if j > 0:
                L[i][j] = L[i][j-1] + 1
            else:
                L[i][j] = 1
    for j in range(w-1,-1,-1):
        if S[i][j] == ".":
            if j < w-1:
                R[i][j] = R[i][j+1] + 1
            else:
                R[i][j] = 1
for i in range(w):
    for j in range(h):
        if S[j][i] == ".":
            if j > 0:
                D[j][i] = D[j-1][i] + 1
            else:
                D[j][i] = 1
    for j in range(h-1,-1,-1):
        if S[j][i] == ".":
            if j < h-1:
                U[j][i] = U[j+1][i] + 1
            else:
                U[j][i] = 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)

Submission status: Accepted

Solution code:
h,w = map(int,input().split())
S = [input() for i in range(h)]
yoko = [[1]*w for _ in range(h)]
tate = [[1]*w for _ in range(h)]
for i in range(h):
    for j in range(w):
        if S[i][j] == '#':
            yoko[i][j] = 0
        else:
            if j != 0:
                yoko[i][j] += yoko[i][j-1]
for i in range(h):
    for j in range(w-1,-1,-1):
        if yoko[i][j] == 0:
            continue
        else:
            if j != w-1:
                yoko[i][j] = max(yoko[i][j],yoko[i][j+1])
for j in range(w):
    for i in range(h):
        if S[i][j] == '#':
            tate[i][j] = 0
        else:
            if i != 0:
                tate[i][j] += tate[i-1][j]
for j in range(w):
    for i in range(h-1,-1,-1):
        if tate[i][j] == 0:
            continue
        else:
            if i != h-1:
                tate[i][j] = max(tate[i][j],tate[i+1][j])
res = 0
for i in range(h):
    for j in range(w):
        res = max(res,tate[i][j]+yoko[i][j]-1)
print(res)

Submission status: Accepted

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

tate_value = [[0]*W for i in range(H)]
yoko_value = [[0]*W for i in range(H)]

for i in range(H):
  for j in range(W):
    if S[i][j] == '#':
      yoko_value[i][j] = 0
    else:
      if j==0:
        left = 0
        right = W-1
        for k in range(0,W):
          if S[i][k] == '#':
            right = k-1
            break
        yoko_value[i][j] = right+1
      else:
        if S[i][j-1] == '.':
          yoko_value[i][j] = yoko_value[i][j-1]
        else:
          right = W-1
          for k in range(j,W):
            if S[i][k] == '#':
              right = k-1
              break
          yoko_value[i][j] = right-j+1

for j in range(W):
  for i in range(H):
    if S[i][j] == '#':
      tate_value[i][j] = 0
    else:
      if i==0:
        up = 0
        down = H-1
        for k in range(0,H):
          if S[k][j] == '#':
            down = k-1
            break
        tate_value[i][j] = down+1
      else:
        if S[i-1][j] == '.':
          tate_value[i][j] = tate_value[i-1][j]
        else:
          down = H-1
          for k in range(i,H):
            if S[k][j] == '#':
              down = k-1
              break
          tate_value[i][j] = down-i+1
          
max = 0
for i in range(H):
  for j in range(W):
    temp = yoko_value[i][j] + tate_value[i][j]-1
    if temp>max:
      max = temp
      
print(max)

Submission status: Accepted

Solution code:
import numpy as np
 
h, w = map(int, input().split())
 
up = np.zeros((h, w), dtype=int)
down = np.zeros((h, w), dtype=int)
right = np.zeros((h, w), dtype=int)
left = np.zeros((h, w), dtype=int)
 
map = np.array([list(input()) for i in range(h)]) == "."
 
for i in range(1, h):
    up[i] = (up[i-1] + 1) * map[i-1]
    down[h-1-i] = (down[h-i] + 1) * map[h-i]
for j in range(1, w):
    left[:,j] = (left[:,j-1] + 1) * map[:,j-1]
    right[:,w-1-j] = (right[:,w-j] + 1) * map[:,w-j]

print(((up+down+right+left+1) * map).max())

Submission status: Accepted

Solution code:
H, W = [int(x) for x in input().split()]
S_list = [input() for _ in range(H)]
cnt = [[0] * W for _ in range(H)]

for i in range(H):
    done = [0] * W
    for j in range(W):
        if (S_list[i][j] == '#'): continue
        elif (done[j]): continue
        else:
            # .がいくつ続くか
            k = 0
            while (j+k < W):
                if (S_list[i][j+k] == '#'): break
                k += 1
            # k個続いてるのでkを加算
            for l in range(k):
                cnt[i][j+l] += k
                done[j+l] = 1
for i in range(W):
    done = [0] * H
    for j in range(H):
        if (S_list[j][i] == '#'): continue
        elif (done[j]): continue
        else:
            # .がいくつ続くか
            k = 0
            while (j+k < H):
                if (S_list[j+k][i] == '#'): break
                k += 1
            # k個続いてるのでkを加算
            for l in range(k):
                cnt[j+l][i] += k
                done[j+l] = 1
print(max(map(max, cnt))-1)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sat Apr 20 20:47:47 2019

@author: Owner
"""

H, W = list(map(int, input().split()))
# 多次元配列の宣言（あとでintにすること。）（タプルにすること。）
A = [""]*H 
#INF = np.Inf
INF = 2000

#D = np.zeros((H+1, W+1))
D = [[0]*(W+1) for h in range(H+1)]

for h in range(H):
    A[h] = input()
    for w in range(W):
        if A[h][w] == '.':
            D[h][w] = 1           

def main(): 
    #G = np.zeros((H+1, W+1))
    #T = np.zeros((H+1, W+1)) # 自分以外横に何マスつながっているか
    #Y = np.zeros((H+1, W+1)) # 自分以外縦に何マスつながっているか
    T = [[0]*(W+1) for h in range(H+1)]
    Y = [[0]*(W+1) for h in range(H+1)]
    
    for h in range(H):
        cnt = 0
        for w in range(W):
            if D[h][w] == 0:
                cnt = 0
                continue
            else:
                Y[h][w] += cnt
                cnt+=1    
    for h in range(H):
        for w in reversed(range(W)):
            if D[h][w] == 0:
               continue
            else:
               Y[h][w] = max(Y[h][w], Y[h][w+1])                           
            
    for w in range(W):
        cnt = 0
        for h in range(H):
            if D[h][w] == 0:
                cnt = 0
                continue
            else:
                T[h][w] += cnt
                cnt+=1
    for w in range(W):
        for h in reversed(range(H)):
            if D[h][w] == 0:
                continue
            else:
                T[h][w] = max(T[h][w], T[h+1][w])
    #G = D+Y+T
    #res = np.amax(G)
    res = 0
    for w in range(W):
        for h in range(H):
            res = max(res, D[h][w]+Y[h][w]+T[h][w])
    print(int(res))
            
            
                

    
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import numpy as np
from pprint import pprint


def solve(MM, H, W):

    L, R, U, D = np.zeros((H, W), dtype=int), np.zeros((H, W), dtype=int), np.zeros((H, W), dtype=int), np.zeros((H, W), dtype=int)

    for i in range(H):
        i_r = H - 1 - i

        # L, U
        for j in range(W):
            j_r = W - 1 - j

            # L
            if MM[i][j]:
                if j == 0:
                    L[i][j] = 1
                else:
                    L[i][j] = L[i][j - 1] + 1

            # R
            if MM[i][j_r]:
                if j_r == W - 1:
                    R[i][j_r] = 1
                else:
                    R[i][j_r] = R[i][j_r + 1] + 1

            # U
            if MM[i][j]:
                if i == 0:
                    U[i][j] = 1
                else:
                    U[i][j] = U[i - 1][j] + 1

            # D
            if MM[i_r][j]:
                if i_r == H - 1:
                    D[i_r][j] = 1
                else:
                    D[i_r][j] = D[i_r + 1][j] + 1

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


def solve2(MM, H, W):
    L, R, U, D = [MM.copy() for _ in range(4)]

    # U, D
    for i in range(1, H):
        U[i] = (U[i - 1] + 1) * MM[i]
        i_r = H - 1 - i
        D[i_r] = (D[i_r + 1] + 1) * MM[i_r]

    MM_T = MM.T
    L_T, R_T = L.T, R.T

    # L, R
    for i in range(1, W):
        L_T[i] = (L_T[i - 1] + 1) * MM_T[i]
        i_r = W - 1 - i
        R_T[i_r] = (R_T[i_r + 1] + 1) * MM_T[i_r]

    L, R = L_T.T, R_T.T
    print((L + R + U + D).max() - 3)


def stdin():
    H, W = [int(x) for x in input().split()]
    MM = np.zeros((H, W), dtype=int)

    for i in range(H):
        line = str(input())

        for j in range(W):
            if line[j] != '#':
                MM[i][j] = 1

    solve2(MM, H, W)


if __name__ == '__main__':
    stdin()

Submission status: Accepted

Solution code:
import sys

import numpy as np


def main():
    sin = sys.stdin.buffer
    H, W = map(int, sin.readline().split())
    a = np.frombuffer(sin.read(H * (W + 1)), dtype="B") == ord(".")
    a = a.reshape((H, W + 1))[:, :-1]

    L = np.empty((H, W), dtype="i4")
    R = np.empty((H, W), dtype="i4")
    T = np.empty((H, W), dtype="i4")
    B = np.empty((H, W), dtype="i4")
    T[0] = a[0]
    for i in range(1, H):
        T[i] = (T[i - 1] + 1) * a[i]
    B[-1] = a[-1]
    for i in range(H - 2, -1, -1):
        B[i] = (B[i + 1] + 1) * a[i]
    L[:, 0] = a[:, 0]
    for i in range(1, W):
        L[:, i] = (L[:, i - 1] + 1) * a[:, i]
    R[:, -1] = a[:, -1]
    for i in range(W - 2, -1, -1):
        R[:, i] = (R[:, i + 1] + 1) * a[:, i]

    print((T + B + L + R - 3).max())


main()

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
s = [input() for _ in range(h)]
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(h):
    for j in range(w):
        if s[i][j] == "#":
            u[i + 1][j + 1] = 0
        else:
            u[i + 1][j + 1] = u[i][j + 1] + 1

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

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

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

ans = 0
for i in range(1, h + 1):
    for j in range(1, w + 1):
        num = u[i][j] + d[i][j] + l[i][j] + r[i][j] - 3
        ans = max(ans, num)
print(ans)

Submission status: Accepted

Solution code:
#D

h,w = (int(x) for x in input().split())

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

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

#横
for i in range(h):
    mi = -1
    count = 0
    for j in range(w):
        this_s = s[i][j]
        
        if this_s == ".":
            count += 1
            if mi == -1:
                mi = j
        if this_s == "#" and not(mi == -1):
            y[i][mi:j] = [count] * (j-mi)
            mi = -1
            count = 0
    else:
        if not(mi == -1):
            y[i][mi:w] = [count] * (w-mi)
#print(y)
        
#縦
for j in range(w):
    mi = -1
    count = 0
    for i in range(h):
        this_s = s[i][j]
        
        if this_s == ".":
            count += 1
            if mi == -1:
                mi = i
        if this_s == "#" and not(mi == -1):
            for k in range(mi,i):
                t[k][j] = count
            mi = -1
            count = 0
    else:
        if not(mi == -1):
            #t[mi:h][j] = [count] * (h-mi)
            for k in range(mi,h):
                t[k][j] = count
#print(t)

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

Submission status: Accepted

Solution code:
import numpy as np
import sys


def main():
    sin = sys.stdin.buffer
    H, W = map(int, sin.readline().split())
    a = np.frombuffer(sin.read(H * (W + 1)), dtype='B') == ord('.')
    a = a.reshape((H, W + 1))[:, :-1]

    L = np.empty((H, W), dtype='i4')
    R = np.empty((H, W), dtype='i4')
    T = np.empty((H, W), dtype='i4')
    B = np.empty((H, W), dtype='i4')
    T[0] = a[0]
    for i in range(1, H):
        T[i] = (T[i - 1] + 1) * a[i]
    B[-1] = a[-1]
    for i in range(H - 2, -1, -1):
        B[i] = (B[i + 1] + 1) * a[i]
    L[:, 0] = a[:, 0]
    for i in range(1, W):
        L[:, i] = (L[:, i - 1] + 1) * a[:, i]
    R[:, -1] = a[:, -1]
    for i in range(W - 2, -1, -1):
        R[:, i] = (R[:, i + 1] + 1) * a[:, i]

    print((T + B + L + R - 3).max())


main()

Submission status: Accepted

Solution code:
import numpy as np
import sys


def main():
    sin = sys.stdin.buffer
    H, W = map(int, sin.readline().split())
    a = np.frombuffer(sin.read(H * (W + 1)), dtype='B') == ord('.')
    a = a.reshape((H, W + 1))
    L = np.empty((H, W + 1), dtype='i4')
    R = np.empty((H, W + 1), dtype='i4')
    T = np.empty((H, W + 1), dtype='i4')
    B = np.empty((H, W + 1), dtype='i4')

    T[0] = a[0]
    for i in range(1, H):
        T[i] = (T[i - 1] + 1) * a[i]
    B[H - 1] = a[H - 1]
    for i in range(H - 2, -1, -1):
        B[i] = (B[i + 1] + 1) * a[i]
    L[:, 0] = a[:, 0]
    for i in range(1, W + 1):
        L[:, i] = (L[:, i - 1] + 1) * a[:, i]
    R[:, W] = a[:, W]
    for i in range(W - 1, -1, -1):
        R[:, i] = (R[:, i + 1] + 1) * a[:, i]

    print((T + B + L + R - 3).max())


main()