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())
S = [list(input()) for _ in range(h)]
L = [[0] * w for _ in range(h)]
for i in range(h):
    for j in range(w):
        # if S[i][j] == '#':
        #     L[i][j] = 0
        if S[i][j] == '.':
            if j == 0:
                L[i][j] = 1
            else:
                L[i][j] = L[i][j-1] + 1
R = [[0] * w for _ in range(h)]
for i in range(h):
    for j in reversed(range(w)):
        # if S[i][j] == '#':
        #     R[i][j] = 0
        if S[i][j] == '.':
            if j == w-1:
                R[i][j] = 1
            else:
                R[i][j] = R[i][j+1] + 1
U = [[0] * w for _ in range(h)]
for j in range(w):
    for i in range(h):
        # if S[i][j] == '#':
        #     U[i][j] = 0
        if S[i][j] == '.':
            if i == 0:
                U[i][j] = 1
            else:
                U[i][j] = U[i-1][j] + 1
D = [[0] * w for _ in range(h)]
for j in range(w):
    for i in reversed(range(h)):
        # if S[i][j] == '#':
        #     D[i][j] = 0
        if S[i][j] == '.':
            if 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:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from functools import reduce



INF = float('inf')
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline().strip()
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007


h, w = LI()
L = SR(h)
u = [[0]*w for _ in range(h)]
d = [[0]*w for _ in range(h)]
l = [[0]*w for _ in range(h)]
r = [[0]*w for _ in range(h)]
for y in range(h):
    l[y][0] = 1 if L[y][0] == '.' else 0
    for x in range(1, w):
        if L[y][x] == '.':
            l[y][x] = l[y][x-1] + 1



for y in range(h):
    r[y][w-1] = 1 if L[y][w-1] == '.' else 0
    for x in range(w-2, -1, -1):
        if L[y][x] == '.':
            r[y][x] = r[y][x+1] + 1



for x in range(w):
    u[0][x] = 1 if L[0][x] == '.' else 0
    for y in range(1, h):
        if L[y][x] == '.':
            u[y][x] = u[y-1][x] + 1


for x in range(w):
    d[h-1][x] = 1 if L[h-1][x] == '.' else 0
    for y in range(h-2, -1, -1):
        if L[y][x] == '.':
            d[y][x] = d[y+1][x] + 1



print(max([u[y][x] + d[y][x] + l[y][x] + r[y][x] for y in range(h) for x in range(w)]) - 3)

Submission status: Accepted

