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 sys
sys.setrecursionlimit(10**6)
from math import floor,ceil,sqrt,factorial,log
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
from operator import itemgetter
from fractions import gcd
mod = 10 ** 9 + 7
 
#整数input
def ii(): return int(sys.stdin.readline().rstrip()) #int(input())
def mii(): return map(int,sys.stdin.readline().rstrip().split())
def limii(): return list(mii()) #list(map(int,input().split()))
def lin(n:int): return [ii() for _ in range(n)]
def llint(n: int): return [limii() for _ in range(n)]
#文字列input
def ss(): return sys.stdin.readline().rstrip() #input()
def mss(): return sys.stdin.readline().rstrip()
def limss(): return list(mss()) #list(input().split())
def lst(n:int): return [ss() for _ in range(n)]
def llstr(n: int): return [limss() for _ in range(n)]

#本当に貪欲法か？ DP法では？？
#本当に貪欲法か？ DP法では？？
#本当に貪欲法か？ DP法では？？

h,w = mii()
a = llstr(h)
sy,sx = 1,1

ans=0
for sy in range(h):
    for sx in range(w):
        if a[sy][sx]==".":
            cnt=1
            
            for di,dj,dia in ((sy+1,sx,1),(sy-1,sx,2),(sy,sx-1,3),(sy,sx+1,4)):
                chk=True
                while (0 <= di <= h-1) and (0 <= dj <= w-1) and chk:
                    if a[di][dj]==".":
                        cnt+=1
                        if dia ==1:
                            di+=1
                        elif dia==2:
                            di-=1
                        elif dia==3:
                            dj-=1
                        elif dia==4:
                            dj+=1
                    else:
                        chk=False
            #print(cnt)
            ans=max(cnt,ans)
print(ans)

Submission status: Time Limit Exceeded

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

