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())
l=[]
for i in range(h):
  l1=list(input())
  l.append(l1)
ll=[[0 for i in range(w)] for j in range(h)]
lr=[[0 for i in range(w)] for j in range(h)]
lu=[[0 for i in range(w)] for j in range(h)]
ld=[[0 for i in range(w)] for j in range(h)]
for i in range(h):
  if l[i][0]=="#":
    ll[i][0]=0
  else:
    ll[i][0]=1
  if l[i][w-1]=="#":
    lr[i][w-1]=0
  else:
    lr[i][w-1]=1
for i in range(w):
  if l[0][i]=="#":
    lu[0][i]=0
  else:
    lu[0][i]=1
  if l[h-1][i]=="#":
    ld[h-1][i]=0
  else:
    ld[h-1][i]=1
for i in range(h):
  for j in range(1,w):
    if l[i][j]=="#":
      ll[i][j]=0
    else:
      ll[i][j]=ll[i][j-1]+1
    if l[i][-1-j]=="#":
      lr[i][-1-j]=0
    else:
      lr[i][-1-j]=lr[i][-j]+1
for i in range(1,h):
  for j in range(w):
    if l[i][j]=="#":
      lu[i][j]=0
    else:
      lu[i][j]=lu[i-1][j]+1
    if l[-i-1][j]=="#":
      ld[-i-1][j]=0
    else:
      ld[-i-1][j]=ld[-i][j]+1
max=0
for i in range(h):
  for j in range(w):
    num=ll[i][j]+lr[i][j]+lu[i][j]+ld[i][j]-3
    if num>max:
      max=num
print(max)

Submission status: Time Limit Exceeded

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

def solve(H: int, W: int, S: "List[str]"):
    ss = [list(s) for s in S]
    answer = 0
    right = [[0]*W for _ in range(H)]
    left = [[0]*W for _ in range(H)]
    up = [[0]*W for _ in range(H)]
    down = [[0]*W for _ in range(H)]

    for h in range(H):
        for j in range(W):
            if ss[h][j] == ".":
                if j>0:
                    right[h][j] = 1+right[h][j-1]
                else:
                    right[h][j] += 1
            else:
                right[h][j] = 0

        for j in range(W-1,-1,-1):
            if ss[h][j] == ".":
                if j<W-1:
                    left[h][j] = 1+left[h][j+1]
                else:
                    left[h][j] += 1
            else:
                left[h][j] = 0
    for w in range(W):                
        for i in range(H-1,-1,-1):
            if ss[i][w] == '.':
                if i < H-1:
                    up[i][w] = 1 + up[i+1][w]
                else:
                    up[i][w] +=1
            else:
                up[i][w] = 0
        for i in range(0,H):
            if ss[i][w] == '.':
                if i > 0:
                    down[i][w] = 1 + down[i-1][w]
                else:
                    down[i][w] +=1
            else:
                down[i][w] = 0

    for i in range(H):
        for j in range(W):
            answer = max(answer,right[i][j]+left[i][j]+up[i][j]+down[i][j]-3)

    print(answer)






    # nps = np.array(ss)
    # nps = nps=="."
    # sum_retu = nps.sum(axis = 0)#列ごとに足したものWこ
    # sum_gyo = nps.sum(axis = 1)#行ごとに足したもの
    # answer  =0
    # for i in sum_retu:
    #     for j in sum_gyo:
    #         answer = max(answer,i+j)
    # print(answer-1)
    return


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

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