Solution code:
# ABC129D - Lamp
def main():
    H, W, *S = open(0).read().split()
    H, W = int(H), int(W)
    L, R = [[0] * W for _ in range(H)], [[0] * W for _ in range(H)]
    U, D = [[0] * W for _ in range(H)], [[0] * W for _ in range(H)]
    for x1 in range(H):
        for y1 in range(W):
            if S[x1][y1] == ".":  # left and upper sides of S[i][j]
                U[x1][y1] = U[x1 - 1][y1] + 1
                L[x1][y1] = L[x1][y1 - 1] + 1
            x2, y2 = -x1 - 1, -y1 - 1
            if S[x2][y2] == ".":  # right and under sides
                D[x2][y2] = D[x2 + 1][y2] + 1
                R[x2][y2] = R[x2][y2 + 1] + 1
    ans = -1
    for i in range(H):
        for j in range(W):
            ans = max(ans, L[i][j] + R[i][j] + U[i][j] + D[i][j] - 3)
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# ABC129D - Lamp
def main():
    H, W, *S = open(0).read().split()
    H, W = int(H), int(W)
    L, R = [[0] * W for _ in range(H)], [[0] * W for _ in range(H)]
    U, D = [[0] * W for _ in range(H)], [[0] * W for _ in range(H)]
    for x1 in range(H):
        for y1 in range(W):
            if S[x1][y1] == ".":  # left and upper sides of S[i][j]
                U[x1][y1] = U[x1 - 1][y1] + 1
                L[x1][y1] = L[x1][y1 - 1] + 1
            x2, y2 = -x1 - 1, -y1 - 1
            if S[x2][y2] == ".":  # right and under sides
                D[x2][y2] = D[x2 + 1][y2] + 1
                R[x2][y2] = R[x2][y2 + 1] + 1
    ans = max(L[i][j] + R[i][j] + U[i][j] + D[i][j] - 3 for i in range(H) for j in range(W))
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# ABC129D - Lamp
def main():
    H, W, *S = open(0).read().split()
    H, W = int(H), int(W)
    L, R = [[0] * W for _ in range(H)], [[0] * W for _ in range(H)]
    U, D = [[0] * W for _ in range(H)], [[0] * W for _ in range(H)]
    for x1 in range(H):
        for y1 in range(W):
            if S[x1][y1] == ".":  # left and upper sides of S[i][j]
                U[x1][y1] = U[x1 - 1][y1] + 1
                L[x1][y1] = L[x1][y1 - 1] + 1
            x2, y2 = -x1 - 1, -y1 - 1
            if S[x2][y2] == ".":  # right and under sides
                D[x2][y2] = D[x2 + 1][y2] + 1
                R[x2][y2] = R[x2][y2 + 1] + 1
    ans = -float("inf")
    for i in range(H):
        for j in range(W):
            ans = max(ans, L[i][j] + R[i][j] + U[i][j] + D[i][j] - 3)
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# ABC129D - Lamp
def main():
    H, W, *S = open(0).read().split()
    H, W = int(H), int(W)
    L, R = [[0] * W for _ in range(H)], [[0] * W for _ in range(H)]
    U, D = [[0] * W for _ in range(H)], [[0] * W for _ in range(H)]
    for x1 in range(H):
        for y1 in range(W):
            if S[x1][y1] == ".":  # left and upper sides of S[i][j]
                U[x1][y1] = U[x1 - 1][y1] + 1
                L[x1][y1] = L[x1][y1 - 1] + 1
            x2, y2 = -x1 - 1, -y1 - 1
            if S[x2][y2] == ".":  # right and under sides
                D[x2][y2] = D[x2 + 1][y2] + 1
                R[x2][y2] = R[x2][y2 + 1] + 1
    ans = -float("inf")
    for i in range(H):
        for j in range(W):
            x = L[i][j] + R[i][j] + U[i][j] + D[i][j] - 3
            if x > ans:
                ans = x
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
ls = [input().strip() for _ in range(h)]
counts = [[[-1] * w for _ in range(h)] for _ in range(4)]

for y in range(h):
    for x in range(w):
        if ls[y][x] == '#':
            counts[0][y][x] = 0
            counts[2][y][x] = 0
        else:
            if x == 0:
                counts[0][y][x] = 1
            else:
                counts[0][y][x] = counts[0][y][x - 1] + 1
            if y == 0:
                counts[2][y][x] = 1
            else:
                counts[2][y][x] = counts[2][y - 1][x] + 1

        yy = h - 1 - y
        xx = w - 1 - x
        if ls[yy][xx] == '#':
            counts[1][yy][xx] = 0
            counts[3][yy][xx] = 0
        else:
            if xx == w - 1:
                counts[1][yy][xx] = 1
            else:
                counts[1][yy][xx] = counts[1][yy][xx + 1] + 1
            if yy == h - 1:
                counts[3][yy][xx] = 1
            else:
                counts[3][yy][xx] = counts[3][yy + 1][xx] + 1

res = 0
for y in range(h):
    for x in range(w):
        val = -3
        for i in range(4):
            val += counts[i][y][x]
        res = max(res, val)
print(res)

Submission status: Accepted

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

def solve():
    H, W = list(map(int, sys.stdin.readline().split()))
    grid = []
    for _ in range(H):
        grid.append(list(input()))
    grid = (np.array(grid) == ".")*1  # ランプが置ける:1, 障害物:0

    #print(grid)

    L = np.zeros((H,W), dtype=int)  # 左方向に照らせるマスの数
    R = np.zeros((H,W), dtype=int)  # 右方向に照らせるマスの数
    U = np.zeros((H,W), dtype=int)  # 上方向に照らせるマスの数
    D = np.zeros((H,W), dtype=int)  # 下方向に照らせるマスの数

    # 左方向に照らせるマスの数のDPを埋める
    for i in range(W):
        if i == 0:
            L[:, i] = grid[:, i]
        else:
            L[:, i] = (L[:, i-1] + 1) * grid[:, i]

    # 右方向に照らせるマスの数のDPを埋める
    for i in range(W-1, -1, -1):
        if i == W-1:
            R[:, i] = grid[:, i]
        else:
            R[:, i] = (R[:, i+1] + 1) * grid[:, i]

    # 上方向に照らせるマスの数のDPを埋める
    for i in range(H):
        if i == 0:
            U[i] = grid[i]
        else:
            U[i] = (U[i-1] + 1) * grid[i]

    # 下の方向に照らせるマスの数のDPを埋める
    for i in range(H-1, -1, -1):
        if i == H-1:
            D[i] = grid[i]
        else:
            D[i] = (D[i+1] + 1) * grid[i]

    # 全方向すべてのDPを埋めた。
    # よって、i行j列目に明かりを設置したとき、全方向に照らせるマスの数を格納した ans_grid は、
    ans_grid = L + R + U + D - 3
    ans = np.max(ans_grid)
    print(ans)


