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:
import re
h,w = map(int, input().split())
m=[]
d = [[0]*w for _ in range(h)]
u = [[0]*w for _ in range(h)]
for _ in range(h):
  t=[]
  for i in re.split("(#)", input()):
    if not i == "#":
      t.extend([len(i)]*len(i))
    else:
      t.append(0)
  m.append(t)

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

    if m[h-1-j][i] == 0:
      u[h-1-j][i] = 0
    else:
      if h-1-j == h-1:
        u[h-1-j][i] = 1
      else:
        u[h-1-j][i] = u[h-j][i] + 1
ans=0
for i in range(w):
  for j in range(h):
    ans = max(ans, u[j][i]+d[j][i]+m[j][i])
print(ans - 2)

Submission status: Time Limit Exceeded

Solution code:
import re
h,w = map(int, input().split())
m = []
for _ in range(h):
  t=[]
  
  for i in re.split("(#)", input()):
    if not i == "#":
      t.extend([len(i)-1]*len(i))
    else:
      t.append(-1)
  m.append(t)

ans=0
tate=[]
for i in range(w):
  temp = []
  cnt = 0
  for j in range(h):
    if m[j][i] == -1:
      if cnt == 0:
        temp.extend([-1])
      else:
        temp.extend([cnt-1]*(cnt))
        temp.extend([-1])
      cnt = 0
    else:
      cnt += 1
  else:
    if cnt > 0:
      temp.extend([cnt-1]*(cnt))
  tate.append(temp)
ans=0
for i in range(w):
  for j in range(h):
    if m[j][i] == -1:
      continue
    ans = max(ans,tate[i][j] + m[j][i])
print(ans + 1)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3

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

    # L[i][j]: このマスが光る時に左方向に照らされるマスの個数
    L = [[0]*W for _ in range(H)]
    R = [[0]*W for _ in range(H)]

    for i in range(H):
        for j in range(1,W):
            if S[i][j] == '.':
                if S[i][j-1] == '.': 
                    L[i][j] = L[i][j-1] + 1
            if S[i][W-j-1] == '.':
                if S[i][W-j] == '.': 
                    R[i][W-j-1] = R[i][W-j] + 1
    
    # U[i][j]: このマスが光る時に上方向に照らされるマスの個数
    U = [[0]*W for _ in range(H)]
    D = [[0]*W for _ in range(H)]

    for j in range(W):
        for i in range(1,H):
            if S[i][j] == '.':
                if S[i-1][j] == '.': 
                    U[i][j] = U[i-1][j] + 1
            if S[H-i-1][j] == '.':
                if S[H-i][j] == '.': 
                    D[H-i-1][j] = D[H-i][j] + 1
    ans = 0
    
    for i in range(H):
        for j in range(W):
            if S[i][j] == '.':
                tmp = 1
                tmp += L[i][j] + R[i][j] + U[i][j] + D[i][j]
                ans = max(tmp,ans)
    
    print(ans)
    
if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
from collections import deque, Counter
from heapq import heappop, heappush
from bisect import bisect_right

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

    # L[i][j]: このマスが光る時に左方向に照らされるマスの個数
    L = [[0]*W for _ in range(H)]
    R = [[0]*W for _ in range(H)]

    for i in range(H):
        for j in range(1,W):
            if S[i][j] == '.':
                if S[i][j-1] == '.': 
                    L[i][j] = L[i][j-1] + 1
            if S[i][W-j-1] == '.':
                if S[i][W-j] == '.': 
                    R[i][W-j-1] = R[i][W-j] + 1
    
    # U[i][j]: このマスが光る時に上方向に照らされるマスの個数
    U = [[0]*W for _ in range(H)]
    D = [[0]*W for _ in range(H)]

    for j in range(W):
        for i in range(1,H):
            if S[i][j] == '.':
                if S[i-1][j] == '.': 
                    U[i][j] = U[i-1][j] + 1
            if S[H-i-1][j] == '.':
                if S[H-i][j] == '.': 
                    D[H-i-1][j] = D[H-i][j] + 1
    ans = 0
    
    for i in range(H):
        for j in range(W):
            tmp = 1
            tmp += L[i][j] + R[i][j] + U[i][j] + D[i][j]
            ans = max(tmp,ans)
    
    print(ans)
    
