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:
MOD = int(1e9) + 7

def main():
    buf = input()
    buflist = buf.split()
    H = int(buflist[0])
    W = int(buflist[1])
    S = []
    for i in range(H):
        buf = input()
        S.append(buf)
    lightmap_h = []
    for i in range(H):
        lightmap_h.append([])
        for j in range(W):
            lightmap_h[i].append(0)
    lightmap_v = []
    for j in range(H):
        lightmap_v.append([])
        for i in range(W):
            lightmap_v[j].append(0)
    for i in range(H):
        light_count = 0
        last_pointer = 0
        for j in range(W):
            if S[i][j] == '.':
                light_count += 1
            else:
                for k in range(last_pointer, j):
                    lightmap_h[i][k] = light_count
                light_count = 0
                last_pointer = j+1
        for k in range(last_pointer, W):
            lightmap_h[i][k] = light_count
    for j in range(W):
        light_count = 0
        last_pointer = 0
        for i in range(H):
            if S[i][j] == '.':
                light_count += 1
            else:
                for k in range(last_pointer, i):
                    lightmap_v[k][j] = light_count
                light_count = 0
                last_pointer = i+1
        for k in range(last_pointer, H):
            lightmap_v[k][j] = light_count
    maximum = 0
    for i in range(H):
        for j in range(W):
            hor = lightmap_h[i][j]
            vert = lightmap_v[i][j]
            if hor == 0 or vert == 0:
                continue
            light_distance = hor + vert - 1
            if light_distance > maximum:
                maximum = light_distance
    print(maximum)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
S = [list(input()) for h in range(H)]
count_matw = [[0 for w in range(W)]for h in range(H)]
count_math = [[0 for w in range(W)]for h in range(H)]

for h in range(H):
    count = 0
    for w in range(W):
        if '#' == S[h][w]:
            count = 0
        else:
            count += 1
        count_matw[h][w] = count
    for w in range(W-1, 0, -1):
        if count_matw[h][w-1] == 0:
            continue
        count_matw[h][w-1] = max(count_matw[h][w], count_matw[h][w-1])

for w in range(W):
    count = 0
    for h in range(H):
        if '#' == S[h][w]:
            count = 0
        else:
            count += 1
        count_math[h][w] = count
    for h in range(H-1, 0, -1):
        if count_math[h-1][w] == 0:
            continue
        count_math[h-1][w] = max(count_math[h][w], count_math[h-1][w])
ans = 1
for h in range(H):
    for w in range(W):
        ans = max(ans, count_math[h][w] + count_matw[h][w])
# ans = max([count_math[h][w] + count_matw[h][w] for h in range(H) for w in range(W)])
# print(count_math)
# print(count_matw)
print(ans-1)

Submission status: Accepted

Solution code:
import numpy as np
import sys

H, W = list(map(int, sys.stdin.readline().split()))
S = np.array([list(sys.stdin.readline().rstrip()) for _ in range(H)]) == '.'
ups = np.zeros((H, W), dtype=int)
downs = np.zeros((H, W), dtype=int)
rights = np.zeros((H, W), dtype=int)
lefts = np.zeros((H, W), dtype=int)

for h in range(1, H):
    ups[h] = (ups[h - 1] + 1) * S[h - 1]
    downs[H - h - 1] = (downs[H - h] + 1) * S[H - h]
for w in range(1, W):
    lefts[:, w] = (lefts[:, w - 1] + 1) * S[:, w - 1]
    rights[:, W - w - 1] = (rights[:, W - w] + 1) * S[:, W - w]
print(((ups + downs + lefts + rights) * S).max() + 1)

Submission status: Accepted

Solution code:
H,W = list(map(int, input().split()))
S = [[0 if s=='#' else 1 for s in input()] for _ in range(H)] #S[h][w]

SH = [[0]*W for _ in range(H)]
SH_ = [[0]*W for _ in range(H)]

SV = [[0]*W for _ in range(H)]
SV_ = [[0]*W for _ in range(H)]

#horizontal
for h in range(H):
  SH[h][0] = S[h][0]
  for w in range(1,W):
    if S[h][w]:
      SH[h][w] = SH[h][w-1] + 1
  
  SH_[h][-1] = S[h][-1]
  for w in range(W-1)[::-1]:
    if S[h][w]:
      SH_[h][w] = SH_[h][w+1] + 1
  

