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 = list(map(int, input().split()))
d = {".":0, "#":1}
ss = [list(input()) for _ in range(h)]
ss = [[d[x] for x in s] for s in ss]

ws = [[0 for _ in range(w)] for _ in range(h)]
hs = [[0 for _ in range(w)] for _ in range(h)]

i = 0
while i < h:
	j = 0
	while j < w:
		if ss[i][j] == 0:
			end = w
			for k in range(j + 1, w):
				if ss[i][k] == 1:
					end = k
					break
			for l in range(j, end):
				ws[i][l] = end - j
			j = end + 1
		else:
			j += 1
	i += 1

j = 0
while j < w:
	i = 0
	while i < h:
		if ss[i][j] == 0:
			end = h
			for k in range(i + 1, h):
				if ss[k][j] == 1:
					end = k
					break
			for l in range(i, end):
				hs[l][j] = end - i
			i = end + 1
		else:
			i += 1
	j += 1

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

Submission status: Accepted

Solution code:
import numpy as np

h, w = list(map(int, input().split()))
d = {".":1, "#":0}
ss = np.array([list(input()) for _ in range(h)]) == "."
ss_t = ss.transpose()

ls, rs, us, ds = [np.zeros((h, w), dtype=int) for _ in range(4)]

ls, rs = ls.transpose(), rs.transpose()

ls[0] = ss_t[0]
rs[w-1] = ss_t[w-1]
us[0] = ss[0]
ds[h-1] = ss[h-1]

for i in range(1, w):
	ls[i] = ss_t[i] * (ls[i - 1] + 1)

for i in reversed(range(w - 1)):
	rs[i] = ss_t[i] * (rs[i + 1] + 1)

for i in range(1, h):
	us[i] = ss[i] * (us[i - 1] + 1)

for i in reversed(range(h - 1)):
	ds[i] = ss[i] * (ds[i + 1] + 1)

# print(ss)
# print(ls)
# print(rs)
# print(us)
# print(ds)
print(max(np.amax((ls.transpose() + rs.transpose() + us + ds) - 3), 0))

Submission status: Accepted

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

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

def countlight(dir):
    yrange = range(H - 1, -1, -1) if dir == 'down'  else range(H)
    xrange = range(W - 1, -1, -1) if dir == 'right' else range(W)
    nxt = {
        'left':  lambda xy: (xy[0] - 1, xy[1]),
        'right': lambda xy: (xy[0] + 1, xy[1]),
        'up':    lambda xy: (xy[0],     xy[1] - 1),
        'down':  lambda xy: (xy[0],     xy[1] + 1)
    }
    for y in yrange:
        for x in xrange:
            x1, y1 = nxt[dir]((x, y))
            tmp[y][x] = (tmp[y1][x1] if 0 <= x1 < W and 0 <= y1 < H else 0) \
                        + 1 if S[y][x] == '.' else 0
            T[y][x]  += tmp[y][x] - 1

for dir in ('left', 'right', 'up', 'down'):
    countlight(dir)

print(max(max(T[y]) for y in range(H)) + 1)

Submission status: Accepted

Solution code:
import numpy as np

H,W = map(int,input().split())
# 周りに壁を、0,1化、壁を0で持つ
grid = np.zeros((H+2,W+2),dtype=np.bool)
grid[1:-1,1:-1] = (np.array([list(input()) for _ in range(H)]) == '.')

# x軸方向の集計
def F(transpose):
  x = grid
  if transpose:
    x = x.T
  x = np.ravel(x) # to 1D array
  L = len(x)
  
  left_zero = -np.arange(L) # 左の壁のindexの-1倍
  left_zero[x] = 0
  np.minimum.accumulate(left_zero, out=left_zero)
  
  right_zero = np.arange(L) # 逆順ソートした上で左の壁のindex
  right_zero[x[::-1]] = 0
  np.maximum.accumulate(right_zero, out=right_zero)
  
  y = left_zero
  y += L-right_zero[::-1]
  
  return y.reshape(grid.T.shape).T if transpose else y.reshape(grid.shape)
  
answer = (F(False) + F(True)).max()-5
print(answer)

Submission status: Accepted

Solution code:
H, W = list(map(int, input().split()))
S = [input() for y in range(H)]
c = [[0 for x in range(W)] for y in range(H)]
for y in range(H):
  s = 0
  for x in range(W):
    if S[y][x]=='#':
      s = 0
    else:
      c[y][x] = 1
      c[y][x] += s
      s += 1
  s = 0
  for x in range(W-1,-1,-1):
    if S[y][x]=='#':
      s = 0
    else:
      c[y][x] += s
      s += 1
