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: Time Limit Exceeded

Solution code:
H,W = map(int,input().split())
s = [[] for i in range(H)]
for i in range(H): s[i] = list(input())
l = [[0]*W for i in range(H)]
r = [[0]*W for i in range(H)]
d = [[0]*W for i in range(H)]
u = [[0]*W for i in range(H)]

for h in range(H):
    for w in range(W):
        if s[h][w] == ".":
            l[h][w], d[h][w] = 1,1
            if w != 0: l[h][w] = l[h][w-1] + 1
            if h != 0: d[h][w] = d[h-1][w] + 1
        if s[H-1-h][W-1-w] == ".":
            r[H-1-h][W-1-w],u[H-1-h][W-1-w] = 1,1
            if w != 0: r[H-1-h][W-1-w] = r[H-1-h][W-w] + 1
            if h != 0: u[H-1-h][W-1-w] = u[H-h][W-1-w] + 1

ans = 0
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]-3)

print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
INF = 10**20
MOD = 10**9 + 7
I = lambda:list(map(int,input().split()))
# from math import gcd
from math import ceil
from collections import defaultdict as dd, Counter
from bisect import bisect_left as bl, bisect_right as br

h, w = I()
g = []
for i in range(h):
    g.append(input())

rows = [[0] * (w + 10) for i in range(h + 10)]
cols = [[0] * (w + 10) for i in range(h + 10)]

ans = 0
for i in range(h):
    for j in range(w):
        rows[i][j] = (g[i][j] == '.')
        if j and g[i][j] == '.':
            rows[i][j] += rows[i][j - 1]
        cols[i][j] = (g[i][j] == '.')
        if i and g[i][j] == '.':
            cols[i][j] += cols[i - 1][j]
    for j in range(w - 1, -1, -1):
        if rows[i][j + 1] != 0 and rows[i][j]:
            rows[i][j] = rows[i][j + 1]

for i in range(h - 1, -1, -1):
    for j in range(w):
        if cols[i + 1][j] != 0 and cols[i][j]:
            cols[i][j] = cols[i + 1][j]
        ans = max(ans, rows[i][j] + cols[i][j] - 1)
print(ans)

Submission status: Time Limit Exceeded

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
H,W = [int(x) for x in stdin.readline().rstrip().split()]
grid = []
for i in range(H):
    grid.append(list(input()))


tate = [[0]*W for i in range(H)]
yoko = [[0]*W for i in range(H)]

for i in range(H):
    cnt = 0
    for j in range(W):
        if grid[i][j] == ".":
            cnt += 1
            yoko[i][j] = cnt
        else:
            cnt = 0
    cnt = 0
    for j in range(W-1,-1,-1):
        if grid[i][j] == ".":
            cnt = max(cnt,yoko[i][j])
            yoko[i][j] = cnt
        else:
            cnt = 0

            
            
for j in range(W):
    cnt = 0
    for i in range(H):
        if grid[i][j] == ".":
            cnt += 1
            tate[i][j] = cnt
        else:
            cnt = 0
    cnt = 0
    for i in range(H-1,-1,-1):
        if grid[i][j] == ".":
            cnt = max(cnt,tate[i][j])
            tate[i][j] = cnt
        else:
            cnt = 0

ans = 0

for i in range(H):
    for j in range(W):
        tmp = tate[i][j] + yoko[i][j] - 1
        ans = max(ans,tmp)

print(ans)

Submission status: Time Limit Exceeded

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

L = [[0 for _ in range(W + 2)] for _ in range(H + 2)]
R = [[0 for _ in range(W + 2)] for _ in range(H + 2)]
U = [[0 for _ in range(W + 2)] for _ in range(H + 2)]
D = [[0 for _ in range(W + 2)] for _ in range(H + 2)]

for h in range(H):
    for w in range(W):
        if S[h][w] == ".":
            L[h + 1][w + 1] = L[h + 1][w] + 1
        i = W - w - 1
        if S[h][i] == ".":
            R[h + 1][i + 1] = R[h + 1][i + 2] + 1

