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:
#!/usr/bin/env python
import sys


def solve(H: int, W: int, S: "List[str]"):
    def isBlock(x, y):
        return S[y][x] == "#"
    cacheW = [[0 for i in range(W)] for j in range(H)]
    cacheH = [[0 for i in range(W)] for j in range(H)]
    for i in range(H):
        count = 0
        begin = 0
        for j in range(W):
            if isBlock(j, i):
                for k in range(begin, j):
                    cacheW[i][k] = count
                begin = j+1
                count = 0
            else:
                count += 1
        if begin != W:
            for k in range(begin, W):
                cacheW[i][k] = count
    for i2 in range(W):
        count2 = 0
        begin2 = 0
        for j2 in range(H):
            if isBlock(i2, j2):
                for k2 in range(begin2, j2):
                    cacheH[k2][i2] = count2
                # print("x="+str(i2) +"--"+ str(begin2)+"~"+str(j2)+"::"+str(count2))
                begin2 = j2+1
                count2 = 0
            else:
                count2 += 1
        if begin2 != H:
            for k in range(begin2,H):
                cacheH[k][i2] = count2
    m = 0
    # print(cacheW)
    # print(cacheH)
    for i in range(W):
        for j in range(H):
            c = cacheW[j][i] + cacheH[j][i]-1
            m = max(m, c)
            # print(str(i)+"-"+str(j)+":"+str(c))
    print(m)
    return


# Generated by 1.1.6 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 -*-
h,w = list(map(int,input().split()))
ss = ["#"*(w+2)]
for i in range(h):
    ss.append("#" + input()+"#")
ss.append("#"*(w+2))

yoko_list = []
tate_list = []
for s in ss:
    w_l_len_list = []
    w_l_len = 0
    for i,v in enumerate(s):
        if v == "#":
            w_l_len_list+=[w_l_len] * w_l_len
            w_l_len_list+=[0]
            w_l_len = 0
        else:
            w_l_len+=1
    yoko_list.append(w_l_len_list)
for s in zip(*ss):
    w_l_len_list = []
    w_l_len = 0
    for i,v in enumerate(s):
        if v == "#":
            w_l_len_list+=[w_l_len] * w_l_len
            w_l_len_list+=[0]
            w_l_len = 0
        else:
            w_l_len+=1
    tate_list.append(w_l_len_list)
ret = 0
for i in range(h+1):
    for j in range(w+1):
        ret = max(ret,tate_list[j][i]+yoko_list[i][j]-1)
print(ret)

Submission status: Accepted

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

x = [[0 for _ in range(w)] for _ in range(h)]
y = [[0 for _ in range(w)] for _ in range(h)]

#横軸
for i in range(h):
    index = []
    l = []
    flag = True
    for j in range(w):
        if s[i][j] == "#":
            x[i][j] = -1
            flag = False
            if len(l) != 0:
                index.append(l)
                l = []
        else:
            flag = True
        if flag:
            l.append(j)
        if j == w - 1:
          if len(l) != 0:
              index.append(l)
    for j in index:
        for m in j:
            x[i][m] = len(j)

#縦軸
for j in range(w):
    index = []
    l = []
    flag = True
    for i in range(h):
        if s[i][j] == "#":
            y[i][j] = -1
            flag = False
            if len(l) != 0:
                index.append(l)
                l = []
        else:
            flag = True
        if flag:
            l.append(i)
        if i == h - 1:
          if len(l) != 0:
              index.append(l)
    for i in index:
        for m in i:
            y[m][j] = len(i)

ans=0

for i in range(h):
    for j in range(w):
        ans = max(ans,x[i][j] + y[i][j] - 1)

print(ans)

Submission status: Accepted

Solution code:
import numpy as np
import sys
buf = sys.stdin.buffer

h,w = map(int,buf.readline().split())
grid = np.array([list(input()) for _ in range(h)])
grid = np.where(grid == ".", 1, 0)
#print(grid)

