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 = (int(i) for i in input().split()) 
s = []

for i in range(h):
  tmp = input()
  s.append(tmp)

#左にいくつあるか
l = [[0 for i in range(w)] for j in range(h)]

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

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

#右にいくつあるか
r = [[0 for i in range(w)] for j in range(h)]

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

#下
u = [[0 for i in range(w)] for j in range(h)]

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

#print(u)


#足し合わせ
for i in range(h):
  for j in range(w):
    l[i][j] += r[i][j] + u[i][j] + d[i][j] -3

unko = []
for i in range(h):
  mm = max(l[i])
  unko.append(mm)

print(max(unko))

Submission status: Accepted

Solution code:
import numpy as np

H, W = map(int, input().split())
inputs = np.array([list(input()) for _ in range(H)])
grid = inputs == '.'

# 左から右に処理を行う
vertical_1 = np.zeros((H, W), dtype=int)
vertical_1[:, 0] = grid[:, 0]
for i in range(1, W):
    vertical_1[:, i] = (vertical_1[:, i-1] + 1) * grid[:, i]

# 右から左に処理を行う
vertical_2 = np.zeros((H, W), dtype=int)
vertical_2[:, -1] = grid[:, -1]
for i in range(W-2, -1, -1):
    vertical_2[:, i] = (vertical_2[:, i+1] + 1) * grid[:, i]

# 上から下に処理を行う
down_1 = np.zeros((H, W), dtype=int)
down_1[0] = grid[0]
for i in range(1, H):
    down_1[i] = (down_1[i-1] + 1) * grid[i]

# 右から左に処理を行う
down_2 = np.zeros((H, W), dtype=int)
down_2[-1] = grid[-1]
for i in range(H-2, -1, -1):
    down_2[i] = (down_2[i+1] + 1) * grid[i]

print((vertical_1 + vertical_2 +  down_1 + down_2 - 3).max())

Submission status: Accepted

Solution code:


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

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

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

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

for i in range(h-1,-1,-1):
    for j in range(w):
        if s[i][j] == "#":
            d[i][j] = 0
        else:
            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-1,-1,-1):
        if s[i][j] == "#":
            r[i][j] = 0
        else:
            if j == w-1:
                r[i][j] = 1
            else:
                r[i][j] = r[i][j+1] + 1

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

Submission status: Accepted

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


def solve(H: int, W: int, S: "List[str]"):
    L = [[0 for _ in range(W)] for _ in range(H)]
    R = [[0 for _ in range(W)] for _ in range(H)]
    D = [[0 for _ in range(W)] for _ in range(H)]
    U = [[0 for _ in range(W)] for _ in range(H)]
    for i in range(H):
        for j in range(W):
            if S[i][j] != '#':
                if j == 0:
                    L[i][j] = 1
                else:
                    L[i][j] = L[i][j-1] + 1
            jj = W - 1 - j
            if S[i][jj] != '#':
                if jj == W-1:
                    R[i][jj] = 1
                else:
                    R[i][jj] = R[i][jj+1] + 1
            if S[i][j] != '#':
                if i == 0:
                    D[i][j] = 1
                else:
                    D[i][j] = D[i-1][j] + 1
            ii = H - 1 - i
            if S[ii][j] != '#':
                if ii == H-1:
                    U[ii][j] = 1
                else:
                    U[ii][j] = U[ii+1][j] + 1
    mx = 0 
    for i in range(H):
        for j in range(W):
            m = L[i][j] + R[i][j] + U[i][j] + D[i][j] - 3
            if mx < m:
                mx = m
    print(int(mx))

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

if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

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

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

    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]

    for i in range(H):
        if i == 0:
            U[i, :] = grid[i, :]
        else:
            U[i, :] = (U[i-1, :] + 1) * grid[i, :]
    
    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, :]
    
    ans = np.max(L+R+U+D-3)
    print(ans)

if __name__ == "__main__":
    solve()

Submission status: Accepted

Solution code:
def D_Lamp():
    import numpy as np
    # input & init
    h, w = map(int, input().split())
    S = np.array([list(input()) for _ in range(h)]) == "."
    L = np.zeros((h,w), dtype=int)
    R = np.zeros((h,w), dtype=int)
    U = np.zeros((h,w), dtype=int)
    D = np.zeros((h,w), dtype=int)
    # calc row
    for row in range(h):
        D[row] =(D[row - 1] + 1) * S[row]
        U[-row - 1] = (U[-row] + 1) * S[-row -1]
    # calc col
    for col in range(w):
        R[:, col] = (R[:, col - 1] + 1) * S[:, col]
        L[:, -col - 1] = (L[:, -col] + 1) * S[:, -col-1]
    num_max = 0
    print(np.max(L+R+U+D) - 3)

if __name__ == "__main__":
    D_Lamp()

Submission status: Accepted

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

# for f in field:
#     print(*f)
# print()

cntx = [[0] * w for _ in range(h)]
cnty = [[0] * w for _ in range(h)]