for w in range(W):
    for h in range(H):
        if S[h][w] == ".":
            U[h + 1][w + 1] = U[h][w + 1] + 1
        i = H - h - 1
        if S[i][w] == ".":
            D[i + 1][w + 1] = D[i + 2][w + 1] + 1

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
H, W=map(int, input().split())
S=np.array([list(input()) for _ in range(H)])
def calc(h,w):
  row=S[h,:]
  col=S[:,w]
  
  count_h=0
  include=False
  for i,r in enumerate(row):
    if i==w:
        include=True
    if r==".":
      count_h+=1
    else:
      if include:
        break
      count_h=0
      
  count_w=0
  include=False
  for i,c in enumerate(col):
    if i==h:
      include=True
    if c==".":
      count_w+=1
    else:
      if include:
        break
      count_w=0
  return count_h+count_w-1


ans=0        
for i in range(H):
  for j in range(W):
    if S[i,j]==".":
      ans=max(ans, calc(i, j))
    
print(ans)

Submission status: Time Limit Exceeded

Solution code:
h,w = map(int,input().split())
s = [input() for _ in range(h)]
u1 = [[0 for _ in range(w)] for _ in range(h)]
u2 = [[0 for _ in range(w)] for _ in range(h)]
d1 = {}
d2 = {}

idx = 1
cnt = 0
for i in range(h):
    flag = False
    for j in range(w):
        if s[i][j] == ".":
            flag = True
            u1[i][j] = idx
            cnt += 1
        elif flag:
            d1[idx] = cnt
            cnt = 0
            idx += 1
            flag = False  
    if flag:
        d1[idx] = cnt
        cnt = 0
        idx += 1

idx = 1
cnt = 0
for j in range(w):
    flag = False
    for i in range(h):
        if s[i][j] == ".":
            flag = True
            u2[i][j] = idx
            cnt += 1
        elif flag:
            d2[idx] = cnt
            cnt = 0
            idx += 1
            flag = False  
    if flag:
        d2[idx] = cnt
        cnt = 0
        idx += 1

ans = 0
for i in range(h):
    for j in range(w):
        if s[i][j] == ".":
            ans = max(ans, d1[u1[i][j]]+d2[u2[i][j]]-1)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
h,w=map(int,input().split())
ans=[[-3]*w for _ in range(h)]
s=[input() for _ in range(h)]
for i in range(h):
    tmp=0
    for j in range(w):
        if s[i][j]=="#": tmp=0
        else: tmp+=1
        ans[i][j]+=tmp
    tmp=0
    for j in range(w-1,-1,-1):
        if s[i][j]=="#": tmp=0
        else: tmp+=1
        ans[i][j]+=tmp

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

print(max( max(ans[i]) for i in range(h) ))

Submission status: Time Limit Exceeded

Solution code:
#import numpy as np
H, W = map(int, input().split())
S = [0]*H
counth = [[0]*W for _ in range(H)]  
countw = [[0]*W for _ in range(H)]

for i in range(H):
  S[i] = input()
  
for i in range(H):
  for j in range(W):
    if S[i][j]=='.':
      if counth[i][j]==0:
        b=False
        for k in range(j,W):
          if S[i][k]=='#':
            b = True
            break
        if b==False:
          k+=1
        #print(S[i][j:],l)
        for l in range(j,k):
          #print(i,j,l)
          counth[i][l]=k-j
      if countw[i][j]==0:
        b=False
        for k in range(i,H):
          if S[k][j]=='#':
            b=True
            break
        if b==False:
          k+=1
        #print(S[i:][j],m)
        for l in range(i,k):
          #print(i,j,l)
          countw[l][j]=k-i
      #print(i,j,count)
#print(counth,countw)
maxim = 0
for i in range(H):
  for j in range(W):
    maxim = int(max(maxim,counth[i][j]+countw[i][j]-1))
print(maxim)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
H, W = map(int, input().split())
S = [0]*H
count = np.zeros((H,W,2))

for i in range(H):
  S[i] = input()
  