#vertical
for w in range(W):
  SV[0][w] = S[0][w]
  for h in range(1,H):
    if S[h][w]:
      SV[h][w] = SV[h-1][w] + 1
  
  SV_[-1][w] = S[-1][w]
  for h in range(H-1)[::-1]:
    if S[h][w]:
      SV_[h][w] = SV_[h+1][w] + 1

#print(SH + SH_)
#print(SV + SV_)

ans = 0
for h in range(H):
  for w in range(W):
    ans = max(ans, SH[h][w] + SH_[h][w] + SV[h][w] + SV_[h][w] -3)

print(ans)

Submission status: Accepted

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

w_score = [[0]*W for _ in range(H)]
h_score = [[0]*W for _ in range(H)]

begin = -1
streak = 1
for hi in range(H):
    for wi in range(W):
        if begin == -1 and S[hi][wi]=='.':
            begin = wi
        elif begin != -1 and S[hi][wi] == '.':
            streak += 1
        if wi ==W-1 or(begin != -1 and S[hi][wi]=='#'):
            if wi == W-1 and S[hi][wi]=='.':
                w_score[hi][wi]=streak
            for i in range(W)[begin:wi]:
                w_score[hi][i] = streak
            streak = 1
            begin = -1

# for s in w_score:
#     print(s)
# print('-'*10)

begin = -1
streak = 1
for wi in range(W):
    for hi in range(H):
        if begin == -1 and S[hi][wi]=='.':
            begin = hi
        elif begin != -1 and S[hi][wi] == '.':
            streak += 1
        if hi ==H-1 or(begin != -1 and S[hi][wi]=='#'):
            if hi == H-1 and S[hi][wi]=='.':
                h_score[hi][wi]= streak
            for i in range(H)[begin:hi]:
                h_score[i][wi] = streak
            streak = 1
            begin = -1

# for s in h_score:
#     print(s)
# print('-'*10)

ans = 0
score = [[0]*W for _ in range(H)]
for hi in range(H):
    for wi in range(W):
        w =w_score[hi][wi] 
        h= h_score[hi][wi] 
        score_ = w+h
        score_ += -1 if w>0 and h>0 else 0
        ans = max(ans,score_)
        score[hi][wi] = score_
        
# for s in score:
#     print(s)

print(ans)

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
S = [list(input()) for h in range(H)]
count_matw = [[0 for w in range(W)]for h in range(H)]
count_math = [[0 for w in range(W)]for h in range(H)]

for h in range(H):
    count = 0
    for w in range(W):
        if '#' == S[h][w]:
            count = 0
        else:
            count += 1
        count_matw[h][w] = count
    for w in range(W-1, 0, -1):
        if count_matw[h][w-1] == 0:
            continue
        count_matw[h][w-1] = max(count_matw[h][w], count_matw[h][w-1])

for w in range(W):
    count = 0
    for h in range(H):
        if '#' == S[h][w]:
            count = 0
        else:
            count += 1
        count_math[h][w] = count
    for h in range(H-1, 0, -1):
        if count_math[h-1][w] == 0:
            continue
        count_math[h-1][w] = max(count_math[h][w], count_math[h-1][w])
ans = 1
# for h in range(H):
#     for w in range(W):
#         ans = max(ans, count_math[h][w] + count_matw[h][w])
ans = max([count_math[h][w] + count_matw[h][w] for h in range(H) for w in range(W)])
# print(count_math)
# print(count_matw)
print(ans-1)

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
s = [input() for i in range(h)]
yoko = [([0] * w) for i in range(h)]
tate = [([0] * w) for i in range(h)]
 
for i in range(h):
    f = 0
    j = 0
    m = 0
    while j < w:
        if s[i][j] == ".":
            if m == 0:
                j += 1
            else:
                f = j
                m = 0
                j += 1
        else:
            if m == 0:
                for p in range(f, j):
                    yoko[i][p] = j - f
            m = 1
            j += 1
    if m == 0:
        for p in range(f, j):
            yoko[i][p] = j - f
 