L,R,U,D = [np.zeros((h,w)) for _ in range(4)]
for i in range(w):
  L[:,i] = (L[:,i-1] + 1) * grid[:,i]
  R[:,-i-1] = (R[:,-i] + 1) * grid[:,-i-1]
  
for i in range(h):
  D[i,:] = (D[i-1,:] + 1) * grid[i,:]
  U[-i-1,:] = (U[-i,:] + 1) * grid[-i-1,:]

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

Submission status: Accepted

Solution code:
import sys
import numpy as np
sys.setrecursionlimit(10**9)
H, W = map(int, input().split())
S = [list('#' * (W + 2))]
for _ in range(H):
    S.append(list('#' + input() + '#'))
S.append(list('#' * (W + 2)))
S = np.array(S)
# print(S)

north = np.zeros_like(S, dtype='int')
for w in range(1, W + 1):
    north[:, w] = north[:, w - 1] + 1
    north[:, w][S[:, w - 1] == '#'] = 0
    north[:, w][S[:, w] == '#'] = 0

south = np.zeros_like(S, dtype='int')
for w in range(W, 0, -1):
    south[:, w] = south[:, w + 1] + 1
    south[:, w][S[:, w + 1] == '#'] = 0
    south[:, w][S[:, w] == '#'] = 0

east = np.zeros_like(S, dtype='int')
for h in range(1, H + 1):
    east[h, :] = east[h - 1, :] + 1
    east[h, :][S[h - 1] == '#'] = 0
    east[h, :][S[h, :] == '#'] = 0

west = np.zeros_like(S, dtype='int')
for h in range(H, 0, -1):
    west[h, :] = west[h + 1, :] + 1
    west[h, :][S[h + 1] == '#'] = 0
    west[h, :][S[h, :] == '#'] = 0
# print(west)

ans = north + south + east + west
print(ans.max() + 1)

Submission status: Accepted

Solution code:
import numpy as np
h,w=map(int,input().split())
S=np.array([list(input()) for _ in range(h)])
S=np.where(S==".",1,0)

left,right,up,down=[np.zeros((h,w),dtype=np.int64) for _ in range(4)]
for i in range(w):
    left[:,i]=(left[:,i-1]+1)*S[:,i]
    right[:,-i-1]=(right[:,-i]+1)*S[:,-i-1]
for i in range(h):
    up[i]=(up[i-1]+1)*S[i]
    down[-i-1]=(down[-i]+1)*S[-i-1]

print(int(np.max(left+right+up+down)-3))

Submission status: Accepted

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

h_cnt = [[0] * w for _ in range(h)]
w_cnt = [[0] * w for _ in range(h)]

for i in range(h):
    dot_cnt, beg_sec = 0, 0
    for j in range(w):
        if s[i][j] == '#':
            if dot_cnt > 0:
                for k in range(beg_sec, j):
                    w_cnt[i][k] = dot_cnt
                beg_sec = j+1
                dot_cnt = 0
            else:
                w_cnt[i][j] = 0
                beg_sec += 1
        else:
            dot_cnt += 1
    if dot_cnt > 0:
        for k in range(beg_sec, w):
            w_cnt[i][k] = dot_cnt

for i in range(w):
    dot_cnt, beg_sec = 0, 0
    for j in range(h):
        if s[j][i] == '#':
            if dot_cnt > 0:
                for k in range(beg_sec, j):
                    h_cnt[k][i] = dot_cnt
                beg_sec = j+1
                dot_cnt = 0
            else:
                h_cnt[j][i] = 0
                beg_sec += 1
        else:
            dot_cnt += 1
    if dot_cnt > 0:
        for k in range(beg_sec, h):
            h_cnt[k][i] = dot_cnt

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

Submission status: Accepted

Solution code:
h,w = map(int,input().split())
t = [list(input()) for i in range(h)]
a = [[0 for i in range(w)] for j in range(h)]
x = [[0 for i in range(w)] for j in range(h)]
for i in range(h):
  for j in range(w):
    if t[i][j] == "#":
      x[i][j] = -1
    elif j == 0:
      x[i][j] = 0
    else:
      x[i][j] = x[i][j-1]+1
      a[i][j] += x[i][j]