for i in range(H):
  for j in range(W):
    if S[i][j]=='.':
      if count[i][j][0]==0:
        l = len(S[i][j:].split('#')[0])
        #print(S[i][j:],l)
        count[i,j:j+l,0]=l
      if count[i][j][1]==0:
        m = len(''.join([S[k][j] for k in range(i,H)]).split('#')[0])
        #print(S[i:][j],m)
        count[i:i+m,j,1]=m
      #print(i,j,count)
#print(count)
maxim = 0
for i in range(H):
  for j in range(W):
    maxim = int(max(maxim,np.sum(count[i][j])-1))
print(maxim)

Submission status: Time Limit Exceeded

Solution code:
import sys
from bisect import *
from heapq import *
from collections import *
from itertools import *
from functools import *
from math import *

sys.setrecursionlimit(100000000)
input = lambda: sys.stdin.readline().rstrip()

def memoize(function):
  memo = {}
  def wrapper(*args):
    if args in memo:
      return memo[args]
    else:
      rv = function(*args)
      memo[args] = rv
      return rv
  return wrapper

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

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

cache = {}

@memoize
def up(i, j):
  if S[i][j] == '#': return 0
  return 1 + (up(i - 1, j) if i - 1 >= 0 else 0)


@memoize
def down(i, j):
  if S[i][j] == '#': return 0
  return 1 + (down(i + 1, j) if i + 1 < H else 0)


@memoize
def right(i, j):
  if S[i][j] == '#': return 0
  return 1 + (right(i, j + 1) if j + 1 < W else 0)


@memoize
def left(i, j):
  if S[i][j] == '#': return 0
  return 1 + (left(i, j - 1) if j - 1 >= 0 else 0)


ans = 0
for i in range(H):
  for j in range(W):
    if S[i][j] == '.':
      ans = max(ans, up(i, j) + down(i, j) + left(i, j) + right(i, j) - 3)
print(ans)

Submission status: Time Limit Exceeded

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

counter = [[[0, 0] for j in range(w)] for i in range(h)]
ans = 0
for i in range(h):
  for j in range(w):
    if grid[i][j] == "#":
      continue
    if i == 0 or (i != 0 and grid[i-1][j] == "#"):
      k = 0
      while i+k < h and grid[i+k][j] == ".":
        k += 1
      counter[i][j][0] = k-1
    else:
      counter[i][j][0] = counter[i-1][j][0]
    if j == 0 or (j != 0 and grid[i][j-1] == "#"):
      k = 0
      while j+k < w and grid[i][j+k] == ".":
        k += 1
      counter[i][j][1] = k-1
    else:
      counter[i][j][1] = counter[i][j-1][1]
    if ans < counter[i][j][0]+counter[i][j][1]:
      ans = counter[i][j][0]+counter[i][j][1]
print(ans+1)

Submission status: Time Limit Exceeded

Solution code:
import sys
from bisect import *
from heapq import *
from collections import *
from itertools import *
from functools import *
from math import *

sys.setrecursionlimit(100000000)
input = lambda: sys.stdin.readline().rstrip()

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

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

cache = {}

def up(i, j):
    key = (i, j, "u")
    if key in cache: return cache[key]
    if S[i][j] == '#': return 0
    cache[key] = 1 + (up(i - 1, j) if i - 1 >= 0 else 0)
    return cache[key]

def down(i, j):
    key = (i, j, "d")
    if key in cache: return cache[key]
    if S[i][j] == '#': return 0
    cache[key] = 1 + (down(i + 1, j) if i + 1 < H else 0)
    return cache[key]

def right(i, j):
    key = (i, j, "r")
    if key in cache: return cache[key]
    if S[i][j] == '#': return 0
    cache[key] = 1 + (right(i, j + 1) if j + 1 < W else 0)
    return cache[key]

def left(i, j):
    key = (i, j, "l")
    if key in cache: return cache[key]
    if S[i][j] == '#': return 0
    cache[key] = 1 + (left(i, j - 1) if j - 1 >= 0 else 0)
    return cache[key]