def solve(H: int, W: int, S: "List[str]"):
    ss = [list(s) for s in S]
    answer = 0
    for h in range(H):
        for w in range(W):
            if ss[h][w]==".":
                a = 0
                ##右に数える
                for right in range(w,W):
                    a+= 1 if ss[h][right]=="." else 0
                    if ss[h][right]=="#":
                        break
                for left in range(w,-1,-1):
                    a+= 1 if ss[h][left]=="." else 0
                    if ss[h][left]=="#":
                        break
                
                for up in range(h,-1,-1):
                    a+= 1 if ss[up][w]=="." else 0 
                    if ss[up][w]=="#":
                        break       
                
                for down in range(h,H):
                    a+= 1 if ss[down][w]=="." else 0 
                    if ss[down][w]=="#":
                        break
                
                a -= 3
                answer = max(answer,a)
    print(answer)






    # nps = np.array(ss)
    # nps = nps=="."
    # sum_retu = nps.sum(axis = 0)#列ごとに足したものWこ
    # sum_gyo = nps.sum(axis = 1)#行ごとに足したもの
    # answer  =0
    # for i in sum_retu:
    #     for j in sum_gyo:
    #         answer = max(answer,i+j)
    # print(answer-1)
    return


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

Solution code:
tmp = list(map(int, input().split()))
h = tmp[0]
w = tmp[1] 

s = ['None'] * (h + 1)
for i in range(h):
  s[i] = input()
  s[i] = s[i] + '#'
s[h] = '#' * w

ans = 0

sta = -1
cnt = 0
pt = [[0] * w for i in range(h)]
for i in range(h):
  for j in range(w+1):
    if s[i][j] == '#':
      if sta != -1:
        for k in range(sta, j):
          pt[i][k] = cnt
      cnt = 0
      sta = -1
    else:
      if sta == -1:
        sta = j
      cnt += 1

for i in range(w):
  for j in range(h+1):
    if s[j][i] == '#':
      if sta != -1:
        for k in range(sta, j):
          pt[k][i] += cnt - 1
      cnt = 0
      sta = -1
    else:
      if sta == -1:
        sta = j
      cnt += 1

ans = 0
for i in range(h):
  for j in range(w):
    if ans < pt[i][j]:
      ans = pt[i][j]
print(ans)

Submission status: Time Limit Exceeded

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

# L, R, U, D方向の照らされるマス数を事前に計算
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):
    road = S[i][::-1][W - j - 1:]
    cnt = 0
    for k in road:
      if k == ".":
        cnt += 1
      else:
        break
    L[i][j] = cnt
    
    road = S[i][j:]
    cnt = 0
    for k in road:
      if k == ".":
        cnt += 1
      else:
        break
    R[i][j] = cnt

# 転置行列を準備
ST = list(map(list, zip(*S)))

for i in range(W):
  for j in range(H):
    road = ST[i][::-1][H - j - 1:]
    cnt = 0
    for k in road:
      if k == ".":
        cnt += 1
      else:
        break
    U[j][i] = cnt

    road = ST[i][j:]
    cnt = 0
    for k in road:
      if k == ".":
        cnt += 1
      else:
        break
    D[j][i] = cnt

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

print(ans)

Submission status: Time Limit Exceeded

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

# L, R, U, D方向の照らされるマス数を事前に計算
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):
    road = S[i][::-1][W - j - 1:]
    cnt = 0
    for k in road:
      if k == ".":
        cnt += 1
      else:
        break
    L[i][j] = cnt
    
    road = S[i][j:]
    cnt = 0
    for k in road:
      if k == ".":
        cnt += 1
      else:
        break
    R[i][j] = cnt

# 転置行列を準備
ST = list(map(list, zip(*S)))

for i in range(W):
  for j in range(H):
    road = ST[i][::-1][H - j - 1:]
    cnt = 0
    for k in road:
      if k == ".":
        cnt += 1
      else:
        break
    U[j][i] = cnt

    road = ST[i][j:]
    cnt = 0
    for k in road:
      if k == ".":
        cnt += 1
      else:
        break
    D[j][i] = cnt

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

print(ans)