x = [[0 for i in range(w)] for j in range(h)]
for i in range(h):
  for j in range(w-1,-1,-1):
    if t[i][j] == "#":
      x[i][j] = -1
    elif j == w-1:
      x[i][j] += 0
    else:
      x[i][j] = x[i][j+1]+1
      a[i][j] += x[i][j]
x = [[0 for i in range(w)] for j in range(h)]
for i in range(h):
  for j in range(w):
    if t[i][j] == "#":
      x[i][j] = -1
    elif i == 0:
      a[i][j] += 0
    else:
      x[i][j] = x[i-1][j]+1
      a[i][j] += x[i][j]
x = [[0 for i in range(w)] for j in range(h)]
for i in range(h-1,-1,-1):
  for j in range(w):
    if t[i][j] == "#":
      x[i][j] = -1
    elif i == h-1:
      a[i][j] += 0
    else:
      x[i][j] = x[i+1][j]+1
      a[i][j] += x[i][j]
print(max(max(a[i]) for i in range(h))+1)

Submission status: Accepted

Solution code:
def solve():
    H, W = map(int, input().split())
    field = [list(input()) for i in range(H)]
    H_table = [[0] * W for i in range(H)]
    W_table = [[0] * W for i in range(H)]
    
    for h in range(H):
        for w in range(W):
            if field[h][w] == '#':
                continue
            if w == 0:
                H_table[h][w] = 1
            else:
                H_table[h][w] = H_table[h][w-1] + 1

    for h in range(H-1, -1, -1):
        mx = 0
        for w in range(W-1,-1,-1):
            if field[h][w] == '#':
                continue
            if w == W-1 or field[h][w+1] == '#':
                mx = H_table[h][w]
            else:
                H_table[h][w] = mx
    
    for w in range(W):
        for h in range(H):
            if field[h][w] == '#':
                continue
            if h == 0:
                W_table[h][w] = 1
            else:
                W_table[h][w] = W_table[h-1][w] + 1

    for w in range(W-1, -1, -1):
        mx = 0
        for h in range(H-1,-1,-1):
            if field[h][w] == '#':
                continue
            if h == H-1 or field[h+1][w] == '#':
                mx = W_table[h][w]
            else:
                W_table[h][w] = mx
    
    mx = 0
    for h in range(H):
        for w in range(W):
            if field[h][w] == '#':
                continue
            mx = max(mx, W_table[h][w] + H_table[h][w] - 1)
    
    print(mx)

if __name__ == '__main__':
    solve()

Submission status: Accepted

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

#print(S)

sta_l1 = [[] for i in range(H)]
goa_l1 = [[] for i in range(H)]

for i in range(H):
    for j in range(W):
        if S[i][j] == ".":
            if j == 0 or S[i][j-1] == "#":
                start = j
                sta_l1[i].append(start)
            if j == W-1:
                goal = j
                goa_l1[i].append(goal)

        else:
            if j-1 >= 0 and S[i][j-1] == ".":
                goal = j-1
                goa_l1[i].append(goal)
#print(sta_l1)
#print(goa_l1)

sta_l2 = [[] for i in range(W)]
goa_l2 = [[] for i in range(W)]

v1 = [[0]*W for i in range(H)]
for i in range(len(sta_l1)):
    for j in range(len(sta_l1[i])):
        for k in range(sta_l1[i][j],goa_l1[i][j]+1):
            v1[i][k] = goa_l1[i][j] - sta_l1[i][j] +1
#print(v1)

for i in range(W):
    for j in range(H):
        if S[j][i] == ".":
            if j == 0 or S[j-1][i] == "#":
                start = j
                sta_l2[i].append(start)
            if j == H-1:
                goal = j
                goa_l2[i].append(goal)

        else:
            if j-1 >= 0 and S[j-1][i] == ".":
                goal = j-1
                goa_l2[i].append(goal)
#print(sta_l2)
#print(goa_l2)