ans = 0
for i in range(H):
    for j in range(W):
        if S[i][j] == '.':
            ans = max(ans, up(i, j) + down(i, j) + left(i, j) + right(i, j) - 3)
print(ans)

Submission status: Time Limit Exceeded

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

data = [[[0, 0] for _ in range(W)] for _ in range(H)]

for h in range(H):
    for w in range(W):
        if S[h][w] == "#":
            continue
        a, b = data[h][w]

        if a == 0:
            x = w
            while x < W and S[h][x] == ".":
                x += 1
            for i in range(x - w):
                data[h][w + i][0] = x - w

        if b == 0:
            y = h
            while y < H and S[y][w] == ".":
                y += 1
            for i in range(y - h):
                data[h + i][w][1] = y - h

n_max = max(sum(data[h][w]) for h in range(H) for w in range(W)) - 1
ans = max(n_max, 0)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
h,w=map(int,input().split())
S=[["#"]+list(input())+["#"] for _ in range(h)]
S.insert(0,["#"]*(w+2))
S.append(["#"]*(w+2))

C=[[[0]*4 for _ in range(w+2)] for _ in range(h+2)]
for i in range(1,h+1):
    for n, flag in enumerate((1, -1)):
        for j in range(1,w+1)[::flag]:
            if S[i][j]!="#":C[i][j][n] +=C[i][j-flag][n]+1
for i in range(1,w+1):
    for n, flag in enumerate((1, -1)):
        for j in range(1,h+1)[::flag]:
            if S[j][i]!="#":C[j][i][n+2] +=C[j-flag][i][n+2]+1

print(max([sum(C[i][j])-3 for i in range(1,h+1) for j in range(1,w+1)]))

Submission status: Time Limit Exceeded

Solution code:
h,w=map(int,input().split())
S=[["#"]+list(input())+["#"] for _ in range(h)]
S.insert(0,["#"]*(w+2))
S.append(["#"]*(w+2))

C=[[[0]*4 for _ in range(w+2)] for _ in range(h+2)]
for n,flag in enumerate((1,-1)):
    for i in range(1,h+1):
        for j in range(1,w+1)[::flag]:
            if S[i][j]!="#":C[i][j][n] +=C[i][j-flag][n]+1
for n,flag in enumerate((1,-1)):
    for i in range(1,w+1):
        for j in range(1,h+1)[::flag]:
            if S[j][i]!="#":C[j][i][n+2] +=C[j-flag][i][n+2]+1

print(max([sum(C[i][j])-3 for i in range(1,h+1) for j in range(1,w+1)]))

Submission status: Time Limit Exceeded

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

L = [[0]*w for _ in range(h)]
R = [[0]*w for _ in range(h)]
U = [[0]*w for _ in range(h)]
D = [[0]*w for _ in range(h)]

for i in range(h):
    for j in range(w):
        if s[i][j] == '#':
            continue
        elif j == 0:
            L[i][j] = 1
        else:
            L[i][j] = L[i][j-1] + 1
            
for i in range(h):
    for j in range(w-1, -1, -1):
        if s[i][j] == '#':
            continue
        elif j == w-1:
            R[i][j] = 1
        else:
            R[i][j] = R[i][j+1] + 1
            
for i in range(h):
    for j in range(w):
        if s[i][j] == '#':
            continue
        elif i == 0:
            U[i][j] = 1
        else:
            U[i][j] = U[i-1][j] + 1
            
for i in range(h-1, -1, -1):
    for j in range(w):
        if s[i][j] == '#':
            continue
        elif 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):
        x = L[i][j] + R[i][j] + U[i][j] + D[i][j] - 3
        ans = max(ans, x)
        
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys

readline = sys.stdin.readline
h, w = map(int, readline().split())
grid = [readline() for _ in range(h)]
light = [[0] * w for _ in range(h)]

# row
for i in range(h):
    start = -1
    for j in range(w):
        if grid[i][j] == '.' and start == -1:
            start = j
        elif grid[i][j] == '#' and start != -1:
            length = j - start
            light[i][start:j] = [length] * length
            start = -1
    if start != -1:
        length = w - start
        light[i][start:w] = [length] * length