for i in range(w):
    f = 0
    j = 0
    m = 0
    while j < h:
        if s[j][i] == ".":
            if m == 0:
                j += 1
            else:
                f = j
                m = 0
                j += 1
        else:
            if m == 0:
                for p in range(f, j):
                    tate[p][i] = j - f
            m = 1
            j += 1
    if m == 0:
        for p in range(f, j):
            tate[p][i] = j - f
ans = 0
for i in range(h):
    for j in range(w):
        q = yoko[i][j] + tate[i][j] - 1
        if q > ans:
            ans = q
print(ans)

Submission status: Accepted

Solution code:
H,W = map(int,input().split())
S = [list(input()) for i in range(H)]
dpyoko = [[1]*(W) for i in range(H)]
for j in range(W):
    for i in range(H):
        if S[i][j] == '#':
            dpyoko[i][j] = 0
        else:
            if j != 0:
                dpyoko[i][j] += dpyoko[i][j-1]
for j in range(W-1,-1,-1):
    for i in range(H):
        if dpyoko[i][j] == 0:
            continue
        if j == W-1:
            continue
        dpyoko[i][j] = max(dpyoko[i][j],dpyoko[i][j+1])
dptate = [[1]*(W) for i in range(H)]
for i in range(H):
    for j in range(W):
        if S[i][j] == '#':
            dptate[i][j] = 0
        else:
            if i != 0:
                dptate[i][j] += dptate[i-1][j]
for i in range(H-1,-1,-1):
    for j in range(W):
        if dptate[i][j] == 0:
            continue
        if i == H-1:
            continue
        dptate[i][j] = max(dptate[i][j],dptate[i+1][j])
res = 0
for i in range(H):
    for j in range(W):
        res = max(res,dptate[i][j]+dpyoko[i][j]-1)
print(res)

Submission status: Accepted

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

G_jouge = []
G_sayu = []
for h in range(H):
    G_migi = []
    G_hidari = []
    r = 0
    l = 0
    for w in range(W):
        if S[h][w] == '.':
            G_migi.append(r)
            r += 1
        else:
            r = 0
            G_migi.append(r)
        if S[h][W-1-w] == '.':
            G_hidari.append(l)
            l += 1
        else:
            l = 0
            G_hidari.append(l)
    G_sayu.append([G_hidari[W-1-i]+G_migi[i] for i in range(W)])
for w in range(W):
    G_migi = []
    G_hidari = []
    r = 0
    l = 0
    for h in range(H):
        if S[h][w] == '.':
            G_migi.append(r)
            r += 1
        else:
            r = 0
            G_migi.append(r)
        if S[H-1-h][w] == '.':
            G_hidari.append(l)
            l += 1
        else:
            l = 0
            G_hidari.append(l)
    G_jouge.append([G_hidari[H-1-i]+G_migi[i] for i in range(H)])

ans = 0
for h in range(H):
    for w in range(W):
        ans = max(ans,G_sayu[h][w]+G_jouge[w][h])
print(ans+1)

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
s=[input() for i in range(h)]
c=[[0 for i in range(w)] for i in range(h)]
t=0
for H in range(h):
    for W in range(w):
        if(s[H][W]=="#"):
            for l in range(1,t+1):
                c[H][W-l]=t
            t=0
        else:
            t+=1
    for l in range(1,t+1):
        c[H][-l]=t
    t=0
ans=0
for W in range(w):
    for H in range(h):
        if(s[H][W]=="#"):
            for l in range(1,t+1):
                ans=max(c[H-l][W]+t,ans)
            t=0
        else:
            t+=1
    for l in range(1,t+1):
        ans=max(c[-l][W]+t,ans)
    t=0
print(max(ans-1,0))

Submission status: Accepted

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

# h, w = 4, 6
# s = ["#..#..",".....#","....#.","#.#..."]
s_ = [list(x) for x in zip(*s)]

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

for j,str in enumerate(s):
    count = 0
    col = 0
    for i, eachstr in enumerate(str):
        if eachstr == "#":
            if i == 0:
                pass
            else:
                for k in range(col,i):
                    ho[j][k] = count
            col = i + 1
            count = 0
        else:
            count += 1
    else:
        if col < w:
            for k in range(col,i+1):
                ho[j][k] = count