Submission status: Time Limit Exceeded

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

    m = [[0] + [0 if e == "#" else 1 for e in s[i]] + [0] for i in range(h)]
    m.append([0] * (w + 2))
    m.insert(0, [0] * (w + 2))

    m_t = [[m[i][j] for i in range(h+2)] for j in range(w+2)]

    lr = []
    for i in range(h + 2):
        sm = 0
        row = []
        for j in range(w + 2):
            if m[i][j]:
                sm += 1
            else:
                row += [sm] * sm + [0]
                sm = 0

        lr.append(row)

    ud = []
    for i in range(w + 2):
        sm = 0
        row = []
        for j in range(h + 2):
            if m_t[i][j]:
                sm += 1
            else:
                row += [sm] * sm + [0]
                sm = 0

        ud.append(row)

    ans = 0
    for i in range(1, h + 1):
        for j in range(1, w + 1):
            sm = lr[i][j] + ud[j][i] - 1
            ans = max(ans, sm)

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
def accumulate(li):
    ret = [0]
    for e in li:
        if e:
            ret.append(ret[-1] + 1)
        else:
            ret.append(0)

    return ret[1:]


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

    m = [[0 if e == "#" else 1 for e in s[i]] for i in range(h)]

    lr = [accumulate(m[i]) for i in range(h)]
    rl = [accumulate(m[i][::-1])[::-1] for i in range(h)]

    m_ud = []
    for i in range(w):
        tmp = []
        for j in range(h):
            tmp.append(m[j][i])
        m_ud.append(tmp)

    ud = [accumulate(m_ud[i]) for i in range(w)]
    du = [accumulate(m_ud[i][::-1])[::-1] for i in range(w)]

    ans = 0
    for i in range(h):
        for j in range(w):
            sm = lr[i][j] + rl[i][j] + ud[j][i] + du[j][i] - 3
            ans = max(ans, sm)

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
h, w = input().split()
h = int(h)
w = int(w)
grid = []
for i in range(1, h+1):
    grid.append(input())

gridNumbers = [[0 for i in range(w)] for j in range(h)]

for i in range(0, h):
    for j in range(0, w):
        if grid[i][j] is "#":
            gridNumbers[i][j] = 0
        else:
            tempi = i
            tempj = j
            currentCellValue = 1
            while tempi > 0 and grid[tempi - 1][j] is not "#":
                currentCellValue = currentCellValue + 1
                tempi = tempi - 1
            tempi = i
            while tempi < h - 1 and grid[tempi + 1][j] is not "#":
                currentCellValue = currentCellValue + 1
                tempi = tempi + 1
            while tempj > 0 and grid[i][tempj - 1] is not "#":
                currentCellValue = currentCellValue + 1
                tempj = tempj - 1
            tempj = j
            while tempj < w - 1 and grid[i][tempj + 1] is not "#":
                currentCellValue = currentCellValue + 1
                tempj = tempj + 1
            gridNumbers[i][j] = currentCellValue

tempList = [0 for i in range(h)]
for i in range(h):
    tempList[i] = max(gridNumbers[i])

print(max(tempList))

Submission status: Time Limit Exceeded

Solution code:
H, W = map(int, input().split())
s, ans = [], 0
dpr = [[0] * W for _ in range(H)]
dpl = [[0] * W for _ in range(H)]
dpd = [[0] * W for _ in range(H)]
dpu = [[0] * W for _ in range(H)]

for i in range(H):
    s.append(input())

# to right
for i in range(H):
    for j in reversed(range(W)):
        if j == W-1 and s[i][j] == '.':
            dpr[i][j] = 1
        elif s[i][j] == '#':
            dpr[i][j] = 0
        else:
            dpr[i][j] = dpr[i][j+1] + 1

# to left
for i in range(H):
    for j in range(0, W):
        if j == 0 and s[i][j] == '.':
            dpl[i][j] = 1
        elif s[i][j] == '#':
            dpl[i][j] = 0
        else:
            dpl[i][j] = dpl[i][j-1] + 1

# to down
for i in reversed(range(H)):
    for j in range(W):
        if i == H-1 and s[i][j] == '.':
            dpd[i][j] = 1
        elif s[i][j] == '#':
            dpd[i][j] = 0
        else:
            dpd[i][j] = dpd[i+1][j] + 1

# to up
for i in range(0, H):
    for j in range(W):
        if i == 0 and s[i][j] == '.':
            dpu[i][j] = 1
        elif s[i][j] == '#':
            dpu[i][j] = 0
        else:
            dpu[i][j] = dpu[i-1][j] + 1