for x in range(W):
  s = 0
  for y in range(H):
    if S[y][x]=='#':
      s = 0
    else:
      c[y][x] += s
      s += 1
  s = 0
  for y in range(H-1,-1,-1):
    if S[y][x]=='#':
      s = 0
    else:
      c[y][x] += s
      s += 1
print(max(map(max,c)))

Submission status: Accepted

Solution code:
import numpy as np

H,W = map(int,input().split())
# 周りに壁を、0,1化、壁をTrueで持つ
grid = np.ones((H+2,W+2),dtype=np.bool)
grid[1:-1,1:-1] = (np.array([list(input()) for _ in range(H)]) == '#')

# x軸方向の集計
def F(transpose):
  x = grid
  if transpose:
    x = x.T
  x = np.ravel(x) # to 1D array
  
  zero_idx = np.nonzero(x)[0]
  zero_span = np.diff(zero_idx) - 1 # 次の0までにある1の個数
  
  y = np.zeros(x.shape[0],dtype=np.int32)
  y[1:][zero_idx[:-1]] = zero_span # 1の個数の加算の起点
  y[zero_idx[1:]] -= zero_span # 1の個数の反映をやめる
  np.cumsum(y, out = y)
  if transpose:
    y = y.reshape(grid.T.shape).T
  else:
    y = y.reshape(grid.shape)
  return y

answer = (F(True) + F(False)).max() - 1
print(answer)

Submission status: Accepted

Solution code:
import numpy as np

H,W = map(int,input().split())
# 周りに壁を、0,1化、壁をTrueで持つ
grid = np.ones((H+2,W+2),dtype=np.bool)
grid[1:-1,1:-1] = (np.array([list(input()) for _ in range(H)]) == '#')

# x軸方向の集計
def F(transpose):
  x = grid
  if transpose:
    x = x.T
  x = np.ravel(x) # to 1D array
  
  zero_idx = np.nonzero(x)[0]
  zero_span = np.diff(zero_idx) - 1 # 次の0までにある1の個数
  
  y = np.zeros(x.shape[0],dtype=np.int32)
  y[1:][zero_idx[:-1]] = zero_span # 1の個数の加算の起点
  y[zero_idx[1:]] -= zero_span # 1の個数の反映をやめる
  np.cumsum(y, out = y)
  return y.reshape(grid.T.shape).T if transpose else y.reshape(grid.shape)

answer = (F(True) + F(False)).max() - 1
print(answer)

Submission status: Accepted

Solution code:
import numpy as np

H,W = map(int,input().split())
# 周りに壁を、0,1化、壁をTrueで持つ
grid = np.ones((H+2,W+2),dtype=np.bool)
grid[1:-1,1:-1] = (np.array([list(input()) for _ in range(H)]) == '#')

# x軸方向の集計
def F(transpose):
  x = grid
  if transpose:
    x = x.T
  x = np.ravel(x) # to 1D array
  
  zero_idx = np.nonzero(x)[0]
  zero_span = np.diff(zero_idx) - 1 # 次の0までにある1の個数
  
  y = np.zeros(x.shape[0],dtype=np.int32)
  y[1:][zero_idx[:-1]] = zero_span # 1の個数の加算の起点
  y[zero_idx[1:]] -= zero_span # 1の個数の反映をやめる
  y = y.cumsum()
  return y.reshape(grid.T.shape).T if transpose else y.reshape(grid.shape)

answer = (F(True) + F(False)).max() - 1
print(answer)

Submission status: Accepted

Solution code:
import numpy as np

H,W = map(int,input().split())
# 周りに壁を、0,1化、壁をTrueで持つ
grid = np.ones((H+2,W+2),dtype=np.bool)
grid[1:-1,1:-1] = (np.array([list(input()) for _ in range(H)]) == '#')

# x軸方向の集計
def F(transpose):
  x = grid
  if transpose:
    x = x.T
  x = np.ravel(x) # to 1D array
  
  zero_idx = np.nonzero(x)[0]
  zero_span = np.diff(zero_idx) - 1 # 次の0までにある1の個数
  
  y = np.zeros(x.shape[0],dtype=np.int32)
  y[1:][zero_idx[:-1]] = zero_span # 1の個数の加算の起点
  y[zero_idx[1:]] -= zero_span # 1の個数の反映をやめる
  y = y.cumsum()
  if transpose:
    y = y.reshape(grid.T.shape).T
  else:
    y = y.reshape(grid.shape)
  return y