for y in range(h):
    c = 0
    left = 0
    for x in range(w + 1):
        if x < w and field[y][x] == ".":
            c += 1
        else:
            for pos in range(left, x):
                cntx[y][pos] = c
            c = 0
            left = x + 1
for x in range(w):
    c = 0
    left = 0
    for y in range(h + 1):
        if y < h and field[y][x] == ".":
            c += 1
        else:
            for pos in range(left, y):
                cnty[pos][x] = c
            c = 0
            left = y + 1
# for c in cntx:
#     print(*c)
# print()
# for c in cnty:
#     print(*c)

maxv = 0
for y in range(h):
    for x in range(w):
        maxv = max(maxv, cntx[y][x] + cnty[y][x] - 1)
print(maxv)

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
meiro = [list(input()) for i in range(H)]
right = [[0 for _ in range(W)] for _ in range(H)]
left= [[0 for _ in range(W)] for _ in range(H)]
up = [[0 for _ in range(W)] for _ in range(H)]
down = [[0 for _ in range(W)] for _ in range(H)]
for i in range(H):
    cur=0
    for j in range(W):
        if meiro[i][j]=="#":
            cur = 0
        else:
            cur +=1
        left[i][j]=cur
for i in range(H):
    cur=0
    for j in range(W-1,-1,-1):
        if meiro[i][j]=="#":
            cur = 0
        else:
            cur +=1
        right[i][j]=cur
for i in range(W):
    cur=0
    for j in range(H):
        if meiro[j][i]=="#":
            cur = 0
        else:
            cur +=1
        up[j][i]=cur
for i in range(W):
    cur=0
    for j in range(H-1,-1,-1):
        if meiro[j][i]=="#":
            cur = 0
        else:
            cur +=1
        down[j][i]=cur
res = 0
for i in range(H):
    for j in range(W):
        res = max(res,right[i][j]+left[i][j]+up[i][j]+down[i][j]-3)
print(res)

Submission status: Accepted

Solution code:
import numpy as np

h,w=map(int,input().split())
s=[]
for i in range(h):
    s.append(list(input()))
grid=(np.array(s) ==".") *1

l=np.zeros((h,w),dtype=int)
r=np.zeros((h,w),dtype=int)
u=np.zeros((h,w),dtype=int)
d=np.zeros((h,w),dtype=int)

for j in range(w):
    if j == 0:
        l[:,j] = grid[:,j]
    else:
        l[:,j] = (l[:,j-1]+1) * grid[:,j]
for j in range(w-1,-1,-1):
    if j == (w-1):
        r[:,j] = grid[:,j]
    else:
        r[:,j] = (r[:,j+1] +1)*grid[:,j]

for i in range(h):
    if i == 0:
        u[i,:]=grid[i,:]
    else:
        u[i,:]=(u[i-1,:]+1)*grid[i,:]
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,:]


ans=np.max(l+r+u+d-3)
print(ans)

Submission status: Accepted

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

def solve():
    H, W = list(map(int, input().split()))
    S = []
    for i in range(H):
        S.append(list(input()))
    grid = (np.array(S) == ".") * 1  # ランプをおける:1, 障害物:0

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

    for j in range(W):
        if j==0:
            L[:,j] = grid[:,j]
        else:
            L[:,j] = (L[:,j-1]+1) * grid[:,j]
    
    for j in range(W-1, -1, -1):
        if j==(W-1):
            R[:,j] = grid[:,j]
        else:
            R[:,j] = (R[:,j+1] + 1) * grid[:, j]
    
    for i in range(H):
        if i==0:
            U[i,:] = grid[i,:]
        else:
            U[i,:] = (U[i-1,:]+1)*grid[i,:]
    
    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,:]
    
    ans = np.max(L+R+U+D-3)
    print(ans)

if __name__ == "__main__":
    solve()

Submission status: Accepted

Solution code:
import sys
from collections import deque, defaultdict
import copy
import bisect
input=sys.stdin.readline
sys.setrecursionlimit(10 ** 9)

H, W = list(map(int, input().split()))
grid = []
for i in range(H):
    grid.append(input()[0:W])

num_yoko = [[0 for i in range(W)] for j in range(H)]
num_tate = [[0 for i in range(W)] for j in range(H)]

for i in range(H):
    for j in range(W):
        if grid[i][j] == '.':
            if j == 0:
                num_yoko[i][j] = 1
            else:
                num_yoko[i][j] = num_yoko[i][j - 1] + 1
            if i == 0:
                num_tate[i][j] = 1
            else:
                num_tate[i][j] = num_tate[i - 1][j] + 1
for i in range(H - 1, -1, -1):
    for j in range(W - 1, -1, -1):
        if grid[i][j] == '.':
            if j != W - 1:
                if num_yoko[i][j + 1] != 0:
                    num_yoko[i][j] = num_yoko[i][j + 1]
            if i != H - 1:
                if num_tate[i + 1][j] != 0:
                    num_tate[i][j] = num_tate[i + 1][j]

num_max = 0
for i in range(H):
    for j in range(W):
        if num_yoko[i][j] + num_tate[i][j] > num_max:
            num_max = num_yoko[i][j] + num_tate[i][j]


print(max(0, num_max - 1))