# column
for i in range(w):
    start = -1
    for j in range(h):
        if grid[j][i] == '.' and start == -1:
            start = j
        elif grid[j][i] == '#' and start != -1:
            length = j - start
            for k in range(start, j):
                light[k][i] += length
            start = -1
    if start != -1:
        length = h - start
        for k in range(start, h):
            light[k][i] += length
ans = max(max(row) for row in light)
print(ans-1)

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10 ** 7)

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

# TLE
# O(HW(H+W)) で10^8になるので無理

H, W = map(int, input().split())

S = [input() for _ in range(H)]
NUM = [[0] * (W) for _ in range(H)]

for r in range(H):
    cnt = 0
    for c in range(W):
        if S[r][c] == '#':
            cnt = 0
        else:
            cnt += 1
            NUM[r][c] += cnt

    cnt = 0
    for c in range(W - 1, -1, -1):
        if S[r][c] == '#':
            cnt = 0
        else:
            cnt += 1
            NUM[r][c] += cnt

for c in range(W):
    cnt = 0
    for r in range(H):
        if S[r][c] == '#':
            cnt = 0
        else:
            cnt += 1
            NUM[r][c] += cnt

    cnt = 0
    for r in range(H - 1, -1, -1):
        if S[r][c] == '#':
            cnt = 0
        else:
            cnt += 1
            NUM[r][c] += cnt

ans = 4
for i in range(H):
    t = max(NUM[i])
    ans = max(ans, t)

print(ans-3)

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10 ** 7)

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

H, W = map(int, input().split())

#S = [[0 if x == '.' else 1 for x in list(input())] for _ in range(H)]
S = [list(input()) for _ in range(H)]
NUM = [[[None] * 2 for _ in range(W)] for _ in range(H)]


def cntDot(r, c, vertical, cnt):
    #print("Input", r, c, vertical, cnt)
    if S[r][c] == '#':
        NUM[r][c][vertical] = 0
        return cnt
    else:
        cnt += 1

    # x軸
    if vertical == 0:
        if c < W - 1:
            t = cntDot(r, c + 1, vertical, cnt)
            NUM[r][c][vertical] = t
            return t
        else:
            NUM[r][c][vertical] = cnt
            return cnt

    # y軸
    if vertical == 1:
        if r < H - 1:
            t = cntDot(r + 1, c, vertical, cnt)
            NUM[r][c][vertical] = t
            return t
        else:
            NUM[r][c][vertical] = cnt
            return cnt

    return 0


for r in range(H):
    for c in range(W):
        if S[r][c] == 1:
            NUM[r][c] = [0, 0]
            continue
        # x軸
        if NUM[r][c][0] == None:
            NUM[r][c][0] = cntDot(r, c, 0, 0)
        # y軸
        if NUM[r][c][1] == None:
            NUM[r][c][1] = cntDot(r, c, 1, 0)

# print(NUM)

ans = 1
for r in range(H):
    v = max(NUM[r])
    ans = max(ans, sum(v)-1)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10 ** 7)

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

H, W = map(int, input().split())

S = [list(input()) for _ in range(H)]
NUM = [[[None] * 2 for _ in range(W)] for _ in range(H)]


def cntDot(r, c, vertical, cnt):
    #print("Input", r, c, vertical, cnt)
    if S[r][c] == '#':
        NUM[r][c][vertical] = 0
        return cnt
    else:
        cnt += 1

    # x軸
    if vertical == 0:
        if c < W - 1:
            t = cntDot(r, c + 1, vertical, cnt)
            NUM[r][c][vertical] = t
            return t
        else:
            NUM[r][c][vertical] = cnt
            return cnt

    # y軸
    if vertical == 1:
        if r < H - 1:
            t = cntDot(r + 1, c, vertical, cnt)
            NUM[r][c][vertical] = t
            return t
        else:
            NUM[r][c][vertical] = cnt
            return cnt

    return 0