answer = (F(True) + F(False)).max() - 1
print(answer)

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
maze = [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 maze[i][j] == '#':
            continue
        if done[j]:
            continue
        l = 0
        while j + l < W:
            if maze[i][j+l] == '#':
                break
            l += 1
        for k in range(l):
            cnt[i][j+k] += l
            done[j+k] = 1

for j in range(W):
    done = [0] * H
    for i in range(H):
        if maze[i][j] == '#':
            continue
        if done[i]:
            continue
        l = 0
        while i + l < H:
            if maze[i+l][j] == '#':
                break
            l += 1
        for k in range(l):
            cnt[i+k][j] += l
            done[i+k] = 1
ans = 0
for i in range(H):
    ans = max(ans, max(cnt[i]) - 1)

print(ans)

Submission status: Accepted

Solution code:
import numpy as np

H,W = map(int,input().split())
# 周りに壁を、0,1化、壁をTrueで持つ
grid = np.ones((H+2,W+2),dtype=np.bool)
grid[1:-1,1:-1] = (np.array([list(input()) for _ in range(H)]) == '#')

# x軸方向の集計
def F(transpose):
  x = grid.copy()
  if transpose:
    x = x.T
  x = np.ravel(x) # to 1D array
  
  zero_idx = np.nonzero(x)[0]
  zero_span = np.diff(zero_idx) - 1 # 次の0までにある1の個数
  
  y = np.zeros(x.shape[0],dtype=np.int32)
  y[1:][zero_idx[:-1]] = zero_span # 1の個数の加算の起点
  y[zero_idx[1:]] -= zero_span # 1の個数の反映をやめる
  y = y.cumsum()
  if transpose:
    y = y.reshape(grid.T.shape).T
  else:
    y = y.reshape(grid.shape)
  return y

answer = (F(True) + F(False)).max() - 1
print(answer)

Submission status: Accepted

Solution code:
import numpy as np

H,W = map(int,input().split())
temp = np.array([list(input()) for _ in range(H)])
# 周りに壁を、0,1化、壁をTrueで持つ
grid = np.ones((H+2,W+2),dtype=np.bool)
grid[1:-1,1:-1] = (temp == '#')

# x軸方向の集計
def F(transpose):
  x = grid.copy()
  if transpose:
    x = x.T
  x = np.ravel(x) # to 1D array
  
  zero_idx = np.nonzero(x)[0]
  zero_span = np.diff(zero_idx) - 1 # 次の0までにある1の個数
  
  y = np.zeros(x.shape[0],dtype=np.int32)
  y[1:][zero_idx[:-1]] = zero_span # 1の個数の加算の起点
  y[zero_idx[1:]] -= zero_span # 1の個数の反映をやめる
  y = y.cumsum()
  if transpose:
    y = y.reshape(grid.T.shape).T
  else:
    y = y.reshape(grid.shape)
  return y

answer = (F(True) + F(False)).max() - 1
print(answer)

Submission status: Accepted

Solution code:
import numpy as np

H,W = map(int,input().split())
temp = np.array([list(input()) for _ in range(H)])
# 周りに壁を、0,1化、壁をTrueで持つ
map = np.ones((H+2,W+2),dtype=np.bool)
map[1:-1,1:-1] = (temp == '#')

# x軸正方向の集計
def F(transpose):
  x = map.copy()
  if transpose:
    x = x.T
  x = np.ravel(x) # to 1D array
  
  zero_idx = np.nonzero(x)[0]
  zero_span = np.diff(zero_idx) - 1 # 次の0までにある1の個数
  y = np.zeros(len(x),dtype=np.int32)
  y[1:][zero_idx[:-1]] = zero_span
  y[zero_idx[1:]] -= zero_span
  y = y.cumsum()
  if transpose:
    y = y.reshape(map.T.shape).T
  else:
    y = y.reshape(map.shape)
  return y

answer = np.zeros(map.shape,dtype=np.int32)
for t in [True,False]:
  answer += F(t)
    
answer = answer.max() - 1
print(answer)

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
dungeon = []
for i in range(0, H):
	dungeon.append(input())
across = []
for i in range(0, H):
	lst = []
	r = []
	for j in range(0, W):
		if dungeon[i][j] == '.':
			if r == []: r.append(j)
		else:
			if r != []:
				r.append(j)
				lst.append(r)
				r = []
	if r != []:
		r.append(W)
		lst.append(r)
	across.append(lst)
acrossdungeon = []
for i in range(0, H):
	lst = []
	index = 0
	for j in range(0, W):
		if dungeon[i][j] == '.':
			lst.append(across[i][index][1]-across[i][index][0])
		else:
			if j != 0:
				if lst[-1] != -1:
					index += 1
			lst.append(-1)
	acrossdungeon.append(lst)
down = []
for i in range(0, W):
	lst = []
	r = []
	for j in range(0, H):
		if dungeon[j][i] == '.':
			if r == []: r.append(j)
		else:
			if r != []:
				r.append(j)
				lst.append(r)
				r = []
	if r != []:
		r.append(H)
		lst.append(r)
	down.append(lst)
downdungeon = []
for i in range(0, W):
	lst = []
	index = 0
	for j in range(0, H):
		if dungeon[j][i] == '.':
			lst.append(down[i][index][1]-down[i][index][0])
		else:
			if j != 0:
				if lst[-1] != -1:
					index += 1
			lst.append(-1)
	downdungeon.append(lst)
max = 0
for i in range(0, H):
	for j in range(0, W):
		if dungeon[i][j] != '#':
			lights = acrossdungeon[i][j] + downdungeon[j][i] - 1
			if max < lights: max = lights
print(max)

Submission status: Accepted

Solution code:
import numpy as np

h, w = map(int, input().split())
grid = []
for i in range(h):
    grid.append(list(input()))
grid = (np.array(grid) == ".") * 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, :]

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