for i in reversed(range(H)):
    for j in reversed(range(W)):
        cnt = dpr[i][j] + dpl[i][j] + dpd[i][j] + dpu[i][j] - 3
        ans = max(ans, cnt)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
H, W = map(int, input().split())
s, ans = [], 0
dpr, dpl, dpd, dpu = {},{},{},{}
for i in range(H):
    s.append(input())

def getCount(h, w):
    if s[h][w] == '#':
        return 0
    rcnt, lcnt, dcnt, ucnt = 0,0,0,0
    rootKey = str(h) + str(w)

    # to right
    for i in range(w+1, W):
        key = str(h) + str(i)
        if s[h][i] == '.':
            if key in dpr:
                rcnt += dpr[key] + 1
                break
            else:
                rcnt += 1
        else:
            break
    dpr[rootKey] = rcnt

    # to left
    for i in reversed(range(w)):
        key = str(h) + str(i)
        if s[h][i] == '.':
            if key in dpl:
                lcnt += dpl[key] + 1
                break
            else:
                lcnt += 1
        else:
            break
    dpl[rootKey] = lcnt

    # to down
    for i in range(h+1, H):
        key = str(i) + str(w)
        if s[i][w] == '.':
            if key in dpd:
                dcnt += dpd[key] + 1
                break
            else:
                dcnt += 1
        else:
            break
    dpd[rootKey] = dcnt

    # to up
    for i in reversed(range(h)):
        key = str(i) + str(w)
        if s[i][w] == '.':
            if key in dpu:
                ucnt += dpu[key] + 1
                break
            else:
                ucnt += 1
        else:
            break
    dpu[rootKey] = ucnt

    return rcnt + lcnt + dcnt + ucnt + 1

for i in range(H):
    for j in range(W):
        ans = max(ans, getCount(i, j))

print(ans)

Submission status: Time Limit Exceeded

Solution code:
H, W = map(int, input().split())
s, ans = [], 0
dp, dpr, dpl, dpd, dpu = {},{},{},{},{}
for _ in range(H):
    s.append(input())

def getRightCount(h, w):
    key = str(h) + str(w)
    if s[h][w] == '#':
        dpr[key] = 0
        return
    cnt = 0
    for i in range(w+1, W):
        if s[h][i] == '#':
            break
        else:
            cnt += 1
        temp = str(h) + str(i)
        if temp in dpr:
            cnt += dpr[temp]
            break
    dpr[key] = cnt

def getLeftCount(h, w):
    key = str(h) + str(w)
    if s[h][w] == '#':
        dpl[key] = 0
        return
    cnt = 0
    for i in reversed(range(0, w)):
        if s[h][i] == '#':
            break
        else:
            cnt += 1
        temp = str(h) + str(i)
        if temp in dpl:
            cnt += dpl[temp]
            break
    dpl[key] = cnt

def getDownCount(h, w):
    key = str(h) + str(w)
    if s[h][w] == '#':
        dpd[key] = 0
        return
    cnt = 0
    for i in range(h+1, H):
        if s[i][w] == '#':
            break
        cnt += 1
        temp = str(i) + str(w)
        if temp in dpd:
            cnt += dpd[temp]
            break
    dpd[key] = cnt

def getUpCount(h, w):
    key = str(h) + str(w)
    if s[h][w] == '#':
        dpu[key] = 0
        return
    cnt = 0
    for i in reversed(range(0, h)):
        if s[i][w] == '#':
            break
        cnt += 1
        temp = str(i) + str(w)
        if temp in dpu:
            cnt += dpu[temp]
            break
    dpu[key] = cnt

def getCount(h, w):
    if s[h][w] == '#':
        return 0
    getRightCount(h, w)
    getLeftCount(h, w)
    getDownCount(h, w)
    getUpCount(h, w)

for i in range(H):
    for j in range(W):
        getCount(i, j)
        #ans = max(ans, getCount(i, j))