if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
H,W = map(int,input().split())
S = []
for i in range(H):
    S.append(list(input()))
L = [[] for _ in range(H)]
R = [[] for _ in range(H)]
D = [[] for _ in range(H)]
U = [[] for _ in range(H)]
for i in range(H):
    for j in range(W):
        if S[i][j] == '#':
            L[i].append(0)
        elif j == 0:
            L[i].append(1)
        else:
            L[i].append(L[i][-1] + 1)
for i in range(H):
    for j in range(-1,-W-1,-1):
        if S[i][j] == '#':
            R[i].insert(0,0)
        elif j == -1:
            R[i].append(1)
        else:
            R[i].insert(0,R[i][0] + 1)
for i in range(W):
    for j in range(H):
        if S[j][i] == '#':
            U[j].append(0)
        elif j == 0:
            U[j].append(1)
        else:
            U[j].append(U[j-1][-1] + 1)
for i in range(W):
    for j in range(-1,-H-1,-1):
        if S[j][i] == '#':
            D[j].append(0)
        elif j == -1:
            D[j].append(1)
        else:
            D[j].append(D[j+1][-1] + 1)
ans = 0
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)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
H,W = map(int,input().split())
S = []
for i in range(H):
    S.append(list(input()))
L = [[] for _ in range(H)]
R = [[] for _ in range(H)]
D = [[] for _ in range(H)]
U = [[] for _ in range(H)]
for i in range(H):
    for j in range(W):
        if S[i][j] == '#':
            L[i].append(0)
        elif j == 0:
            L[i].append(1)
        else:
            L[i].append(L[i][-1] + 1)
for i in range(H):
    for j in range(-1,-W-1,-1):
        if S[i][j] == '#':
            R[i].insert(0,0)
        elif j == -1:
            R[i].append(1)
        else:
            R[i].insert(0,R[i][0] + 1)
for i in range(W):
    for j in range(H):
        if S[j][i] == '#':
            U[j].append(0)
        elif j == 0:
            U[j].append(1)
        else:
            U[j].append(U[j-1][-1] + 1)
for i in range(W):
    for j in range(-1,-H-1,-1):
        if S[j][i] == '#':
            D[j].append(0)
        elif j == -1:
            D[j].append(1)
        else:
            D[j].append(D[j+1][-1] + 1)
ans = 0
for i in range(H):
    for j in range(W):
        if S[i][j] == '.':
            ans = max(ans,U[i][j]+D[i][j]+L[i][j]+R[i][j]-3)
print(ans)

Submission status: Time Limit Exceeded

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

m = []
for i in range(H):
    m.append(input().rstrip('\n'))

wei = [[0]*(W+1) for _ in range(H+1)]
for h in range(H):
    for w in range(W):
        if m[h][w] == '.':
             wei[h][w] = wei[h][w-1] + 1
        else:
            wei[h][w] = 0
    #reverse
    for wz in range(W-1, -1, -1):
        if wei[h][wz] > 0 and wei[h][wz] < wei[h][wz+1]:
                wei[h][wz] = wei[h][wz + 1]

hei = [[0]*(W+1) for _ in range(H+1)]
for w in range(W):
    for h in range(H):
        if m[h][w] == '.':
             hei[h][w] = hei[h-1][w] + 1
        else:
            hei[h][w] = 0

    #reverse
    for hz in range(H-1, -1, -1):
        if hei[hz][w] > 0 and hei[hz][w] < hei[hz+1][w]:
            hei[hz][w] = hei[hz+1][w]

ans = 0
for h in range(H):
    for w in range(W):
       val = wei[h][w] + hei[h][w] - 1
       if ans < val:
           ans = val

print(ans)

Submission status: Time Limit Exceeded