v2 = [[0]*W for i in range(H)]
for i in range(len(sta_l2)):
    for j in range(len(sta_l2[i])):
        for k in range(sta_l2[i][j],goa_l2[i][j]+1):
            v2[k][i] = goa_l2[i][j] - sta_l2[i][j] +1
#print(v2)

ans = 0
v = [[0]*W for i in range(H)]
for i in range(H):
    for j in range(W):
        v[i][j] = v1[i][j] + v2[i][j] -1
        ans = max(ans,v1[i][j] + v2[i][j] -1)

#print(v)
print(ans)

Submission status: Accepted

Solution code:
H, W = map(int,input().split())
MAP = []
for _ in range(H):
    MAP.append(list(input()))
L = [[0 for i in range(W)] for j in range(H)]
R = [[0 for i in range(W)] for j in range(H)]
D = [[0 for i in range(W)] for j in range(H)]
U = [[0 for i in range(W)] for j in range(H)]

for i in range(H):
    for j in range(W):
        if MAP[i][j] == ".":
            if i == 0:
                U[i][j] = 1
            else:
                U[i][j] = U[i-1][j] + 1
            if j == 0:
                L[i][j] = 1
            else:
                L[i][j] = L[i][j-1] + 1
        if MAP[H-1-i][W-1-j] == ".":
            if i == 0:
                D[H-1-i][W-1-j] = 1
            else:
                D[H-1-i][W-1-j] = D[H-i][W-1-j] + 1
            if j == 0:
                R[H-1-i][W-1-j] = 1
            else:
                R[H-1-i][W-1-j] = R[H-1-i][W-j] + 1

MAX = 0

for i in range(H):
    for j in range(W):
        tmp = L[i][j] + R[i][j] + D[i][j] + U[i][j] - 3
        if MAX < tmp:
            MAX = tmp

print(MAX)

Submission status: Accepted

Solution code:
import bisect
# from collections import Counter, defaultdict, deque, namedtuple
# import copy
# from heapq import heappush, heappop, heapify
# from fractions import gcd
import itertools
# from operator import attrgetter, itemgetter
# import math

import sys

# import numpy as np

ipti = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)

def main():
    h, w = list(map(int,ipti().split()))
    hw = [sys.stdin.readline().rstrip("\n") 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)]

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

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

    print(ans-3)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

dph = [[0 for _ in range(w)] for _ in range(h)]
dpw = [[0 for _ in range(w)] for _ in range(h)]

for i in range(h):
  for j in range(w):
    if maze[i][j] == '#':
      continue
    if dpw[i][j] != 0:
      continue
    k = j
    count = 0
    while k< w:
      
      if maze[i][k] == '.':
        count += 1
        k += 1
      else:
        break
    for l in range(j,k):
      dpw[i][l] = count
      
for i in range(w):
  for j in range(h):
    if maze[j][i] == '#':
      continue
    if dph[j][i] != 0:
      continue
    k = j
    count = 0
    while k< h:
      
      if maze[k][i] == '.':
        count += 1
        k += 1
      else:
        break
    for l in range(j,k):
      dph[l][i] = count


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

Submission status: Accepted

Solution code:
def main():

    h,w = map(int,input().split())
    l = [list(input()) for i in range(h)]

    c1 = [[0 for i in range(w)] for i in range(h)]
    c2 = [[0 for i in range(w)] for i in range(h)]
    ans = 0
    for i in range(h):
        for j in range(w):
            if c1[i][j] == 0:
                n = 0
                k = i
                while k < h:
                    if l[k][j] == ".":
                        n +=1
                        k += 1
                    else:
                        break
                for r in range(i,k):
                    c1[r][j] = n
                
            if c2[i][j] == 0:
                n = 0
                k = j
                while k < w:
                    if l[i][k] == ".":
                        n +=1
                        k += 1
                    else:
                        break
                for r in range(j,k):
                    c2[i][r] = n


            ans = max(ans,c1[i][j]+c2[i][j])
    print(ans-1)
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:

h,w = map(int,input().split())
l = [list(input()) for i in range(h)]