for i in range(H):
    for j in range(W):
        if s[i][j] == '.':
            #print('\n', i, j)
            key = str(i) + str(j)
            #print(dpr[key], dpl[key], dpd[key], dpu[key])
            temp = dpr[key] + dpl[key] + dpd[key] + dpu[key] + 1
            ans = max(ans, temp)
            #print(ans)

#print(dpr)
print(ans)

Submission status: Time Limit Exceeded

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

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

#print(L)
for h in range(H):
  for w in range(W-1,-1,-1):
    if field[h][w] == "#":
      R[h][w] = 0
    elif w == W-1:
      R[h][w] = 1
    else:
      R[h][w] = R[h][w+1] + 1
#print(R)
for h in range(H-1,-1,-1):
  for w in range(W):
    if field[h][w] == "#":
      D[h][w] = 0
    elif h == H-1:
      D[h][w] = 1
    else:
      D[h][w] = D[h+1][w] + 1
#print(D)     
#print(U)
ans = 0
for h in range(H):
  for w in range(W):
    if field[h][w] == "#":
      L[h][w] = 0
    elif w == 0:
      L[h][w] = 1
    else:
      L[h][w] = L[h][w-1] + 1
    if field[h][w] == "#":
      U[h][w] = 0
    elif h == 0:
      U[h][w] = 1
    else:
      U[h][w] = U[h-1][w] + 1
    ans = max(ans,L[h][w] + R[h][w] + D[h][w] + U[h][w] - 3)

print(ans)

Submission status: Time Limit Exceeded

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

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

for h in range(H):
  for w in range(W):
    if field[h][w] == "#":
      L[h][w] = 0
    elif w == 0:
      L[h][w] = 1
    else:
      L[h][w] = L[h][w-1] + 1
#print(L)
for h in range(H):
  for w in range(W-1,-1,-1):
    if field[h][w] == "#":
      R[h][w] = 0
    elif w == W-1:
      R[h][w] = 1
    else:
      R[h][w] = R[h][w+1] + 1
#print(R)
for h in range(H-1,-1,-1):
  for w in range(W):
    if field[h][w] == "#":
      D[h][w] = 0
    elif h == H-1:
      D[h][w] = 1
    else:
      D[h][w] = D[h+1][w] + 1
#print(D)     
for h in range(H):
  for w in range(W):
    if field[h][w] == "#":
      U[h][w] = 0
    elif h == 0:
      U[h][w] = 1
    else:
      U[h][w] = U[h-1][w] + 1
#print(U)
ans = 0
for h in range(H):
  for w in range(W):
    ans = max(ans,L[h][w] + R[h][w] + D[h][w] + U[h][w] - 3)
print(ans)

Submission status: Time Limit Exceeded

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

U=[[-1]*W for _ in range(H)]
D=[[-1]*W for _ in range(H)]
L=[[-1]*W for _ in range(H)]
R=[[-1]*W for _ in range(H)]

def u(i,j):
    if S[i][j]=='#':
        U[i][j]==-2
    elif i==0:
        U[i][j]=0
    elif U[i-1][j]==-1:
        a=0
        for k in range(i-1,-1,-1):
            if S[k][j]=='#': break
            else: a+=1
        U[i][j]=a
    elif U[i-1][j]==-2:
        U[i][j]=0
    else:
        U[i][j]=U[i-1][j]+1

def d(i,j):
    if S[i][j]=='#':
        D[i][j]==-2
    elif i==H-1:
        D[i][j]=0
    elif D[i+1][j]==-1:
        a=0
        for k in range(i+1,H):
            if S[k][j]=='#': break
            else: a+=1
        D[i][j]=a
    elif D[i+1][j]==-2:
        D[i][j]=0
    else:
        D[i][j]=D[i+1][j]+1