Submission status: Accepted

Solution code:
from sys import stdin
import math
import fractions
from collections import deque
import itertools

INF = 10 ** 10

# H <= 2000, W <= 2000
# H*W -> O(4*10^6)
H, W = [int(x) for x in stdin.readline().rstrip().split()]
S = [stdin.readline().rstrip() for _ in range(H)]

# L[i][j]: i行目j列目に明かりを置いたときにそこから左方向に照らされるマスの個数
#          (i行目j列目のマスを含む)
L = [[0] * W for _ in range(H)]
R = [[0] * W for _ in range(H)]
D = [[0] * W for _ in range(H)]
U = [[0] * W for _ in range(H)]

# L
for i in range(H):
    for j in range(W):
        if S[i][j] == "#":
            L[i][j] = 0
            continue

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

# R
for i in range(H-1, -1, -1):
    for j in range(W-1, -1, -1):
        if S[i][j] == "#":
            R[i][j] = 0
            continue

        if j == W-1:
            R[i][j] = 1
        else:
            R[i][j] = R[i][j+1] + 1

# U
for j in range(W):
    for i in range(H):
        if S[i][j] == "#":
            U[i][j] = 0
            continue

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

# D
for j in range(W-1, -1, -1):
    for i in range(H-1, -1, -1):
        if S[i][j] == "#":
            D[i][j] = 0
            continue

        if i == H-1:
            D[i][j] = 1
        else:
            D[i][j] = D[i+1][j] + 1


ans = -INF

for i in range(H):
    for j in range(W):
        ans = max(ans, L[i][j] + R[i][j] + D[i][j] + U[i][j] - 3)

# for DEBUG
#print(*S, sep="\n")
#print(L)
#print(R)
#print(D)
#print(U)

print(ans)

Submission status: Accepted

Solution code:
import numpy as np

h, w = map(int, input().split())
grid = []
for i in range(h):
    grid.append(list(input()))
grid = (np.array(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)

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, :]

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

Submission status: Accepted

Solution code:
import numpy as np

H,W = map(int,input().split())
temp = np.array([list(input()) for _ in range(H)])
# 周りに壁を、0,1化
map = np.zeros((H+2,W+2),dtype=np.int32)
map[1:-1,1:-1][temp == '.'] = 1

# x軸正方向の集計
def F(transpose,reverse):
  x = map.copy()
  if transpose:
    x = x.T
  x = np.ravel(x)
  if reverse:
    x = x[::-1]
  zero_idx = np.nonzero(x==0)[0]
  x[zero_idx[1:]] = 1-np.diff(zero_idx) # cumsumで求められるように変形
  x = x.cumsum()
  if reverse:
    x = x[::-1]
  if transpose:
    x = x.reshape(map.T.shape).T
  else:
    x = x.reshape(map.shape)
  return x