Solution code:
#!usr/bin/env python3
from collections import defaultdict, deque, Counter, OrderedDict
import collections, heapq, itertools, bisect
import math, fractions
import sys, copy

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI1(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline().rstrip())
def LS(): return [list(x) for x in sys.stdin.readline().split()]
def S(): return list(sys.stdin.readline().rstrip())
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
dire = [[1, 0], [0, 1], [-1, 0], [0, -1]]
dire8 = [[1, 0], [1, 1], [0, 1], [-1, 1], [-1, 0], [-1, -1], [0, -1], [1, -1]]
MOD = 1000000007

def main():
    H, W = LI()
    maze = SR(H)
    # top, right, bottom, left
    info = [[[0, 0, 0, 0] for _ in range(W)] for _ in range(H)]

    # from top to bottom
    for w in range(W):
        current = 0
        for h in range(H):
            if maze[h][w] == "#":
                current = 0
            else:
                info[h][w][0] = current
                current += 1

    # from right to left
    for h in range(H):
        current = 0
        for w in range(W)[::-1]:
            if maze[h][w] == "#":
                current = 0
            else:
                info[h][w][1] = current
                current += 1

    # from bottom to top
    for w in range(W):
        current = 0
        for h in range(H)[::-1]:
            if maze[h][w] == "#":
                current = 0
            else:
                info[h][w][2] = current
                current += 1

    # from  left to right
    for h in range(H):
        current = 0
        for w in range(W):
            if maze[h][w] == "#":
                current = 0
            else:
                info[h][w][3] = current
                current += 1

    ans = 0
    for h in range(H):
        for w in range(W):
            if maze[h][w] == ".":
                ans = max(ans, sum(info[h][w]) + 1)
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
#!usr/bin/env python3
from collections import defaultdict, deque, Counter, OrderedDict
import collections, heapq, itertools, bisect
import math, fractions
import sys, copy

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI1(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline().rstrip())
def LS(): return [list(x) for x in sys.stdin.readline().split()]
def S(): return list(sys.stdin.readline().rstrip())
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
dire = [[1, 0], [0, 1], [-1, 0], [0, -1]]
dire8 = [[1, 0], [1, 1], [0, 1], [-1, 1], [-1, 0], [-1, -1], [0, -1], [1, -1]]
MOD = 1000000007

def main():
    H, W = LI()
    maze = SR(H)
    # top, right, bottom, left
    info = [[[0, 0, 0, 0] for _ in range(W)] for _ in range(H)]

    # from top to bottom
    for w in range(W):
        current = 0
        for h in range(H):
            if maze[h][w] == "#":
                current = 0
            else:
                info[h][w][0] = current
                current += 1

    # from right to left
    for h in range(H):
        current = 0
        for w in range(W)[::-1]:
            if maze[h][w] == "#":
                current = 0
            else:
                info[h][w][1] = current
                current += 1

    # from bottom to top
    for w in range(W):
        current = 0
        for h in range(H)[::-1]:
            if maze[h][w] == "#":
                current = 0
            else:
                info[h][w][2] = current
                current += 1

    ans = 0
    # from  left to right
    for h in range(H):
        current = 0
        for w in range(W):
            if maze[h][w] == "#":
                current = 0
            else:
                info[h][w][3] = current
                ans = max(ans, sum(info[h][w]) + 1)
                current += 1

    print(ans)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

toUP = [[0] * w for _ in range(h)]
for i in range(w):
  cnt = 0
  for j in range(h):
    if S[j][i] == ".":
      cnt += 1
      toUP[j][i] = cnt
    else:
      cnt = 0

toDOWN = [[0] * w for _ in range(h)]
for i in range(w):
  cnt = 0
  for j in range(h - 1, -1, -1):
    if S[j][i] == ".":
      cnt += 1
      toDOWN[j][i] = cnt
    else:
      cnt = 0

toLEFT = [[0] * w for _ in range(h)]
for i in range(h):
  cnt = 0
  for j in range(w):
    if S[i][j] == ".":
      cnt += 1
      toLEFT[i][j] = cnt
    else:
      cnt = 0
    