c1 = [[0 for i in range(w)] for i in range(h)]
c2 = [[0 for i in range(w)] for i in range(h)]
ans = 0
for i in range(h):
    for j in range(w):
        if c1[i][j] == 0:
            n = 0
            k = i
            while k < h:
                if l[k][j] == ".":
                    n +=1
                    k += 1
                else:
                    break
            for r in range(i,k):
                c1[r][j] = n
            
        if c2[i][j] == 0:
            n = 0
            k = j
            while k < w:
                if l[i][k] == ".":
                    n +=1
                    k += 1
                else:
                    break
            for r in range(j,k):
                c2[i][r] = n


        ans = max(ans,c1[i][j]+c2[i][j])
print(ans-1)

Submission status: Accepted

Solution code:

h,w = map(int,input().split())
l = [list(input()) for i in range(h)]

c1 = [[0 for i in range(w)] for i in range(h)]
c2 = [[0 for i in range(w)] for i in range(h)]
ans = 0
for i in range(h):
    for j in range(w):
        if c1[i][j] == 0:
            n = 0
            while i+n < h:
                if l[i+n][j] == ".":
                    n +=1
                else:
                    break
            for k in range(n):
                c1[i+k][j] = n
            
        if c2[i][j] == 0:
            n = 0
            while j+n < w:
                if l[i][j+n] == ".":
                    n +=1
                else:
                    break
            for k in range(n):
                c2[i][j+k] = n


        ans = max(ans,c1[i][j]+c2[i][j])
print(ans-1)

Submission status: Accepted

Solution code:

h,w = map(int,input().split())
l = [list(input()) for i in range(h)]

c1 = [[-1 for i in range(w)] for i in range(h)]
c2 = [[-1 for i in range(w)] for i in range(h)]
ans = 0
for i in range(h):
    for j in range(w):
        if c1[i][j] == -1:
            n = 0
            while i+n < h:
                if l[i+n][j] == ".":
                    n += 1
                else:
                    break
            for k in range(n):
                c1[i+k][j] = n
            
        if c2[i][j] == -1:
            n = 0
            while j+n < w:
                if l[i][j+n] == ".":
                    n += 1
                else:
                    break
            for k in range(n):
                c2[i][j+k] = n


        ans = max(ans,c1[i][j]+c2[i][j])
print(ans-1)

Submission status: Accepted

Solution code:
h,w = map(int,input().split())
ans = [[0]*(w+2) for _ in range(h+2)]
grid = []
grid.append(['#']*(w+2))
for _ in range(h):
    s = list(input())
    grid.append(['#'] + s + ['#'])
grid.append(['#']*(w+2))
for i in range(1,h+1):
    for j in range(0,w+2):
        if grid[i][j] == '#':
            if j >= 1:
                for k in range(j-start):
                    ans[i][start+k] += j-start
            start = j + 1
for j in range(1,w+1):
    for i in range(0,h+2):
        if grid[i][j] == '#':
            if i >= 1:
                for k in range(i-start):
                    ans[start+k][j] += i-start
            start = i + 1
print(max(list(map(lambda x:max(x),ans)))-1)

Submission status: Accepted

Solution code:
import numpy as np

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

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(H):
    D[i] = (D[i-1]+1) * S[i]
    U[-i-1] = (U[-i]+1) * S[-i-1]

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

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

Submission status: Accepted

Solution code:
def main():
    import sys
    input = sys.stdin.readline

    H, W = map(int, input().split())
    grid = []
    for _ in range(H):
        grid.append(input().rstrip('\n'))

    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)]
    for h in range(H):
        for w in range(1, W):
            if grid[h][w-1] == '.' and grid[h][w] == '.':
                L[h][w] = L[h][w-1] + 1
    for h in range(H):
        for w in range(W-2, -1, -1):
            if grid[h][w] == '.' and grid[h][w+1] == '.':
                R[h][w] = R[h][w+1] + 1
    for w in range(W):
        for h in range(1, H):
            if grid[h][w] == '.' and grid[h-1][w] == '.':
                U[h][w] = U[h-1][w] + 1
    for w in range(W):
        for h in range(H-2, -1, -1):
            if grid[h][w] == '.' and grid[h+1][w] == '.':
                D[h][w] = D[h+1][w] + 1

    ans = 1
    for h in range(H):
        for w in range(W):
            ans = max(ans, L[h][w] + R[h][w] + U[h][w] + D[h][w] + 1)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