for j,str in enumerate(s_):
    count = 0
    col = 0
    for i, eachstr in enumerate(str):
        if eachstr == "#":
            if i == 0:
                pass
            else:
                for k in range(col,i):
                    he[j][k] = count
            col = i + 1
            count = 0
        else:
            count += 1
    else:
        if col < w:
            for k in range(col,i+1):
                he[j][k] = count

he_ = [list(x) for x in zip(*he)]

ans = 0
for i,j in zip(ho,he_):
    for ss, tt in zip(i,j):
        ans = max(ans, ss+tt)
print(ans-1)

Submission status: Accepted

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

map_=[[0]*W for _ in range(H)]
for i in range(H):
    cnt=0
    q=[]
    for j in range(W+1):
        if S[i][j]==False and cnt!=0:
            for Q in q:
                map_[Q[0]][Q[1]]=cnt
            cnt=0
            q=[]
        elif S[i][j]:
            cnt+=1
            q.append([i,j])
for j in range(W):
    cnt=0
    q=[]
    for i in range(H+1):
        if S[i][j]==False and cnt!=0:
            for Q in q:
                map_[Q[0]][Q[1]]+=cnt
            cnt=0
            q=[]
        elif S[i][j]:
            cnt+=1
            q.append([i,j])
print(max([max(i) for i in map_])-1)

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
S = [input() 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 j == 0 and S[i][0] == '.':
            l[i][j] = 1
        elif S[i][j] == '.':
            l[i][j] = l[i][j-1] + 1
    for j in range(W-2, -1, -1):
        if S[i][j] == '.' and S[i][j+1] == '.':
            r[i][j] = r[i][j+1] + 1
for j in range(W):
    for i in range(H):
        if i == 0 and S[0][j] == '.':
            u[i][j] = 1
        elif S[i][j] == '.':
            u[i][j] = u[i-1][j] + 1
    for i in range(H-2, -1, -1):
        if S[i][j] == '.' and S[i+1][j] == '.':
            d[i][j] = d[i+1][j] + 1
m = 0
for i in range(H):
    for j in range(W):
        tmp = l[i][j] + r[i][j] + u[i][j] + d[i][j] - 1
        if tmp > m:
            m = tmp
print(m)

Submission status: Accepted

Solution code:
#!/usr/bin/env pypy
from __future__ import division, print_function

import os
import sys
from __builtin__ import xrange as range
from cStringIO import StringIO
from future_builtins import ascii, filter, hex, map, oct, zip
from io import IOBase

import __pypy__


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

    hor = [[0] * (w + 1) for _ in range(h)]
    ver = [[0] * (h + 1) for _ in range(w)]

    for i, row in enumerate(mat):
        hi = hor[i]
        for j in range(w):
            if row[j] == '.':
                hi[j + 1] = hi[j] + 1
            else:
                hi[j + 1] = 0
        for j in reversed(range(w)):
            if hi[j]:
                hi[j] = max(hi[j], hi[j + 1])
        del hi[0]

    for i, col in enumerate(zip(*mat)):
        vi = ver[i]
        for j in range(h):
            if col[j] == '.':
                vi[j + 1] = vi[j] + 1
            else:
                vi[j + 1] = 0
        for j in reversed(range(h)):
            if vi[j]:
                vi[j] = max(vi[j], vi[j + 1])
        del vi[0]

    ver = list(zip(*ver))

    res = 0
    for i in range(h):
        hi = hor[i]
        vi = ver[i]
        for j in range(w):
            res = max(res, hi[j] + vi[j] - 1)

    print(res)


# region fastio

BUFSIZE = 8192


class FastI(IOBase):
    def __init__(self, file):
        self._fd = file.fileno()
        self._buffer = StringIO()
        self.newlines = 0

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count("\n") + (not b)
            ptr = self._buffer.tell()
            self._buffer.seek(0, 2), self._buffer.write(b), self._buffer.seek(ptr)
        self.newlines -= 1
        return self._buffer.readline()


class FastO(IOBase):
    def __init__(self, file):
        self._fd = file.fileno()
        self._buffer = __pypy__.builders.StringBuilder()
        self.write = lambda s: self._buffer.append(s)

    def flush(self):
        os.write(self._fd, self._buffer.build())
        self._buffer = __pypy__.builders.StringBuilder()


def print(*args, **kwargs):
    sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout)
    at_start = True
    for x in args:
        if not at_start:
            file.write(sep)
        file.write(str(x))
        at_start = False
    file.write(kwargs.pop("end", "\n"))
    if kwargs.pop("flush", False):
        file.flush()