def l(i,j):
    if S[i][j]=='#':
        L[i][j]==-2
    elif j==0:
        L[i][j]=0
    elif L[i][j-1]==-1:
        a=0
        for k in range(j-1,-1,-1):
            if S[i][k]=='#': break
            else: a+=1
        L[i][j]=a
    elif L[i][j-1]==-2:
        L[i][j]=0
    else:
        L[i][j]=L[i][j-1]+1

def r(i,j):
    if S[i][j]=='#':
        R[i][j]==-2
    elif j==W-1:
        R[i][j]=0
    elif R[i][j+1]==-1:
        a=0
        for k in range(j+1,W):
            if S[i][k]=='#': break
            else: a+=1
        R[i][j]=a
    elif R[i][j+1]==-2:
        R[i][j]=0
    else:
        R[i][j]=R[i][j+1]+1

def udlr(i,j):
    if S[i][j]=='#':
        U[i][j]=D[i][j]=L[i][j]=R[i][j]=-2
        return 0
    else:
        if U[i][j]==-1:
            u(i,j)
        if D[i][j]==-1:
            d(i,j)
        if L[i][j]==-1:
            l(i,j)
        if R[i][j]==-1:
            r(i,j)
        return U[i][j]+D[i][j]+L[i][j]+R[i][j]+1

x=0
for i in range(H):
    for j in range(W):
        x=max(x,max(x,udlr(i,j)))
print(x)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
# Your code here!
H,W=map(int,input().split())
S=[]
ans=0

for _ in range(H):
    S.append(list(input()))

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

for h in range(H):
    count=0
    start=0
    for i in range(W):
        if S[h][i]=="#":
            count=0
            start=i+1
        else:
            count+=1
            l[h][i]+=count

for h in range(H):
    count=0
    start=W-1
    for i in range(W)[::-1]:
        if S[h][i]=="#":
            count=0
            start=i
        else:
            count+=1
            l[h][i]+=count

for w in range(W):
    count=0
    start=0
    for i in range(H):
        if S[i][w]=="#":
            count=0
            start=i+1
        else:
            count+=1
            l[i][w]+=count

for w in range(W):
    count=0
    start=H-1
    for i in range(H)[::-1]:
        if S[i][w]=="#":
            count=0
            start=i
        else:
            count+=1
            l[i][w]+=count
            ans=max(ans,l[i][w])

print(ans-3)

Submission status: Time Limit Exceeded

Solution code:
import sys

sys.setrecursionlimit(5000)
H, W = map(int, input().split())
s, dp, ans = [], {}, 0
for i in range(H):
    s.append(input())
def getRightCount(h, w):
    key = str(h) + str(w) + 'r'
    if key in dp:
        return dp[key]
    elif s[h][w] == '#':
        dp[key] = 0
    elif w == W - 1:
        dp[key] = 0
    elif s[h][w+1] == '#':
        dp[key] = 0
    else:
        dp[key] = 1 + getRightCount(h, w+1)
    return dp[key]
def getLeftCount(h, w):
    key = str(h) + str(w) + 'l'
    if key in dp:
        return dp[key]
    elif s[h][w] == '#':
        dp[key] = 0
    elif w == 0:
        dp[key] = 0
    elif s[h][w-1] == '#':
        dp[key] = 0
    else:
        dp[key] = 1 + getLeftCount(h, w-1)
    return dp[key]
def getDownCount(h, w):
    key = str(h) + str(w) + 'd'
    if key in dp:
        return dp[key]
    elif s[h][w] == '#':
        dp[key] = 0
    elif h == H - 1:
        dp[key] = 0
    elif s[h+1][w] == '#':
        dp[key] = 0
    else:
        dp[key] = 1 + getDownCount(h+1, w)
    return dp[key]
def getUpCount(h, w):
    key = str(h) + str(w) + 'u'
    if key in dp:
        return dp[key]
    elif s[h][w] == '#':
        dp[key] = 0
    elif h == 0:
        dp[key] = 0
    elif s[h-1][w] == '#':
        dp[key] = 0
    else:
        dp[key] = 1 + getUpCount(h-1, w)
    return dp[key]