if __name__ == "__main__":
    solve()

Submission status: Accepted

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

L = [[1]*W for i in range(H)]
R = [[1]*W for i in range(H)]
U = [[1]*W for i in range(H)]
D = [[1]*W for i in range(H)]
ans = 0
for i in range(H):
    for j in range(W):
        if S[i][j] == "#":
            L[i][j] = 0
            U[i][j] = 0
        elif i == 0 and j == 0:
            continue
        elif i == 0:
            L[i][j] = L[i][j-1] + 1
        elif j == 0:
            U[i][j] = U[i-1][j] + 1
        else:
            L[i][j] = L[i][j-1] + 1
            U[i][j] = U[i-1][j] + 1


for i in range(H-1,-1,-1):
    for j in range(W-1,-1,-1):
        if S[i][j] == "#":
            R[i][j] = 0
            D[i][j] = 0
        elif i == H-1 and j == W-1:
            R[i][j] = 1
        elif i == H-1:
            R[i][j] = R[i][j+1] + 1
        elif j == W-1:
            D[i][j] = D[i+1][j] + 1
        else:
            R[i][j] = R[i][j+1] + 1
            D[i][j] = D[i+1][j] + 1
        tmp = L[i][j] + R[i][j] + D[i][j] + U[i][j] - 3
        if tmp > ans:
            ans = tmp

print(ans)

Submission status: Accepted

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

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

init = lambda :[[0] * (w+2) for i in range(h+2)]
r, l, u, d = init(), init(), init(), init()

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
        else:
            l[i][j] = 0
            u[i][j] = 0

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

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

print(ans-3)

Submission status: Accepted

Solution code:
import numpy as np

H,W=map(int,input().split())
S=np.array([list(input()) for _ in range(H)])=='.'
D=np.zeros((H,W),dtype=int)
U=np.zeros((H,W),dtype=int)
L=np.zeros((H,W),dtype=int)
R=np.zeros((H,W),dtype=int)

for i in range(H):
  U[i]=(U[i-1]+1)*S[i]
  D[-i-1]=(D[-i]+1)*S[-i-1]
for i in range(W):
  L[:,i]=(L[:,i-1]+1)*S[:,i]
  R[:,-i-1]=(R[:,-i]+1)*S[:,-i-1]
#print(L)
print(np.max(U+D+L+R)-3)

Submission status: Accepted

Solution code:
H,W=map(int,input().split())
A=[list(map(lambda x:False if x=='#' else True ,input())) for i in range(H)]

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


for i in range(H):
	for j in range(1,W):
		if A[i][j]:
			L[i][j]=int(A[i][j-1])+L[i][j-1]

for i in range(H):
	for j in range(W-1)[::-1]:
		if A[i][j]:
			R[i][j]=int(A[i][j+1])+R[i][j+1]

for i in range(1,H):
	for j in range(W):
		if A[i][j]:
			U[i][j]=int(A[i-1][j])+U[i-1][j]


for i in range(H-1)[::-1]:
	for j in range(W):
		if A[i][j]:
			D[i][j]=int(A[i+1][j])+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]+1)

print(ans)

Submission status: Accepted

Solution code:
#00:27
h,w = map(int,input().split())
raw = []
for _ in range(h):
  raw.append(input())
ver = [[] for _ in range(h)]
hrz = [[] for _ in range(h)]
for x in range(h):
  st = 0
  for y in range(w):
    if raw[x][y] == '#':
      for k in range(st,y):
        ver[x].append(y-st)
      ver[x].append(0)
      st = y + 1
  for k in range(st,w):
    ver[x].append(w-st)
#print(ver)
for y in range(w):
  st = 0
  for x in range(h):
    if raw[x][y] == '#':
      for k in range(st,x):
        hrz[k].append(x-st)
      hrz[x].append(0)
      st = x + 1
  for k in range(st,h):
    hrz[k].append(h-st)