for r in range(H):
    for c in range(W):
        # x軸
        if NUM[r][c][0] == None:
            NUM[r][c][0] = cntDot(r, c, 0, 0)
        # y軸
        if NUM[r][c][1] == None:
            NUM[r][c][1] = cntDot(r, c, 1, 0)

#print(NUM)

ans = 1
for r in range(H):
    for c in range(W):
        ans = max(NUM[r][c][0] + NUM[r][c][1] - 1, ans)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10 ** 7)

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

H, W = map(int, input().split())

S = [[0 if x == '.' else 1 for x in list(input())] for _ in range(H)]
NUM = [[[None] * 2 for _ in range(W)] for _ in range(H)]


def cntDot(r, c, vertical, cnt):
    #print("Input", r, c, vertical, cnt)
    if S[r][c] == 1:
        NUM[r][c][vertical] = 0
        return cnt
    else:
        cnt += 1

    # x軸
    if vertical == 0:
        if c < W - 1:
            t = cntDot(r, c + 1, vertical, cnt)
            NUM[r][c][vertical] = t
            return t
        else:
            NUM[r][c][vertical] = cnt
            return cnt

    # y軸
    if vertical == 1:
        if r < H - 1:
            t = cntDot(r + 1, c, vertical, cnt)
            NUM[r][c][vertical] = t
            return t
        else:
            NUM[r][c][vertical] = cnt
            return cnt

    return 0


for r in range(H):
    for c in range(W):
        # x軸
        if NUM[r][c][0] == None:
            NUM[r][c][0] = cntDot(r, c, 0, 0)
        # y軸
        if NUM[r][c][1] == None:
            NUM[r][c][1] = cntDot(r, c, 1, 0)

# print(NUM)

ans = 1
for r in range(H):
    for c in range(W):
        ans = max(NUM[r][c][0] + NUM[r][c][1] - 1, ans)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10 ** 7)

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

H, W = map(int, input().split())

S = [input() for _ in range(H)]
NUM = [[[None] * 2 for _ in range(W)] for _ in range(H)]


def cntDot(r, c, vertical, cnt):
    #print("Input", r, c, vertical, cnt)
    if S[r][c] == '#':
        NUM[r][c][vertical] = 0
        return cnt
    else:
        cnt += 1

    # x軸
    if vertical == 0:
        if c < W - 1:
            t = cntDot(r, c + 1, vertical, cnt)
            NUM[r][c][vertical] = t
            return t
        else:
            NUM[r][c][vertical] = cnt
            return cnt

    # y軸
    if vertical == 1:
        if r < H - 1:
            t = cntDot(r + 1, c, vertical, cnt)
            NUM[r][c][vertical] = t
            return t
        else:
            NUM[r][c][vertical] = cnt
            return cnt

    return 0


for r in range(H):
    for c in range(W):
        # x軸
        if NUM[r][c][0] == None:
            NUM[r][c][0] = cntDot(r, c, 0, 0)
        # y軸
        if NUM[r][c][1] == None:
            NUM[r][c][1] = cntDot(r, c, 1, 0)

#print(NUM)

ans = 1
for r in range(H):
    for c in range(W):
        ans = max(NUM[r][c][0] + NUM[r][c][1] - 1, ans)

print(ans)

Submission status: Time Limit Exceeded

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 h in range(1, H + 1):
    for w in range(1, W + 1):
        if S[h, w] == '#':
            continue
        if S[h - 1, w] == '#':
            continue
        north[h, w] = north[h - 1, w] + 1
# print(north)

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

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

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

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

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10**9)
import numpy as np
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)
# print(S.shape)


def f(x, y, dx, dy):
    if S[x + dx, y + dy] == '#':
        return 0
    return f(x + dx, y + dy, dx, dy) + 1


ans = 0
for i in range(1, H + 1):
    for j in range(1, W + 1):
        # print('i', i, j)
        if S[i, j] != '.':
            continue
        v = 1
        for dx, dy in ((1, 0), (-1, 0), (0, 1), (0, -1)):
            v += f(i, j, dx, dy)
        ans = max(ans, v)
print(ans)