def getCount(h, w):
    if s[h][w] == '#':
        return 0
    rCnt, lCnt, uCnt, dCnt = 0,0,0,0
    key = str(h) + str(w)
    # to right
    rkey = key + 'r'
    rCnt = getRightCount(h, w)
    # to left
    lkey = key + 'l'
    lCnt = getLeftCount(h, w)
    # to down
    dkey = key + 'd'
    dCnt = getDownCount(h, w)
    # to up
    ukey = key + 'u'
    uCnt = getUpCount(h, w)
    return rCnt + lCnt + uCnt + dCnt + 1

for i in reversed(range(H)):
    for j in reversed(range(W)):
        ans = max(ans, getCount(i, j))
print(ans)

Submission status: Time Limit Exceeded

Solution code:
H, W = map(int, input().split())
s = []
ans = 0
dp = {}
for i in range(H):
    s.append(input())

def getCount(h, w):
    if s[h][w] == '#':
        return 0
    rCnt = 0
    lCnt = 0
    uCnt = 0
    dCnt = 0
    key = str(h) + str(w)

    # to right
    rkey = key + 'r'
    if rkey in dp:
        rCnt = dp[rkey]
    else:
        for i in range(w+1, W):
            if s[h][i] == '.':
                rCnt += 1
            else:
                break
        dp[rkey] = rCnt

    # to left
    lkey = key + 'l'
    if lkey in dp:
        lCnt = dp[lkey]
    else:
        for i in reversed(range(w)):
            if s[h][i] == '.':
                lCnt += 1
            else:
                break
        dp[lkey] = lCnt

    # to down
    dkey = key + 'd'
    if dkey in dp:
        dCnt = dp[dkey]
    else:
        for i in range(h+1, H):
            if s[i][w] == '.':
                dCnt += 1
            else:
                break
        dp[dkey] = dCnt

    # to up
    ukey = key + 'u'
    if ukey in dp:
        uCnt = dp[ukey]
    else:
        for i in reversed(range(h)):
            if s[i][w] == '.':
                uCnt += 1
            else:
                break
        dp[ukey] = uCnt
    return rCnt + lCnt + uCnt + dCnt + 1

for i in reversed(range(H)):
    for j in reversed(range(W)):
        ans = max(ans, getCount(i, j))

print(ans)

Submission status: Time Limit Exceeded

Solution code:
H, W = map(int, input().split())
s = []
ans = 0

for i in range(H):
    s.append(input())

def getCount(h, w):
    if s[h][w] == '#':
        return 0
    cnt = 0

    if s[h][w] == '.':
        cnt += 1

    # to right
    for i in range(w+1, W):
        if s[h][i] == '.':
            cnt += 1
        else:
            break

    # to left
    #if w > 0:
    #for i in range(w - 1, -1, -1):
    for i in reversed(range(w)):
        if s[h][i] == '.':
            cnt += 1
        else:
            break

    # to down
    for i in range(h+1, H):
        if s[i][w] == '.':
            cnt += 1
        else:
            break

    # to up
    for i in reversed(range(h)):
        if s[i][w] == '.':
            cnt += 1
        else:
            break

    return cnt

for i in range(H):
    for j in range(W):
        ans = max(ans, getCount(i, j))

print(ans)

Submission status: Time Limit Exceeded

Solution code:
h,w = map(int,input().split())
l = [[0] *w for i in range(h)]
r = [[0] *w for i in range(h)]
u = [[0] *h for i in range(w)]
d = [[0] *h for i in range(w)]
s = [str(input()) for i in range(h)]
for i in range(h):
  for j in range(w):
    if s[i][j] == ".":
      if j == 0:
        l[i][j] = 1
      else:
        l[i][j] = l[i][j-1] +1
    if s[i][-(j+1)] == ".":
      if j == 0:
        r[i][-(j+1)] = 1
      else:
        r[i][-(j+1)] = r[i][-j] +1