sys.stdin, sys.stdout = FastI(sys.stdin), FastO(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

# endregion

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
H, W = map(int, input().split())

room = [[True if c == "." else False for c in input()] for _ in range(H)]

count = []

for i in range(H):
    count.append([0] * W)

for row in range(H):
    start = 0
    for col in range(W):
        if not room[row][col]:
            if col != 0:
                plus = col - start
                for i in range(start, col):
                    count[row][i] += plus
            start = col + 1
    if room[row][W - 1]:
        plus = W - start
        for i in range(start, W):
            count [row][i] += plus

for col in range(W):
    start = 0
    for row in range(H):
        if not room[row][col]:
            if row != 0:
                plus = row - start
                for i in range(start, row):
                    count[i][col] += plus
            start = row + 1
    if room[H - 1][col]:
        plus = H - start
        for i in range(start, H):
            count[i][col] += plus

ans = 0

for rowline in count:
    ans = max(max(rowline), ans)

print((ans - 1))

Submission status: Accepted

Solution code:
H,W = (int(i) for i in input().split())
s = [[-1] * (W + 2) for i in range(H+2)]
t = [[-1] * (W + 2) for i in range(H+2)]

for i in range(1, H+1):
    SS = input()
    for j in range(W):
        if SS[j] == "#":
            s[i][j+1] = -1
            t[i][j+1] = -1
        else:
            s[i][j+1] = 0
            t[i][j+1] = 0

tmp = []
num = 0
for i in range(1, H+1):
    for j in range(1, W+2):
        if s[i][j] == -1 :
            if num != 0:
                for tp in tmp:
                    s[i][tp] = num
                tmp = []
                num =0
        else:
            tmp.append(j)
            num += 1

tmp = []
num = 0
for j in range(1, W+1):
    for i in range(1, H+2):
        if t[i][j] == -1 :
            if num != 0:
                for tp in tmp:
                    t[tp][j] = num
                tmp = []
                num =0
        else:
            tmp.append(i)
            num += 1

ref = 0
for i in range(H+2):
    for j in range(W+2):
        ref = max(s[i][j] + t[i][j], ref)

print(ref-1)

Submission status: Accepted

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

table = []
table.append(["#" for _ in range(w+2)])
for i in range(h):
    s = input()
    table.append(["#"]+list(s)+["#"])
table.append(["#" for _ in range(w+2)])

# yoko = []#[None for _ in range(w)] for _ in range(h)]
yoko_cnt = []
for i in range(1,h+1):
    cnt = 0
#     start = 0
    yoko = []
    for j in range(1,w+2):
        if table[i][j] == "#":
            if cnt != 0:
                yoko.append(cnt)
            yoko.append(0)
            cnt = 0
            continue
        else:
            cnt += 1
    yoko_cnt.append(yoko[:-1])

# yoko = []#[None for _ in range(w)] for _ in range(h)]
tate_cnt = []
for i in range(1,w+1):
    cnt = 0
#     start = 0
    tate = []
    for j in range(1,h+2):
        if table[j][i] == "#":
            if cnt != 0:
                tate.append(cnt)
            tate.append(0)
            cnt = 0
            continue
        else:
            cnt += 1
    tate_cnt.append(tate[:-1])

yoko_cnt_mod = []
for l in yoko_cnt:
    hoge = []
    for i in l:
        if i == 0:
            hoge.append(0)
        else:
            hoge.extend([i for _ in range(i)])
    yoko_cnt_mod.append(hoge)
# yoko_cnt_mod = [[0 if i == 0 else i for _ in range(i) for i in l] for l in yoko_cnt  ]

tate_cnt_mod = []
for l in tate_cnt:
    hoge = []
    for i in l:
        if i == 0:
            hoge.append(0)
        else:
            hoge.extend([i for _ in range(i)])
    tate_cnt_mod.append(hoge)
# yoko_cnt_mod = [[0 if i == 0 else i for _ in range(i) for i in l] for l in yoko_cnt  ]

result = 0
for i in range(h):
    for j in range(w):
        tt = tate_cnt_mod[j][i]
        yy = yoko_cnt_mod[i][j]
        
        if tt == 0 or yy ==0:
            continue
        else:
            c = tt + yy - 1
            if result < c:
                result = c
                
print(result)

Submission status: Accepted

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

for h in range(H):
  cnt = 0
  for w in range(W):
    if grid[h][w] == "#":
      cnt = 0
    else:
      cnt += 1
      cntW[h][w] = cnt

for h in range(H-1,-1,-1):
  cnt = 0
  for w in range(W-1,-1,-1):
    if grid[h][w] == "#":
      cnt = 0
    elif cnt == 0:
      cnt = cntW[h][w]
    else:
      cntW[h][w] = cnt

for w in range(W):
  cnt = 0
  for h in range(H):
    if grid[h][w] == "#":
      cnt = 0
    else:
      cnt += 1
      cntH[h][w] = cnt

for w in range(W-1,-1,-1):
  cnt = 0
  for h in range(H-1,-1,-1):
    if grid[h][w] == "#":
      cnt = 0
    elif cnt == 0:
      cnt = cntH[h][w]
    else:
      cntH[h][w] = cnt

ans = 0
for h in range(H):
  for w in range(W):
    ans = max(ans,cntW[h][w]+cntH[h][w])

print(ans-1)

Submission status: Accepted

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

memo_yoko1 = [[-1 for i in range(w)] for j in range(h)]
memo_tate1 = [[-1 for i in range(w)] for j in range(h)]

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

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

# transpose
s_t = [[''] for j in range(h)]
for i in range(h):
    s_t[i] = s[h - i - 1][0][::-1]

memo_yoko2 = [[-1 for i in range(w)] for j in range(h)]
memo_tate2 = [[-1 for i in range(w)] for j in range(h)]

for i in range(h):
    for j in range(w):
        if s_t[i][j] == '.':
            if j == 0:
                memo_yoko2[i][j] = 1
            else:
                memo_yoko2[i][j] = memo_yoko2[i][j-1] + 1

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


memo = [[-1 for i in range(w)] for j in range(h)]
ans = -1
for i in range(h):
    for j in range(w):
        _memo = memo_tate1[i][j] + memo_tate2[h - i - 1][w - j - 1] + memo_yoko1[i][j] + memo_yoko2[h - i - 1][w - j - 1] - 3
        memo[i][j] = _memo

        ans = max(ans, _memo)

print(ans)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**7)
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return sys.stdin.readline().strip()
INF = 10 ** 18
MOD = 10 ** 9 + 7