toRIGHT = [[0] * w for _ in range(h)]
for i in range(h):
  cnt = 0
  for j in range(w - 1, -1, -1):
    if S[i][j] == ".":
      cnt += 1
      toRIGHT[i][j] = cnt
    else:
      cnt = 0

ans = 0
for i in range(h):
  for j in range(w):
    if S[i][j] == ".":
      ans = max(ans, toUP[i][j] + toDOWN[i][j] + toLEFT[i][j] + toRIGHT[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)]

TS = list(zip(*S))

toUP = [[0] * w for _ in range(h)]
for i in range(w):
  cnt = 0
  for j in range(h):
    if S[j][i] == ".":
      cnt += 1
      toUP[j][i] = cnt
    else:
      cnt = 0

toDOWN = [[0] * w for _ in range(h)]
for i in range(w):
  cnt = 0
  for j in range(h - 1, -1, -1):
    if S[j][i] == ".":
      cnt += 1
      toDOWN[j][i] = cnt
    else:
      cnt = 0


toLEFT = [[0] * w for _ in range(h)]
for i in range(h):
  cnt = 0
  for j in range(w):
    if S[i][j] == ".":
      cnt += 1
      toLEFT[i][j] = cnt
    else:
      cnt = 0
    
toRIGHT = [[0] * w for _ in range(h)]
for i in range(h):
  cnt = 0
  for j in range(w - 1, -1, -1):
    if S[i][j] == ".":
      cnt += 1
      toRIGHT[i][j] = cnt
    else:
      cnt = 0

ans = 0
for i in range(h):
  for j in range(w):
    if S[i][j] == ".":
      ans = max(ans, toUP[i][j] + toDOWN[i][j] + toLEFT[i][j] + toRIGHT[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)]

TS = list(zip(*S))

TtoUP = [[0] * h for _ in range(w)]
for itr1, line in enumerate(TS):
  cnt = 0
  for itr2, s in enumerate(line):
    if s == ".":
      cnt += 1
      TtoUP[itr1][itr2] = cnt
    else:
      cnt = 0
toUP = list(zip(*TtoUP))


TtoDOWN = [[0] * h for _ in range(w)]
for itr1, line in enumerate(TS):
  cnt = 0
  for itr2, s in enumerate(line[::-1]):
    if s == ".":
      cnt += 1
      TtoDOWN[itr1][h - itr2- 1] = cnt
    else:
      cnt = 0
toDOWN = list(zip(*TtoDOWN))


toLEFT = [[0] * w for _ in range(h)]
for itr1, line in enumerate(S):
  cnt = 0
  for itr2, s in enumerate(line):
    if s == ".":
      cnt += 1
      toLEFT[itr1][itr2] = cnt
    else:
      cnt = 0
    
toRIGHT = [[0] * w for _ in range(h)]
for itr1, line in enumerate(S):
  cnt = 0
  for itr2, s in enumerate(line[::-1]):
    if s == ".":
      cnt += 1
      toRIGHT[itr1][w - itr2 - 1] = cnt
    else:
      cnt = 0

ans = 0
for i in range(h):
  for j in range(w):
    if S[i][j] == ".":
      ans = max(ans, toUP[i][j] + toDOWN[i][j] + toLEFT[i][j] + toRIGHT[i][j] - 3)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
H, W = map(int, input().split())
S = []
for i in range(H):
  cha = str(input())
  s = [i for i in cha]
  S.append(s)
  
def side(li, h, w):
  left = w
  right = w
  i = 0
  if w == 0:
    left = 0
  else:
    while S[h][w-i] == '.':
      if i == w:
        left = 0
        break
      i += 1
    else:
      left = w-i+1
  i = 0
  if w == len(S[h])-1:
    right = len(S[h])-1
  else:
    while S[h][w+i] == '.':
      if w+i == len(S[h])-1:
        right = len(S[h])-1
        break
      i += 1
    else:
      right = w+i-1
  return right-left+1