# そのマスを含めてLeft, Right, Up, Downにそれぞれ何マス分明かりを照らせるかを配列に記録
L = [[0] * W for _ in range(H)]
R = [[0] * W for _ in range(H)]
D = [[0] * W for _ in range(H)]
U = [[0] * W for _ 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

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

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

for j in range(W):
	for i in range(H):
		if S[i][j] == ".":
			if i == 0:
				U[i][j] = 1
			else:
				U[i][j] = U[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] = L[i][j] + R[i][j] + D[i][j] + U[i][j] - 3
	ans = max(ans, max(X[i]))

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

def calculate(h, w, arr):
    L = np.zeros([h, w])
    U = np.zeros([h, w])
    D = np.zeros([h, w])
    R = np.zeros([h, w])

    for ih in range(h):
        for iw in range(w):
            if arr[ih][iw] == ".":
                if ih == 0:
                    U[ih][iw] = 1
                else:
                    U[ih][iw] = U[ih - 1][iw] + 1


    for ih in range(h):
        for iw in range(w):
            if arr[ih][iw] == ".":
                if iw == 0:
                    L[ih][iw] = 1
                else:
                    L[ih][iw] = L[ih][iw-1] + 1


    for ih in range(h):
        for iw in range(w):
            rih = h - 1 - ih
            riw = w - 1 - iw
            if arr[rih][riw] == ".":
                if rih == h - 1:
                    D[rih][riw] = 1
                else:
                    D[rih][riw] = D[rih + 1][riw] + 1



    for ih in range(h):
        for iw in range(w):
            rih = h - 1 - ih
            riw = w - 1 - iw
            if arr[rih][riw] == ".":
                if riw == w - 1:
                    R[rih][riw] = 1
                else:
                    R[rih][riw] = R[rih][riw + 1] + 1

    max = 0
    for ih in range(h):
        for iw in range(w):
            if arr[ih][iw] == "#":
                continue
            value = L[ih][iw] + R[ih][iw] + U[ih][iw] + D[ih][iw] - 3
            if value > max:
                max = value
    print(int(max))


S = input().split(" ")
H = int(S[0])
W = int(S[1])

arr = []
for i in range(H):
    arr.append(input())

calculate(H, W, arr)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

def calculate(h, w, arr):
    L = np.zeros([h, w])
    U = np.zeros([h, w])
    D = np.zeros([h, w])
    R = np.zeros([h, w])

    for ih in range(h):
        for iw in range(w):
            if arr[ih][iw] == ".":
                if ih == 0:
                    U[ih][iw] = 1
                if iw == 0:
                    L[ih][iw] = 1

                if iw > 0:
                    L[ih][iw] = L[ih][iw - 1] + 1
                if ih > 0:
                    U[ih][iw] = U[ih - 1][iw] + 1


    for ih in range(h):
        for iw in range(w):
            rih = h - 1 - ih
            riw = w - 1 - iw
            if arr[rih][riw] == ".":
                if rih == h - 1:
                    D[rih][riw] = 1
                if riw == w - 1:
                    R[rih][riw] = 1

                if riw < w - 1:
                    R[rih][riw] = R[rih][riw + 1] + 1
                if rih < h - 1:
                    D[rih][riw] = D[rih + 1][riw] + 1

    max = 0
    for ih in range(h):
        for iw in range(w):
            if arr[ih][iw] == "#":
                continue
            value = L[ih][iw] + R[ih][iw] + U[ih][iw] + D[ih][iw] - 3
            if value > max:
                max = value
    print(int(max))


S = input().split(" ")
H = int(S[0])
W = int(S[1])

arr = []
for i in range(H):
    arr.append(input())

calculate(H, W, arr)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

def calculate(h, w, arr):
    L = np.zeros([h, w])
    U = np.zeros([h, w])
    D = np.zeros([h, w])
    R = np.zeros([h, w])

    for ih in range(h):
        for iw in range(w):
            if arr[ih][iw] == ".":
                if ih == 0:
                    U[ih][iw] = 1
                if iw == 0:
                    L[ih][iw] = 1

                if iw > 0:
                    L[ih][iw] = L[ih][iw - 1] + 1
                if ih > 0:
                    U[ih][iw] = U[ih - 1][iw] + 1


    for ih in range(h):
        for iw in range(w):
            rih = h - 1 - ih
            riw = w - 1 - iw
            if arr[rih][riw] == ".":
                if rih == h - 1:
                    D[rih][riw] = 1
                if riw == w - 1:
                    R[rih][riw] = 1

                if riw < w - 1:
                    R[rih][riw] = R[rih][riw + 1] + 1
                if rih < h - 1:
                    D[rih][riw] = D[rih + 1][riw] + 1

    max = 0
    for ih in range(h):
        for iw in range(w):
            if arr[ih][iw] == "#":
                continue
            value = L[ih][iw] + R[ih][iw] + U[ih][iw] + D[ih][iw] - 3
            if value > max:
                max = value
    print(int(max))


S = input().split(" ")
H = int(S[0])
W = int(S[1])

arr = []
for i in range(H):
    q = list(input())
    arr.append(q)

calculate(H, W, arr)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
import sys
sys.setrecursionlimit(20000000)

def calculate(h, w, arr):
    L = np.zeros([h, w])
    U = np.zeros([h, w])
    D = np.zeros([h, w])
    R = np.zeros([h, w])

    for ih in range(h):
        for iw in range(w):
            if ih == 0:
                U[ih][iw] = 1
            if iw == 0:
                L[ih][iw] = 1

            if arr[ih][iw] == "#":
                L[ih][iw] = 0
                U[ih][iw] = 0

            else:
                if iw > 0:
                    L[ih][iw] = L[ih][iw - 1] + 1
                if ih > 0:
                    U[ih][iw] = U[ih - 1][iw] + 1


    for ih in range(h):
        for iw in range(w):
            rih = h - 1 - ih
            riw = w - 1 - iw

            if rih == h - 1:
                D[rih][riw] = 1
            if riw == w - 1:
                R[rih][riw] = 1

            if arr[rih][riw] == "#":
                D[rih][riw] = 0
                R[rih][riw] = 0
            else:
                if riw < w - 1:
                    R[rih][riw] = R[rih][riw + 1] + 1
                if rih < h - 1:
                    D[rih][riw] = D[rih + 1][riw] + 1

    max = 0
    for ih in range(h):
        for iw in range(w):
            if arr[ih][iw] == "#":
                continue
            value = L[ih][iw] + R[ih][iw] + U[ih][iw] + D[ih][iw] - 3
            if value > max:
                max = value
    print(int(max))


S = input().split(" ")
H = int(S[0])
W = int(S[1])

arr = []
for i in range(H):
    q = list(input())
    arr.append(q)

calculate(H, W, arr)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
import sys
sys.setrecursionlimit(20000000)

def calculate(h, w, arr):
    L = np.zeros([h, w])
    U = np.zeros([h, w])
    D = np.zeros([h, w])
    R = np.zeros([h, w])

    for ih in range(h):
        for iw in range(w):
            if ih == 0:
                U[ih][iw] = 1
            if iw == 0:
                L[ih][iw] = 1

            if arr[ih][iw] == "#":
                L[ih][iw] = 0
                U[ih][iw] = 0

            else:
                if iw > 0:
                    L[ih][iw] = L[ih][iw - 1] + 1
                if ih > 0:
                    U[ih][iw] = U[ih - 1][iw] + 1

            rih = h - 1 - ih
            riw = w - 1 - iw

            if rih == h - 1:
                D[rih][riw] = 1
            if riw == w - 1:
                R[rih][riw] = 1

            if arr[rih][riw] == "#":
                D[rih][riw] = 0
                R[rih][riw] = 0
            else:
                if riw < w - 1:
                    R[rih][riw] = R[rih][riw + 1] + 1
                if rih < h - 1:
                    D[rih][riw] = D[rih + 1][riw] + 1

    max = 0
    for ih in range(h):
        for iw in range(w):
            if arr[ih][iw] == "#":
                continue
            value = L[ih][iw] + R[ih][iw] + U[ih][iw] + D[ih][iw] - 3
            if value > max:
                max = value
    print(int(max))


S = input().split(" ")
H = int(S[0])
W = int(S[1])

arr = []
for i in range(H):
    q = list(input())
    arr.append(q)

calculate(H, W, arr)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

def calculate(h, w, arr):
    L = np.zeros([h, w])
    U = np.zeros([h, w])
    D = np.zeros([h, w])
    R = np.zeros([h, w])

    for ih in range(h):
        for iw in range(w):
            if ih == 0:
                U[ih][iw] = 1
            if iw == 0:
                L[ih][iw] = 1

            if arr[ih][iw] == "#":
                L[ih][iw] = 0
                U[ih][iw] = 0

            else:
                if iw > 0:
                    L[ih][iw] = L[ih][iw - 1] + 1
                if ih > 0:
                    U[ih][iw] = U[ih - 1][iw] + 1

            rih = h - 1 - ih
            riw = w - 1 - iw

            if rih == h - 1:
                D[rih][riw] = 1
            if riw == w - 1:
                R[rih][riw] = 1

            if arr[rih][riw] == "#":
                D[rih][riw] = 0
                R[rih][riw] = 0
            else:
                if riw < w - 1:
                    R[rih][riw] = R[rih][riw + 1] + 1
                if rih < h - 1:
                    D[rih][riw] = D[rih + 1][riw] + 1

    max = 0
    for ih in range(h):
        for iw in range(w):
            if arr[ih][iw] == "#":
                continue
            value = L[ih][iw] + R[ih][iw] + U[ih][iw] + D[ih][iw] - 3
            if value > max:
                max = value
    print(int(max))


S = input().split(" ")
H = int(S[0])
W = int(S[1])

arr = []
for i in range(H):
    q = list(input())
    arr.append(q)

calculate(H, W, arr)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

def calculate(h, w, arr):
    L = np.zeros([h, w])
    U = np.zeros([h, w])
    D = np.zeros([h, w])
    R = np.zeros([h, w])

    for ih in range(h):
        for iw in range(w):
            if ih == 0:
                U[ih][iw] = 1
            if iw == 0:
                L[ih][iw] = 1

            if arr[ih][iw] == "#":
                L[ih][iw] = 0
                U[ih][iw] = 0

            else:
                if iw > 0:
                    L[ih][iw] = L[ih][iw - 1] + 1
                if ih > 0:
                    U[ih][iw] = U[ih - 1][iw] + 1

            rih = h - 1 - ih
            riw = w - 1 - iw

            if rih == h - 1:
                D[rih][riw] = 1
            if riw == w - 1:
                R[rih][riw] = 1

            if arr[rih][riw] == "#":
                D[rih][riw] = 0
                R[rih][riw] = 0
            else:
                if riw < w - 1:
                    R[rih][riw] = R[rih][riw + 1] + 1
                if rih < h - 1:
                    D[rih][riw] = D[rih + 1][riw] + 1

    max = 0
    for ih in range(h):
        for iw in range(w):
            value = L[ih][iw] + R[ih][iw] + U[ih][iw] + D[ih][iw] - 3
            if value > max:
                max = value
    print(int(max))


S = input().split(" ")
H = int(S[0])
W = int(S[1])

arr = []
for i in range(H):
    q = list(input())
    arr.append(q)

calculate(H, W, arr)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np


def calculate(h, w, arr):
    L = np.zeros([h, w])
    U = np.zeros([h, w])
    D = np.zeros([h, w])
    R = np.zeros([h, w])

    for ih in range(h):
        for iw in range(w):
            if ih == 0:
                U[ih][iw] = 1
            if iw == 0:
                L[ih][iw] = 1

            if arr[ih][iw] == 1:
                L[ih][iw] = 0
                U[ih][iw] = 0

            else:
                if iw > 0:
                    L[ih][iw] = L[ih][iw - 1] + 1
                if ih > 0:
                    U[ih][iw] = U[ih - 1][iw] + 1

            rih = h - 1 - ih
            riw = w - 1 - iw

            if rih == h - 1:
                D[rih][riw] = 1
            if riw == w - 1:
                R[rih][riw] = 1

            if arr[rih][riw] == 1:
                D[rih][riw] = 0
                R[rih][riw] = 0
            else:
                if riw < w - 1:
                    R[rih][riw] = R[rih][riw + 1] + 1
                if rih < h - 1:
                    D[rih][riw] = D[rih + 1][riw] + 1

    max = 0
    for ih in range(h):
        for iw in range(w):
            value = L[ih][iw] + R[ih][iw] + U[ih][iw] + D[ih][iw] - 3
            if value > max:
                max = value
    print(int(max))


S = input().split(" ")
H = int(S[0])
W = int(S[1])

arr = []
for i in range(H):
    q = input()
    tmp = []
    for qs in q:
        if qs == "#":
            tmp.append(1)
        if qs == ".":
            tmp.append(0)
    arr.append(tmp)

calculate(H, W, arr)

Submission status: Time Limit Exceeded

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

count = 0
answer = 0

for i in range(H):
    for j in range(W):

        if U[i][j] == ".":
            for k in reversed(range(j)):
                if U[i][k] == ".":
                    count += 1
                if U[i][k] == "#":
                    break

            for k in range(j + 1, W):
                if U[i][k] == ".":
                    count += 1
                if U[i][k] == "#":
                    break

            for k in reversed(range(i)):
                if U[k][j] == ".":
                    count += 1
                if U[k][j] == "#":
                    break

            for k in range(i + 1, H):
                if U[k][j] == ".":
                    count += 1
                if U[k][j] == "#":
                    break

            if answer < count:
                answer = count

        count = 0

print(answer + 1)

Submission status: Time Limit Exceeded

Solution code:
H, W = [int(a) for a in input().split()]
S = [None] * H

for i in range(H):
    S[i] = input()


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

# 横を調べる
for i in range(H):
    j = 0
    while j < W:
        horizontal = 0
        while j < W and S[i][j] == '.':
            horizontal += 1
            j += 1
        for jj in range(horizontal):
            light[i][j - 1 - jj][0] = horizontal
        while j < W and S[i][j] == '#':
            j += 1

# 縦を調べる
for j in range(W):
    i = 0
    while i < H:
        vertical = 0
        while i < H and S[i][j] == '.':
            vertical += 1
            i += 1
        for ii in range(vertical):
            light[i - 1 - ii][j][1] = vertical
            ans = max( ans, sum(light[i - 1 - ii][j]) - 1)
        while i < H and S[i][j] == '#':
            i += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
H, W = [int(a) for a in input().split()]
S = [None] * H

for i in range(H):
    S[i] = input()

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

# 横を調べる
for i in range(H):
    j = 0
    while j < W:
        horizontal = 0
        while j < W and S[i][j] == '.':
            horizontal += 1
            j += 1
        for jj in range(horizontal):
            light[i][j - 1 - jj][0] = horizontal
        while j < W and S[i][j] == '#':
            j += 1

# 縦を調べる
for j in range(W):
    i = 0
    while i < H:
        vertical = 0
        while i < H and S[i][j] == '.':
            vertical += 1
            i += 1
        for ii in range(vertical):
            light[i - 1 - ii][j][1] = vertical
            ans = max( ans, sum(light[i - 1 - ii][j]) - 1)
        while i < H and S[i][j] == '#':
            i += 1

print(ans)

Submission status: Time Limit Exceeded

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

for i in range(h):
    s = input()
    count = 0
    for j in range(w):
        if j != w-1:
            if s[j] == '.':
                count += 1
            else:
                li[i][j] = 0
                for k in range(count):
                    li[i][j-k-1] = count
                count = 0
        else:
            if s[j] == '.':
                count += 1
                for k in range(count):
                    li[i][j-k] = count
            else:
                li[i][j] = 0
                for k in range(count):
                    li[i][j-k-1] = count

for i in range(w):
    count = 0
    for j in range(h):
        if j != h-1:
            if li[j][i] > 0:
                count += 1
            else:
                for k in range(count):
                    li[j-k-1][i] += count
                count = 0
        else:
            if li[j][i] > 0:
                count += 1
                for k in range(count):
                    li[j-k][i] += count
            else:
                for k in range(count):
                    li[j-k-1][i] += count

ans = 0

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

print(ans-1)

"""


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] == '#':
            l[i][j] = 0
        else:
            if 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] == '#':
            r[i][j] = 0
        else:
            if 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] == '#':
            u[i][j] = 0
        else:
            if 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] == '#':
            d[i][j] = 0
        else:
            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(ans, l[i][j] + r[i][j] + u[i][j] + d[i][j] - 3)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import re

H,W = map(int,input().split())
arr = []

for i in range(H):
  S = list(map(int,input().replace("#","0").replace(".","1")))
  arr.append(S)

L = [[] for _ in range(H)]
for i in range(H):
  for j in range(W):
    if j == 0:
      ans = arr[i][j]
    elif arr[i][j-1] == 0:
      ans = arr[i][j]
    else:
      ans = ans + 1
    L[i].append(ans)

R = [[] for _ in range(H)]
for i in range(H):
  for j in range(W):
    if j == 0:
      ans = arr[i][W-1-j]
    elif arr[i][W-j] == 0:
      ans = arr[i][W-1-j]
    else:
      ans = ans + 1
    R[i].insert(0, ans)

T = [[] for _ in range(H)]
for j in range(W):
  for i in range(H):
    if i == 0:
      ans = arr[i][j]
    elif arr[i-1][j] == 0:
      ans = arr[i][j]
    else:
      ans = ans + 1
    T[i].append(ans)

U = [[] for _ in range(H)]
for j in range(W):
  for i in range(H):
    if i == 0:
      ans = arr[H-1-i][j]
    elif arr[H-i][j] == 0:
      ans = arr[H-1-i][j]
    else:
      ans = ans + 1
    U[H-1-i].append(ans)

ans = []
for j in range(W):
  for i in range(H):
    if arr[i][j] != 0:
      a = L[i][j] + R[i][j] + T[i][j] + U[i][j] - 3
      ans.append(a)
      
print(int(max(ans)))

Submission status: Time Limit Exceeded

Solution code:
H, W = map(int, input().split())
S = [list(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)]
ans = 0
for i in range(H):
    for j in range(W):
        if S[i][j] != '#':
            if j != 0:
                r[i][j] = r[i][j - 1] + 1
            else:
                r[i][j] = 1

for i in range(H):
    for j in reversed(range(W)):
        if S[i][j] != '#':
            if j != W - 1:
                l[i][j] = l[i][j + 1] + 1
            else:
                l[i][j] = 1

for i in range(W):
    for j in range(H):
        if S[j][i] != '#':
            if j != 0:
                d[j][i] = d[j - 1][i] + 1
            else:
                d[j][i] = 1

for i in range(W):
    for j in reversed(range(H)):
        if S[j][i] != '#':
            if j != H - 1:
                u[j][i] = u[j + 1][i] + 1
            else:
                u[j][i] = 1

for i in range(H):
    for j in range(W):
        tmp = l[i][j] + r[i][j] + u[i][j] + d[i][j] - 3
        ans = max(ans, tmp)

print(ans)

Submission status: Time Limit Exceeded

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


h, w = map(int, input().split())

GRID = []
for _ in range(h):
    s = input().strip()
    R = [0 if c == "#" else 1 for c in s]
    GRID.append(R)

LEFT = [R.copy() for R in GRID]
for i in range(h):
    for j in range(1, w):
        if LEFT[i][j] == 0:
            continue
        LEFT[i][j] += LEFT[i][j - 1]

RIGHT = [R.copy() for R in GRID]
for i in range(h):
    for j in range(w - 2, -1, -1):
        if RIGHT[i][j] == 0:
            continue
        RIGHT[i][j] += RIGHT[i][j + 1]

TOP = [R.copy() for R in GRID]
for j in range(w):
    for i in range(1, h):
        if TOP[i][j] == 0:
            continue
        TOP[i][j] += TOP[i - 1][j]

BOTTOM = [R.copy() for R in GRID]
for j in range(w):
    for i in range(h - 2, -1, -1):
        if BOTTOM[i][j] == 0:
            continue
        BOTTOM[i][j] += BOTTOM[i + 1][j]

LIGHTED = [[0] * w for _ in range(h)]
for i in range(h):
    for j in range(w):
        LIGHTED[i][j] = LEFT[i][j] + RIGHT[i][j] + TOP[i][j] + BOTTOM[i][j] - 3

answer = 0
for i in range(h):
    for j in range(w):
        if LIGHTED[i][j] > answer:
            answer = LIGHTED[i][j]

print(answer)

Submission status: Time Limit Exceeded

Solution code:
import sys

(H, W) = [int(x) for x in sys.stdin.readline().rstrip().split(" ")]
S = [sys.stdin.readline().rstrip() for i in range(H)]

def grid(h, w):
	return [[0 for j in range(w)] for i in range(h)]

l = grid(H, W)
r = grid(H, W)
u = grid(H, W)
d = grid(H, W)

for i in range(0, H):
	if S[i][0] == '.': l[i][0] = 1
	for j in range(1, W):
		l[i][j] = l[i][j - 1] + 1
		if S[i][j] == '#': l[i][j] = 0

	if S[i][W - 1] == '.': r[i][W - 1] = 1
	for j in range(W - 2, -1, -1):
		r[i][j] = r[i][j + 1] + 1
		if S[i][j] == '#': r[i][j] = 0

for j in range(0, W):
	if S[0][j] == '.': u[0][j] = 1
	for i in range(1, H):
		u[i][j] = u[i - 1][j] + 1
		if S[i][j] == '#': u[i][j] = 0

	if S[H - 1][j] == '.': d[H - 1][j] = 1
	for i in range(H - 2, -1, -1):
		d[i][j] = d[i + 1][j] + 1
		if S[i][j] == '#': d[i][j] = 0

answer = 0
for i in range(H):
	for j in range(W):
		if S[i][j] == '.':
			candidate = l[i][j] + r[i][j] + u[i][j] + d[i][j] - 3
			if answer < candidate: answer = candidate

print(answer)

Submission status: Time Limit Exceeded

Solution code:
import sys
inputs = sys.stdin.readlines()

h, w = map(int, inputs[0].split())
S = [list(map(lambda x: 0 if x=='#' else 1, row)) for row in inputs[1:]]

w_costs = [[0]*w for _ in range(h)]
h_costs = [[0]*w for _ in range(h)]

def row_search(x, y):
    x_, cost = x, 0
    while x+1 < w and S[y][x+1]:
        x += 1
        cost += 1
    while x_ <= x:
        w_costs[y][x] = cost
        x -= 1
def col_search(x, y):
    y_, cost = y, 0
    while y+1 < h and S[y+1][x]:
        y += 1
        cost += 1
    while y_ <= y:
        h_costs[y][x] = cost
        y -= 1

for y in range(h):
    for x in range(w):
        if S[y][x]:
            if w_costs[y][x] == 0:
                row_search(x, y)
            if h_costs[y][x] == 0:
                col_search(x, y)

ans = 0
for y in range(h):
    for x in range(w):
        ans = max(ans, w_costs[y][x] + h_costs[y][x] + 1)
print(ans)

Submission status: Time Limit Exceeded

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

S = [input() for i in range(H)]

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

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

        ri = H-i-1
        rj = W-j-1
        if S[ri][rj] == ".":
            if i != 0:
                D[ri][rj] = D[ri+1][rj]+1
            else:
                D[ri][rj] = 1
            if j != 0:
                R[ri][rj] = R[ri][rj+1]+1
            else:
                R[ri][rj] = 1
        else:
            D[ri][rj] = 0
            R[ri][rj] = 0


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 = list(map(int,input().split()))

S = [input() for i in range(H)]

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

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

        if S[H-i-1][W-j-1] == ".":
            if i != 0:
                D[H-i-1][W-j-1] = D[H-i][W-j-1]+1
            else:
                D[H-i-1][W-j-1] = 1
            if j != 0:
                R[H-i-1][W-j-1] = R[H-i-1][W-j]+1
            else:
                R[H-i-1][W-j-1] = 1 
        else:
            D[H-i-1][W-j-1] = 0
            R[H-i-1][W-j-1] = 0        


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 = list(map(int,input().split()))

S = [input() for i in range(H)]

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

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

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)