answer = np.zeros_like(map)
for t in [True,False]:
  for r in [True,False]:
    answer += F(t,r)
    
answer = answer.max() - 3
print(answer)

Submission status: Accepted

Solution code:
import numpy as np

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

grid = []
for i in range(h):
    grid.append(list(input()))
grid = (np.array(grid) == ".") * 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, :]

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

Submission status: Accepted

Solution code:
import numpy as np

h, w = map(int, input().split())
grid = [[False for j in range(w)] for i in range(h)]

for i in range(h):
    s = input()
    for j in range(w):
        if s[j] == '.':
            grid[i][j] = 1
        else:
            grid[i][j] = 0

grid = np.array(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)

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, :]

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

Submission status: Accepted

Solution code:
from sys import stdin
import math
import fractions
from collections import deque
import itertools

INF = 10 ** 10

# H <= 2000, W <= 2000
# H*W -> O(4*10^6)
H, W = [int(x) for x in stdin.readline().rstrip().split()]
S = [[x for x in stdin.readline().rstrip()] for _ in range(H)]

# L[i][j]: i行目j列目に明かりを置いたときにそこから左方向に照らされるマスの個数
#          (i行目j列目のマスを含む)
L = [[0] * W for _ in range(H)]
R = [[0] * W for _ in range(H)]
D = [[0] * W for _ in range(H)]
U = [[0] * W for _ in range(H)]

# L
for i in range(H):
    for j in range(W):
        if S[i][j] == "#":
            L[i][j] = 0
            continue

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

# R
for i in range(H-1, -1, -1):
    for j in range(W-1, -1, -1):
        if S[i][j] == "#":
            R[i][j] = 0
            continue

        if j == W-1:
            R[i][j] = 1
        else:
            R[i][j] = R[i][j+1] + 1

# U
for j in range(W):
    for i in range(H):
        if S[i][j] == "#":
            U[i][j] = 0
            continue

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

# D
for j in range(W-1, -1, -1):
    for i in range(H-1, -1, -1):
        if S[i][j] == "#":
            D[i][j] = 0
            continue

        if i == H-1:
            D[i][j] = 1
        else:
            D[i][j] = D[i+1][j] + 1


ans = -INF

for i in range(H):
    for j in range(W):
        ans = max(ans, L[i][j] + R[i][j] + D[i][j] + U[i][j] - 3)

# for DEBUG
#print(*S, sep="\n")
#print(L)
#print(R)
#print(D)
#print(U)

print(ans)

Submission status: Accepted

Solution code:
# -*- coding:utf-8 -*-
"""解説
https://www.youtube.com/watch?v=L8grWxBlIZ4

PDFに書いてある実装をする。

....#.
..#...
.#..#.

それぞれのマスについて、

・左マスを照らせる個数: L[i][j]
・右マスを照らせる個数: R[i][j]
・上マスを照らせる個数: U[i][j]
・下マスを照らせる個数: D[i][j]

をそれぞれ計算する。

左マスを照らす漸化式L[i][j]は、

・i行j列に障害物がある場合はL[i][j] = 0
・そうでないとき、j=1ならば、L[i][j] = 1
・そうでないとき、j>1ならば、L[i][j] = L[i][j-1] + 1

求める答えは、L[i][j]+R[i][j]+U[i][j]+D[i][j]-3 の最大値


計算量はO(HW)。

H,W = 2000なので、O(HW)はO(4*10**6)。 Pythonでこの計算量は無理だろと思って実装を諦めたが、いけるっぽい。認識を改めた。というかPyPyもあるし。

と思ったら普通に無理だった。

numpyを使ったらfor文を一個減らせる。計算量が減ったかどうかは内部処理によるので知らんけどACした。結論：numpyの行列計算は速い。

・参考にした他人のコード
https://atcoder.jp/contests/abc129/submissions/5861992

"""
import numpy as np

def solve():
    H, W = list(map(int, input().split()))
    grid = []
    for i 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 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] * 1
        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:
# -*- coding:utf-8 -*-
"""解説
https://www.youtube.com/watch?v=L8grWxBlIZ4

PDFに書いてある実装をする。

....#.
..#...
.#..#.

それぞれのマスについて、

・左マスを照らせる個数: L[i][j]
・右マスを照らせる個数: R[i][j]
・上マスを照らせる個数: U[i][j]
・下マスを照らせる個数: D[i][j]

をそれぞれ計算する。

左マスを照らす漸化式L[i][j]は、

・i行j列に障害物がある場合はL[i][j] = 0
・そうでないとき、j=1ならば、L[i][j] = 1
・そうでないとき、j>1ならば、L[i][j] = L[i][j-1] + 1

求める答えは、L[i][j]+R[i][j]+U[i][j]+D[i][j]-3 の最大値


計算量はO(HW)。

H,W = 2000なので、O(HW)はO(4*10**6)。 Pythonでこの計算量は無理だろと思って実装を諦めたが、いけるっぽい。認識を改めた。というかPyPyもあるし。

と思ったら普通に無理だった。

numpyを使ったらfor文を一個減らせる。計算量が減ったかどうかは内部処理によるので知らんけどACした。結論：numpyの行列計算は速い。

"""
import numpy as np

def solve():
    H, W = list(map(int, input().split()))
    grid = []
    for i 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 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] * 1
        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 f(h, w):
    t = [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)]
    ans = 0
    Ui1 = U[0]
    for i in range(1, 1 + h):
        Ui = U[i]
        Li = L[i]
        ti1 = t[i - 1]
        for j, tk in enumerate(ti1):
            j += 1
            if tk == "#":
                Ui[j] = 0
                Li[j] = 0
            else:
                Ui[j] = Ui1[j] + 1
                Li[j] = Li[j - 1] + 1
        Ui1 = Ui
        Di1 = D[-1]
    for i in range(h, 0, -1):
        Ui = U[i]
        Li = L[i]
        Di = D[i]
        Ri = R[i]
        ti1 = t[i - 1]
        for j,tk in enumerate(ti1[::-1]):
            j=w-j
            if tk == "#":
                Di[j] = 0
                Ri[j] = 0
            else:
                Di[j] = Di1[j] + 1
                Ri[j] = Ri[j + 1] + 1
                tmp = Ui[j] + Di[j] + Li[j] + Ri[j]
                if tmp > ans:
                    ans = tmp
        Di1 = Di
    print(ans - 3)

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

Submission status: Accepted

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

### その1
"""
# 4方向の累積和をとって計算する
Rs = [[0]*W for i in range(H)]
Ls = [[0]*W for i in range(H)]
Ds = [[0]*W for i in range(H)]
Us = [[0]*W for i in range(H)]

for i in range(H):
    tmp = 0
    # 順方向(左にどれだけ伸びてるか)
    for j in range(W):
        if S[i][j] == '#':
            tmp = 0
        else:
            tmp += 1
            Ls[i][j] = tmp
    tmp = 0
    # 逆方向(右にどれだけ伸びてるか)
    for j in reversed(range(W)):
        if S[i][j] == '#':
            tmp = 0
        else:
            tmp += 1
            Rs[i][j] = tmp

# 行列転置 順番逆にするだけ
for j in range(W):
    tmp = 0
    # 順方向 上にどれだけ伸びているか
    for i in range(H):
        if S[i][j] == '#':
            tmp = 0
        else:
            tmp += 1
            Us[i][j] = tmp
    tmp = 0
    # 順方向 下にどれだけ伸びているか
    for i in reversed(range(H)):
        if S[i][j] == '#':
            tmp = 0
        else:
            tmp += 1
            Ds[i][j] = tmp

ans = 0
for i in range(H):
    for j in range(W):
        if S[i][j] == '#': 
            continue
        # 上下左右に伸びた数を重なっている部分引く
        tmp = Rs[i][j] + Ls[i][j] + Ds[i][j] + Us[i][j] - 3
        ans = max(ans, tmp)
print(ans)
"""

# その2
"""
左・上からゴリゴリ攻めていくパターン
一回通ったかどうかのフラグが必要
"""

cnt = [[0] * (W+1) for i in range(H+1)]
# 横
for i in range(H):
    done = [False] * (W+1)
    for j in range(W):
        l = 0
        if done[j]: continue
        if S[i][j] == ".":
            while j+l < W:
                if S[i][j+l] == ".":
                    l += 1
                else:
                    break
        for k in range(j,j+l):
            cnt[i][k] += l
            done[k] = True