def front(S, h, w):
  up = h
  down = h
  i = 0
  if h == 0:
    up = 0
  else:
    while S[h-i][w] == '.':
      if i == h:
        up = 0
        break
      i += 1
    else:
      up = h-i+1
  i = 0
  if h == len(S)-1:
    down = len(S)-1
  else:
    while S[h+i][w] == '.':
      if h+i == len(S)-1:
        down = len(S)-1
        break
      i += 1
    else:
      down = h+i-1
  return down-up+1

ans = 0
for i in range(H):
  for j in range(W):
    if S[i][j] == '.':
      ans =  max(ans, side(S, i, j) + front(S, i, j) - 1)
print(ans)

Submission status: Time Limit Exceeded

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

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

    DX = [0, 1, 0, -1]
    DY = [1, 0, -1, 0]

    #print(*g, sep="\n")


    def cnt_dot(k, l):
        if g[k][l] == ".":
            cnt = 1
            for dx, dy in zip(DX, DY):
                x, y = k + dx, l + dy
                while g[x][y] == ".":
                    cnt += 1
                    x, y = x + dx, y + dy
            return cnt
        else:
            return 0


    res = 0
    for i in range(h + 2):
        for j in range(w + 2):
            res = max(res, cnt_dot(i, j))
    print(res)
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

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

DX = [0, 1, 0, -1]
DY = [1, 0, -1, 0]

#print(*g, sep="\n")


def cnt_dot(k, l):
    if g[k][l] == ".":
        cnt = 1
        for dx, dy in zip(DX, DY):
            x, y = k + dx, l + dy
            while g[x][y] == ".":
                cnt += 1
                x, y = x + dx, y + dy
        return cnt
    else:
        return 0


res = 0
for i in range(h + 2):
    for j in range(w + 2):
        res = max(res, cnt_dot(i, j))
print(res)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
from collections import deque, Counter
from heapq import heappop, heappush
from bisect import bisect_right

def main():
    H, W = map(int, input().split())
    S = [input() for _ in range(H)]
    adj = [[] for _ in range(H*W)]
    dx = [1,-1,0,0]
    dy = [0,0,1,-1]
    ans = 0
    
    for i in range(H):
        for j in range(W):
            if S[i][j] == '.':
                tmp = 1
                for k in range(4):
                    y = i + dy[k]
                    x = j + dx[k]
                    while 0 <= y < H and 0 <= x < W:
                        if S[y][x] == '.':
                            tmp += 1
                            y += dy[k]
                            x += dx[k]
                        else:
                            break
                ans = max(ans, tmp)
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
from collections import deque, Counter
from heapq import heappop, heappush
from bisect import bisect_right