def main():
    H, W = LI()
    S = []
    for _ in range(H):
        S.append(SI())

    # print(S)

    # 横方向に探索
    yoko = [ [-1]*W for _ in range(H)]
    for i in range(H):
        beginning = True
        continuous = 0
        for j in range(W):
            if beginning:
                if S[i][j] == '#':
                    continue
                if S[i][j] == '.':
                    beginning = False
                    continuous += 1
            else:
                if S[i][j] == '#':
                    for k in range(j-continuous, j):
                        yoko[i][k] = continuous
                    beginning = True
                    continuous = 0
                if S[i][j] == '.':
                    continuous += 1
        if continuous:
            for k in range(W-continuous, W):
                yoko[i][k] = continuous

    # 縦方向に探索
    tate = [ [-1]*W for _ in range(H)]
    for j in range(W):
        beginning = True
        continuous = 0
        for i in range(H):
            if beginning:
                if S[i][j] == '#':
                    continue
                if S[i][j] == '.':
                    beginning = False
                    continuous += 1
            else:
                if S[i][j] == '#':
                    for k in range(i-continuous, i):
                        tate[k][j] = continuous
                    beginning = True
                    continuous = 0
                if S[i][j] == '.':
                    continuous += 1
        if continuous:
            for k in range(H-continuous, H):
                tate[k][j] = continuous

    ans = 1  # ぜったい1マスは光る。
    for i in range(H):
        for j in range(W):
            ans = max(ans, tate[i][j] + yoko[i][j] - 1)

    print(ans)