# 縦
for j in range(W):
    done = [False] * (H+1)
    for i in range(H):
        l = 0
        if done[i]: continue
        if S[i][j] == ".":
            while i+l < H:
                if S[i+l][j] == ".":
                    l += 1
                else:
                    break
        for k in range(i,i+l):
            cnt[k][j] += l
            done[k] = True

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

Submission status: Accepted

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

h, w = [ int(v) for v in input().split() ]

field = [ list(map(int,input().replace("#","0").replace(".","1").replace("\n",""))) for i in range(h)]

yoko = [ copy.deepcopy(field[i]) for i in range(h)]


for i in range(h):
	for j in range(1,w):
		if yoko[i][j] == 0:
			yoko[i][j] = 0
		else:
			yoko[i][j] += yoko[i][j-1]
	for j in range(w-1,0,-1):
		if yoko[i][j-1] != 0:
			yoko[i][j-1] = max(yoko[i][j-1],yoko[i][j])

tate = [ [ field[i][j] for i in range(h)] for j in range(w) ]

for i in range(w):
	for j in range(1,h):
		if tate[i][j] == 0:
			tate[i][j] = 0
		else:
			tate[i][j] += tate[i][j-1]
	for j in range(h-1,0,-1):
		if tate[i][j-1] != 0:
			tate[i][j-1] = max(tate[i][j-1],tate[i][j])


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

print(ans-1)

Submission status: Accepted

Solution code:
import fractions #最小公倍数などはこっち
import math
import sys

mod = 10**9+7
sys.setrecursionlimit(mod) # 再帰回数上限はでdefault1000

H,W = map(int,input().split())
S = [[] for i in range(H)]
for i in range(H):
    S[i] = list(str(input()))

lights = [[(0,0) for j in range(W)] for i in range(H)]

def count_ver(i,j):
    cnt = 0
    for x in range(i,W):
        if S[j][x] == ".":
            cnt += 1
        else:
            break
    for x in range(i,i+cnt):
        ver,hor = lights[j][x]
        lights[j][x] = (cnt,hor)
    return cnt


def count_hor(i,j):
    cnt = 0
    for y in range(j,H):
        if S[y][i] == ".":
            cnt += 1
        else:
            break
    for y in range(j,j+cnt):
        ver,hor = lights[y][i]
        lights[y][i] = (ver,cnt)
    return cnt

ans = -1
for i in range(H):
    for j in range(W):
        if S[i][j] == ".":
            ver,hor = lights[i][j]
            if ver == 0:
                ver = count_ver(j,i)
            if hor == 0:
                hor = count_hor(j,i)
            ans = max(ans,hor+ver-1)

print(ans)

Submission status: Accepted

Solution code:
H,W = map(int,input().split())
List=[input() for i in range(H)]
A =[[] for i in range(H)]
B =[[] for i in range(W)]
for i in range(H):
    k = 0
    a = 0
    while k < W:
        if List[i][k]==".":
            a += 1
            k = k+1
        else:
            for j in range(a):
                A[i].append(a)
            A[i].append(0)
            a = 0
            k = k+1
    for j in range(a):
                A[i].append(a)
for i in range(W):
    k = 0
    a = 0
    while k < H:
        if List[k][i]==".":
            a += 1
            k = k+1
        else:
            for j in range(a):
                B[i].append(a)
            B[i].append(0)
            a = 0
            k = k+1
    for j in range(a):
                B[i].append(a)
max = 0
for i in range(H):
    for j in range(W):
        if max < A[i][j] + B[j][i]:
            max = A[i][j] + B[j][i]
print(max-1)

Submission status: Accepted

Solution code:
H,W = map(int,input().split())
List=[input() for i in range(H)]
A =[[1]*(W) for i in range(H)]
B =[[1]*(H) for i in range(W)]
for i in range(H):
    k = 0
    a = 0
    while k < W:
        if List[i][k]==".":
            a += 1
            k = k+1
        else:
            for j in range(a):
                A[i][k-j-1]=a
            A[i][k]=a
            a = 0
            k = k+1
    for j in range(a):
                A[i][k-j-1]=a
for i in range(W):
    k = 0
    a = 0
    while k < H:
        if List[k][i]==".":
            a += 1
            k = k+1
        else:
            for j in range(a):
                B[i][k-j-1]=a
            B[i][k] = 0
            a = 0
            k = k+1
    for j in range(a):
                B[i][k-j-1]= a
max = 0
for i in range(H):
    for j in range(W):
        if max < A[i][j] + B[j][i]:
            max = A[i][j] + B[j][i]
print(max-1)