L=[[0]*w for _ in range(h)]
for i in range(h):
    for j in range(w):
        if M[i][j]=='#':
            L[i][j]=0
        elif j!=0:
            L[i][j]=L[i][j-1]+1
        else:
            L[i][j]=1
    
R=[[0]*w for _ in range(h)]
for i in range(h):
    for j in reversed(range(w)):
        if M[i][j]=='#':
            R[i][j]=0
        elif j!=w-1:
            R[i][j]=R[i][j+1]+1
        else:
            R[i][j]=1

U=[[0]*w for _ in range(h)]
for i in range(h):
    for j in range(w):
        if M[i][j]=='#':
            U[i][j]=0
        elif i!=0:
            U[i][j]=U[i-1][j]+1
        else:
            U[i][j]=1

D=[[0]*w for _ in range(h)]
for i in reversed(range(h)):
    for j in range(w):
        if M[i][j]=='#':
            D[i][j]=0
        elif i!=h-1:
            D[i][j]=D[i+1][j]+1
        else:
            D[i][j]=1

ans=1
for i in range(h):
    for j in range(w):
        if M[i][j]=='#':
            continue
        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 deque
import sys
sys.setrecursionlimit(10 ** 9)

h, w = map(int, input().split())
maze = [[-1 for i in range(w)] for j in range(h)]

ans = 0
for i in range(h):
    maze[i] = input()

queue = deque()

left = [[0 for j in range(w)] for i in range(h)]
right = [[0 for j in range(w)] for i in range(h)]
up = [[0 for j in range(w)] for i in range(h)]
down = [[0 for j in range(w)] for i in range(h)]

cnt = 0

for i in range(h):
    cnt = 0
    for j in range(w):
        if maze[i][j] == '#': cnt = 0
        else:
            cnt += 1
            left[i][j] = cnt

    cnt = 0
    for j in reversed(range(w)):
        if maze[i][j] == '#': cnt = 0
        else:
            cnt += 1
            right[i][j] = cnt


for j in range(w):
    cnt = 0
    for i in range(h):
        if maze[i][j] == '#': cnt = 0
        else:
            cnt += 1
            up[i][j] = cnt

    cnt = 0
    for i in reversed(range(h)):
        if maze[i][j] == '#': cnt = 0
        else:
            cnt += 1
            down[i][j] = cnt

ans = 0
for i in range(h):
    for j in range(w):
        tmp = left[i][j] + right[i][j] + up[i][j] + down[i][j] - 3
        ans = max(tmp, ans)

print(ans)

Submission status: Accepted

Solution code:
def main():
    H, W = (int(i) for i in input().split())
    S = [input() for i in range(H)]
    c = [[-3]*W for i in range(H)]
    for h in range(H):
        cur = 1
        for w in range(W):
            if S[h][w] == ".":
                c[h][w] += cur
            else:
                cur = 0
            cur += 1
    for h in range(H):
        cur = 1
        for w in range(W)[::-1]:
            if S[h][w] == ".":
                c[h][w] += cur
            else:
                cur = 0
            cur += 1
    for w in range(W):
        cur = 1
        for h in range(H):
            if S[h][w] == ".":
                c[h][w] += cur
            else:
                cur = 0
            cur += 1
    for w in range(W):
        cur = 1
        for h in range(H)[::-1]:
            if S[h][w] == ".":
                c[h][w] += cur
            else:
                cur = 0
            cur += 1
    ma = 0
    # print(*c, sep="\n")
    for h in range(H):
        for w in range(W):
            if ma < c[h][w]:
                ma = c[h][w]
    print(ma)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

H, W = [int(x) for x in input().split()]
S = []
for _ in range(H):
    S.append(input().rstrip())