main()

Submission status: Accepted

Solution code:
#-*- coding:utf-8 -*-

# def print_map(mp):
#     for r in mp:
#         print(['{:2d}'.format(n) for n in r])

H, W = map(int, input().split())
mp = [[] for h in range(H)]
mp_row = [[0 for w in range(W)] for h in range(H)]
mp_col = [[0 for w in range(W)] for h in range(H)]

# True = なし、 False = 障害物
for h in range(H):
    mp[h] = [True if c == "." else False for c in input()]

# 全探索はO(HWHW)となって爆発。
# 縦横でまずカウントするのがよさげ。
# このプログラムはO(HW)だがTLE: カウントしたものの書き込みでO(HHW+HWW)あたりになっている？
# これpythonのforの遅さでTLEしてないか…？

## 横のパネル数
for h in range(H):
    st = 0
    cnt = 0
    for w in range(W):
        if mp[h][w] is False:
            for i in range(st, w):
                mp_row[h][i] = cnt
            cnt = 0
            st = w+1
        elif w == W-1:
            cnt += 1
            for i in range(st, W):
                mp_row[h][i] = cnt
        else:
            cnt += 1

## 縦のパネル数
for w in range(W):
    st = 0
    cnt = 0
    for h in range(H):
        if mp[h][w] is False:
            for i in range(st, h):
                mp_col[i][w] = cnt
            cnt = 0
            st = h+1
        elif h == H-1:
            cnt += 1
            for i in range(st, H):
                mp_col[i][w] = cnt
        else:
            cnt += 1

#print_map(mp_row)
#print()
#print_map(mp_col)

mx = 0
for h in range(H):
    for w in range(W):
        # 重なっている中央を引くのを忘れずに
        mx = max(mx, mp_row[h][w]+mp_col[h][w]-1)
print(mx)

Submission status: Accepted

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

g = [".", "#"]
HL = [[0] * W for _ in range(H)]
HR = [[0] * W for _ in range(H)]

for i in range(H):
  l = 0
  r = 0
  for j in range(W):
    if S[i][j] == g[0]:
      l += 1
    else:
      l = 0
    HL[i][j] = l
    if S[i][W - 1 - j] == g[0]:
      r += 1
    else:
      r = 0
    HR[i][W - 1 - j] = r

WU = [[0] * W for _ in range(H)]
WD = [[0] * W for _ in range(H)]

for i in range(W):
  u = 0
  d = 0
  for j in range(H):
    if S[j][i] == g[0]:
      u += 1
    else:
      u = 0
    WU[j][i] = u
    if S[H - j - 1][i] == g[0]:
      d += 1
    else:
      d = 0
    WD[H - j - 1][i] = d

ans = 0

for i in range(H):
  for j in range(W):
    k = HL[i][j] + HR[i][j] + WU[i][j] + WD[i][j] - 3
    ans = max(ans, k)

print(ans)

Submission status: Accepted

Solution code:
import sys
input=sys.stdin.readline
h,w=map(int,input().split())
S=[list(input().strip()) for _ in range(h)]
A=[[0]*(w+2) for _ in range(h+2)]
for i,s in enumerate(S):
    for j,t in enumerate(s):
        if t=='.':
            A[i+1][j+1]=A[i+1][j]+1
    for k in reversed(range(w+1)):
        if A[i+1][k]==0:
            continue
        A[i+1][k]=max(A[i+1][k],A[i+1][k+1])
St=list(map(list,zip(*S)))
B=[[0]*(h+2) for _ in range(w+2)]
for i,s in enumerate(St):
    for j,t in enumerate(s):
        if t=='.':
            B[i+1][j+1]=B[i+1][j]+1
    for k in reversed(range(h+1)):
        if B[i+1][k]==0:
            continue
        B[i+1][k]=max(B[i+1][k],B[i+1][k+1])
B=list(map(list,zip(*B)))
ans=0
for i in range(h+2):
    for j in range(w+2):
        ans=max(ans,A[i][j]+B[i][j]-1)
print(ans)