for i in range(w):
  for j in range(h):
    if s[j][i] == ".":
      if j == 0:
        u[i][j] = 1
      else:
        u[i][j] = u[i][j-1] +1
    if s[-(j+1)][i] == ".":
      if j == 0:
        d[i][-(j+1)] = 1
      else:
        d[i][-(j+1)] = d[i][-j] +1
ans = 0
for i in range(h):
  for j in range(w):
    ans = max(ans,l[i][j]+r[i][j]+u[j][i]+d[j][i]-3)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
from itertools import accumulate
import bisect
import numpy as np
input = sys.stdin.readline
def inputs():return [int(x) for x in input().split()]
H,W = inputs()
meiro = [tuple(input()) for i in range(H)]
#print(meiro)
imosw = [[0]*(W) for _ in range(H)]
imosh = [[0]*(W) for _ in range(H)]
for i in range(H):
    cnt=0
    for j in range(W):
        if meiro[i][j]=="#":
            if cnt!=0:
                imosw[i][j]-=cnt
                imosw[i][j-cnt]+=cnt
            cnt = 0
        else:
            cnt+=1
    if cnt!=0:
    	imosw[i][W-1-cnt+1]+=cnt
for j in range(W):
    cnt= 0
    for i in range(H):
        if meiro[i][j]=="#":
            if cnt!=0:
                imosh[i][j]-=cnt
                imosh[i-cnt][j]+=cnt
            cnt = 0
        else:
            cnt+=1
    if cnt!=0:
    	imosh[H-1-cnt+1][j]+=cnt
imosw = np.cumsum(imosw, axis=1)
imosh = np.cumsum(imosh, axis=0)
ans = np.max(imosw+imosh)-1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
from itertools import accumulate
import bisect
import numpy as np
input = sys.stdin.readline
def inputs():return [int(x) for x in input().split()]
H,W = inputs()
meiro = [list(input()) for i in range(H)]
#print(meiro)
imosw = [[0]*(W) for _ in range(H)]
imosh = [[0]*(W) for _ in range(H)]
for i in range(H):
    cnt=0
    for j in range(W):
        if meiro[i][j]=="#":
            if cnt!=0:
                imosw[i][j]-=cnt
                imosw[i][j-cnt]+=cnt
            cnt = 0
        else:
            cnt+=1
    if cnt!=0:
    	imosw[i][W-1-cnt+1]+=cnt
for j in range(W):
    cnt= 0
    for i in range(H):
        if meiro[i][j]=="#":
            if cnt!=0:
                imosh[i][j]-=cnt
                imosh[i-cnt][j]+=cnt
            cnt = 0
        else:
            cnt+=1
    if cnt!=0:
    	imosh[H-1-cnt+1][j]+=cnt
imosw = np.cumsum(imosw, axis=1)
imosh = np.cumsum(imosh, axis=0)
ans = np.max(imosw+imosh)-1
print(ans)

Submission status: Time Limit Exceeded

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

left   = [[0] * 2005 for i in range(2005)]
right  = [[0] * 2005 for i in range(2005)]
top    = [[0] * 2005 for i in range(2005)]
bottom = [[0] * 2005 for i in range(2005)]

for h in range(H):
	cur = 0
	for w in range(W):
		if grid[h][w] == '#':
			left[h][w] = -1
			cur = 0
		else:
			left[h][w] = cur
			cur += 1
	cur = 0
	for w in range(W-1, -1, -1):
		if grid[h][w] == '#':
			right[h][w] = -1
			cur = 0
		else:
			right[h][w] = cur
			cur += 1

for w in range(W):
	cur = 0
	for h in range(H):
		if grid[h][w] == '#':
			top[h][w] = -1
			cur = 0
		else:
			top[h][w] = cur
			cur += 1

	cur = 0
	for h in range(H-1, -1, -1):
		if grid[h][w] == '#':
			bottom[h][w] = -1
			cur = 0
		else:
			bottom[h][w] = cur
			cur += 1

ans = 0
for h in range(H):
	for w in range(W):
		if grid[h][w] == '.':
			ans = max(ans, left[h][w] + right[h][w] + top[h][w] + bottom[h][w] + 1)
print(ans)