dp_left = [[0] * (W + 2) for _ in range(H + 2)]
dp_up = [[0] * (W + 2) for _ in range(H + 2)]
dp_right = [[0] * (W + 2) for _ in range(H + 2)]
dp_down = [[0] * (W + 2) for _ in range(H + 2)]

for i in range(1, H + 1):
    for j in range(1, W + 1):
        if S[i - 1][j - 1] == "#":
            dp_left[i][j] = 0
        else:
            dp_left[i][j] = dp_left[i][j - 1] + 1
        
        if S[i - 1][-j] == "#":
            dp_right[i][-(j + 1)] = 0
        else:
            dp_right[i][-(j + 1)] = dp_right[i][-j] + 1

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

        if S[-j][i - 1] == "#":
            dp_down[-(j + 1)][i] = 0
        else:
            dp_down[-(j + 1)][i] = dp_down[-j][i] + 1

ans = 0

for i in range(1, H + 1):
    for j in range(1, W + 1):
        ans = max(ans, dp_left[i][j] + dp_up[i][j] + dp_right[i][j] + dp_down[i][j] - (dp_left[i][j] > 0) - (dp_up[i][j] > 0) - (dp_right[i][j] > 0) - (dp_down[i][j] > 0) + 1)

print(ans)

Submission status: Accepted

Solution code:
import numpy as np
from copy import deepcopy


def main():
    height, width = map(int, input().split())
    grid = [list(input()) for _ in range(height)]
    grid = (np.array(grid) == ".") * 1
    left = np.zeros((height, width), dtype=int)
    right = deepcopy(left)
    upper = deepcopy(left)
    down = deepcopy(left)

    for i in range(height):
        if i == 0:
            upper[i] = grid[i]
        else:
            upper[i] = (upper[i - 1] + 1) * grid[i]

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

    for i in range(width - 1, -1, -1):
        if i == width - 1:
            right[:, i] = grid[:, i]
        else:
            right[:, i] = (right[:, i + 1] + 1) * grid[:, i]

    for i in range(height - 1, -1, -1):
        if i == height - 1:
            down[i] = grid[i]
        else:
            down[i] = (down[i + 1] + 1) * grid[i]

    print(np.max(left + right + upper + down - 3))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import math,itertools,fractions,heapq,collections,bisect,sys,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

def main():
  h,w=LI()
  field=[list(S()) for _ in range(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 i in range(h):
    for j in range(w):
      if field[i][j]=='#':
        u[i][j]=0
        l[i][j]=0
      else:
        if i!=0:
          u[i][j]=u[i-1][j]+1
        else:
          u[i][j]=1
        if j!=0:
          l[i][j]=l[i][j-1]+1
        else:
          l[i][j]=1

  for i in range(h)[::-1]:
    for j in range(w)[::-1]:
      if field[i][j]=='#':
        d[i][j]=0
        r[i][j]=0
      else:
        if i!=h-1:
          d[i][j]=d[i+1][j]+1
        else:
          d[i][j]=1
        if j!=w-1:
          r[i][j]=r[i][j+1]+1
        else:
          r[i][j]=1

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

  return ans

# main()
print(main())

Submission status: Accepted

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

nums = [[0] * w for _ in range(h)]
ans = 0

for i in range(h):
    tmp = 0
    head = 0
    for j in range(w):
        if(s[i][j] == '#'):
            for k in range(head,j):
                nums[i][k] += tmp
            tmp = 0
            head = j+1
        else:
            tmp += 1
            if(j == w-1):
                for k in range(head,j+1):
                    nums[i][k] += tmp

for j in range(w):
    tmp = 0
    head = 0
    for i in range(h):
        if(s[i][j] == '#'):
            for k in range(head,i):
                nums[k][j] += tmp -1
            tmp = 0
            head = i+1
        else:
            tmp += 1
            if(i == h-1):
                for k in range(head,i+1):
                    nums[k][j] += tmp -1

for i in range(h):
    ans = max(ans, max(nums[i]))

print(ans)