def main():
    H, W = map(int, input().split())
    S = [input() for _ in range(H)]
    adj = [[] for _ in range(H*W)]
    dx = [1,-1,0,0]
    dy = [0,0,1,-1]
    ans = 0
    
    for i in range(H):
        for j in range(W):
            if S[i][j] == '.':
                tmp = 1
                for k in range(4):
                    y = i + dy[k]
                    x = j + dx[k]
                    while 0 <= y < H and 0 <= x < W:
                        if S[y][x] == '.':
                            tmp += 1
                            y += dy[k]
                            x += dx[k]
                        else:
                            break
                ans = max(ans, tmp)
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
h,w = map(int, input().split())
g = []
for _ in range(h):
  g.append(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 g[i][j] == '#':
      l[i][j] = 0
      u[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 reversed(range(h)):
  for j in reversed(range(w)):
    if g[i][j] == '#':
      r[i][j] = 0
      d[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):
    ans = max(l[i][j]+r[i][j]+u[i][j]+d[i][j]-3,ans)
print(ans)

Submission status: Time Limit Exceeded

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

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

for i in range(H):
    for j in range(W):
        if S[i][j] == '#':
            U[i][j] = 0
        elif i == 0 and S[i][j] == '.':
            U[i][j] = 1
        else:
            U[i][j] = U[i-1][j] + 1
            
    for j in range(W):
        if S[i][j] == '#':
            L[i][j] = 0
        elif j == 0 and S[i][j] == '.':
            L[i][j] = 1
        else:
            L[i][j] = L[i][j-1] + 1
            
for i in range(H-1,-1,-1):
    for j in range(W-1,-1,-1):
        if S[i][j] == '#':
            D[i][j] = 0
        elif i == H-1 and S[i][j] == '.':
            D[i][j] = 1
        else:
            D[i][j] = D[i+1][j] + 1
            
    for j in range(W-1,-1,-1):
        if S[i][j] == '#':
            R[i][j] = 0
        elif j == W-1 and S[i][j] == '.':
            R[i][j] = 1
        else:
            R[i][j] = R[i][j+1] + 1
            
ans = 0 

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

Submission status: Time Limit Exceeded

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

left_cnts = []
for hi, row in enumerate(sl):
    curr_left_cnts = []
    curr_cnt = 0
    for i, s in enumerate(row):
        if s == '.':
            curr_left_cnts.append(curr_cnt)
            curr_cnt += 1
        else:
            curr_left_cnts.append(-1)
            curr_cnt = 0
    left_cnts.append(curr_left_cnts)


right_cnts = []
for hi, row in enumerate(sl):
    curr_right_cnts = []
    curr_cnt = 0
    for i, s in enumerate(row[::-1]):
        if s == '.':
            curr_right_cnts.append(curr_cnt)
            curr_cnt += 1
        else:
            curr_right_cnts.append(-1)
            curr_cnt = 0
    curr_right_cnts = curr_right_cnts[::-1]
    right_cnts.append(curr_right_cnts)


sl_t = [list(x) for x in zip(*sl)]
left_cnts_t = []
for hi, row in enumerate(sl_t):
    curr_left_cnts = []
    curr_cnt = 0
    for i, s in enumerate(row):
        if s == '.':
            curr_left_cnts.append(curr_cnt)
            curr_cnt += 1
        else:
            curr_left_cnts.append(-1)
            curr_cnt = 0
    left_cnts_t.append(curr_left_cnts)


right_cnts_t = []
for hi, row in enumerate(sl_t):
    curr_right_cnts = []
    curr_cnt = 0
    for i, s in enumerate(row[::-1]):
        if s == '.':
            curr_right_cnts.append(curr_cnt)
            curr_cnt += 1
        else:
            curr_right_cnts.append(-1)
            curr_cnt = 0
    curr_right_cnts = curr_right_cnts[::-1]
    right_cnts_t.append(curr_right_cnts)


ans = 0
for hi in range(h):
    for wi in range(w):
        if sl[hi][wi] == '#':continue
        cnt = left_cnts[hi][wi] + right_cnts[hi][wi] + left_cnts_t[wi][hi] + right_cnts_t[wi][hi]
        ans = max(ans, cnt)

print(ans+1)

Submission status: Time Limit Exceeded

Solution code:
def ko(h,w,j):
  #hは配列
  a = [0 for _ in range(w)]
  for k in range(w):
    if h[k]=='.':
      cou = 0
      t = k
      s = k
      while t>0 and h[t-1]=='.':
        cou += 1
        t -= 1
    #  print("left",cou)
      while s+1<w and h[s+1]=='.':
        cou += 1
        s += 1
     # print("right",cou)
      cou += 1
      a[k] += cou
      #print(a)
  return a[j]
      
h,w = map(int,input().split())
s = [list(input()) for i in range(h)]
s1 = [list(x) for x in zip(*s)]

ma = 0
for i in range(h):
  for j in range(w):
    c = s[i][j]
    if c==".":
      ma1 = ko(s[i],w,j) + ko(s1[j],h,i)
      ma = max(ma,ma1-1)
print(ma)

Submission status: Time Limit Exceeded

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

light_w=[[0]*w for _ in range(h)]
light_h=[[0]*w for _ in range(h)]

for i in range(w):
  cnt_up = 0
  cnt_down = 0
  for j in range(h):
    if maze[j][i] == "#":
      cnt_up=0
    else:
      light_w[j][i] += cnt_up
      cnt_up+=1

    if maze[h-j-1][i] == "#":
      cnt_down=0
    else:
      light_w[h-j-1][i] += cnt_down
      cnt_down+=1

for i in range(h):
  cnt_right = 0
  cnt_left = 0
  for j in range(w):
    if maze[i][j] == "#":
      cnt_left=0
    else:
      light_h[i][j] += cnt_left
      cnt_left+=1

    if maze[i][w-j-1] == "#":
      cnt_right=0
    else:
      light_h[i][w-j-1] += cnt_right
      cnt_right+=1
ans=0
for i in range(h):
  for j in range(w):
    ans=max(ans,light_h[i][j]+light_w[i][j])
print(ans+1)

Submission status: Time Limit Exceeded

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees, log2, log
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
from decimal import Decimal
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10**9 + 7
from decimal import *
 
H, W = MAP()
S = [input() for _ in range(H)]

w = [[-1]*W for _ in range(H)]
h = [[-1]*W for _ in range(H)]

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


for i in range(W):
	cnt = 0
	for j in range(H):
		if S[j][i] == ".":
			cnt += 1
			h[j][i] = cnt
		else:
			cnt = 0

	for j in range(H-2, -1, -1):
		if S[j][i] == ".":
			h[j][i] = max(h[j][i], h[j+1][i])


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

Submission status: Time Limit Exceeded

Solution code:
def main():
    H, W = map(int, input().split())
    A = [list(input()) for i in range(H)]
    right = [[0] * W for _ in range(H)]
    left = [[0] * W for _ in range(H)]
    upper = [[0] * W for _ in range(H)]
    under = [[0] * W for _ in range(H)]

    num_max_loc = 0

    for i in range(H):
        tmp_num = 0
        for j in range(W):
            if A[i][j] == "#":
                tmp_num = 0
            else:
                tmp_num += 1
                left[i][j] = tmp_num

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

    for i in range(W):
        tmp_num = 0
        for j in range(H):
            if A[j][i] == "#":
                tmp_num = 0
            else:
                tmp_num += 1
                upper[j][i] = tmp_num

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

    for i in range(H):
        for j in range(W):
            tmp_sum = (
                left[i][j] + right[i][j] + upper[i][j] + under[i][j] - 3)
            num_max_loc = max(num_max_loc, tmp_sum)

    print(num_max_loc)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys 
sys.setrecursionlimit(10**7)
h,w = map(int,input().split())##広さはh×w
c = [list(input()) for i in range(h)]#道の状態を記録するリスト

def dfs1(x,y):
    if not(0 <= x < h) or not(0 <= y < w) or c[x][y] == "#":#壁に当たったり，場外に出た場合はreturn
        return 0
    else:
        return 1 + dfs1(x+1,y)

def dfs2(x,y):
    if not(0 <= x < h) or not(0 <= y < w) or c[x][y] == "#":#壁に当たったり，場外に出た場合はreturn
        return 0
    else:
        return 1 + dfs2(x-1,y)
    
def dfs3(x,y):
    if not(0 <= x < h) or not(0 <= y < w) or c[x][y] == "#":#壁に当たったり，場外に出た場合はreturn
        return 0
    else:
        return 1 + dfs3(x,y+1)

def dfs4(x,y):
    if not(0 <= x < h) or not(0 <= y < w) or c[x][y] == "#":#壁に当たったり，場外に出た場合はreturn
        return 0
    else:
        return 1 + dfs4(x,y-1)
    
total = 0     
for i in range(h):
    for j in range(w):
        if c[i][j] == ".":
            sx,sy = i,j #スタート位置を定める
                     
            total = max(dfs1(sx,sy)+dfs2(sx,sy)+dfs3(sx,sy)+dfs4(sx,sy),total)
            

print(total-3)