#print(hrz)
ans = 0
for x in range(h):
  for y in range(w):
    ans = max(ans,ver[x][y]+hrz[x][y]-1)
print(ans)

Submission status: Accepted

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

def main():
    H, W = map(int, input().split())

    map_ = np.array([list(input().strip()) for i 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(1, H):
        up[i] = (up[i-1] + 1) * map_[i-1]
    for i in range(H-2, -1, -1):
        down[i] = (down[i+1] + 1) * map_[i+1]
    for j in range(1, W):
        left[:,j] = (left[:,j-1] + 1) * map_[:,j-1]
    for j in range(W-2, -1, -1):
        right[:,j] = (right[:,j+1] + 1) * map_[:,j+1]
    
    print(((up + down + left + right + 1) * map_).max())


if __name__ == "__main__":
    main()

Submission status: Accepted

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

M_W = [[0] * W for i in range(H)]
M_H = [[0] * W for i in range(H)]

for i in range(H):
  for j in range(W):
    if S[i][j] == '.':
      if j == 0:
        M_W[i][j] = 1
      else:
        M_W[i][j] = M_W[i][j - 1] + 1
  for j in range(W-2,-1,-1):
    if S[i][j] == '.' and S[i][j+1] == '.':
      M_W[i][j] = M_W[i][j+1]

for j in range(W):
  for i in range(H):
    if S[i][j] == '.':
      if i == 0:
        M_H[i][j] = 1
      else:
        M_H[i][j] = M_H[i-1][j] + 1
  for i in range(H-2,-1,-1):
    if S[i][j] == '.' and S[i+1][j] == '.':
      M_H[i][j] = M_H[i+1][j]

#print(M_W)
#print(M_H)

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

Submission status: Accepted

Solution code:
def slove():
    import sys
    input = sys.stdin.readline
    h, w = list(map(int, input().rstrip('\n').split()))
    s = [list(str(input().rstrip('\n'))) for _ in range(h)]
    R = [[0] * w for _ in range(h)]
    L = [[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 i == 0:
                U[i][j] = 1 if s[i][j] == "." else 0
                D[-i-1][j] = 1 if s[-i-1][j] == "." else 0
            else:
                U[i][j] = U[i-1][j] + 1 if s[i][j] == "." else 0
                D[-i-1][j] = D[-i][j] + 1 if s[-i-1][j] == "." else 0
            if j == 0:
                L[i][j] = 1 if s[i][j] == "." else 0
                R[i][-j-1] = 1 if s[i][-j-1] == "." else 0
            else:
                L[i][j] = L[i][j-1] + 1 if s[i][j] == "." else 0
                R[i][-j-1] = R[i][-j] + 1 if s[i][-j-1] == "." else 0
    m_l = 0
    for i in range(h):
        for j in range(w):
            m_l = max(m_l, R[i][j] + L[i][j] + U[i][j] + D[i][j] - 3)
    print(m_l)


if __name__ == '__main__':
    slove()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
h,w = map(int,input().split())
M = []
for i in range(h):
    M.append(input())
L = []
R = []
U = []
D = []
Total = []
for i in range(h):
    s = [0]*w
    L.append(s)
for i in range(h):
    s = [0]*w
    R.append(s)
for i in range(h):
    s = [0]*w
    U.append(s)
for i in range(h):
    s = [0]*w
    D.append(s)
for i in range(h):
    s = [0]*w
    Total.append(s)
for i in range(h):
    for j in range(w):
        if M[i][j] == '.':
            if j == 0:
                L[i][j] = 1
            else:
                L[i][j] = L[i][j-1]+1
for i in range(h):
    for j in range(w-1,-1,-1):
        if M[i][j] == '.':
            if 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 M[i][j] == '.':
            if i == 0:
                U[i][j] = 1
            else:
                U[i][j] = U[i-1][j]+1
for i in range(h-1,-1,-1):
    for j in range(w):
        if M[i][j] == '.':
            if i == h-1:
                D[i][j] = 1
            else:
                D[i][j] = D[i+1][j]+1
for i in range(h):
    for j in range(w):
        Total[i][j] += L[i][j]+R[i][j]+U[i][j]+D[i][j]-3
ans = 0
for i in range(h):
    for j in range(w):
        ans = max(ans,Total[i][j])
print(ans)