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 = list(map(int, input().split()))
s = ''
for _ in range(h):
  s += input()

l = [0 for i in range(h * w)]
r = [0 for i in range(h * w)]
u = [0 for i in range(h * w)]
d = [0 for i in range(h * w)]

for i in range(h):
  for j in range(w):
    idx_lt = i * w + j
    idx_rd = (h-1-i) * w + (w-1-j)
    
    # left
    if s[idx_lt] == '#':
      l[idx_lt] = 0
    elif j == 0:
      l[idx_lt] = 1
    else:
      l[idx_lt] = l[idx_lt - 1] + 1
      
    # up
    if s[idx_lt] == '#':
      u[idx_lt] = 0
    elif i == 0:
      u[idx_lt] = 1
    else:
      u[idx_lt] = u[idx_lt - w] + 1
      
    # right
    if s[idx_rd] == '#':
      r[idx_rd] = 0
    elif j == 0:
      r[idx_rd] = 1
    else:
      r[idx_rd] = r[idx_rd + 1] + 1

    # down
    if s[idx_rd] == '#':
      d[idx_rd] = 0
    elif i == 0:
      d[idx_rd] = 1
    else:
      d[idx_rd] = d[idx_rd + w] + 1

res = 0
for i in range(h * w):
  res = max(res, l[i] + r[i] + u[i] + d[i] - 3)
print(res)

Submission status: Time Limit Exceeded

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




def main():
    def ue(i, j):
        if lst_ue[i][j] != -1:
            return lst_ue[i][j]
        if lst[i][j] == '#':
            lst_ue[i][j] = 0
            return 0
        else:
            lst_ue[i][j] = ue(i-1, j) + 1
            return lst_ue[i][j]

    def shita(i, j):
        if lst_shita[i][j] != -1:
            return lst_shita[i][j]
        if lst[i][j] == '#':
            lst_shita[i][j] = 0
            return 0
        else:
            lst_shita[i][j] = shita(i+1, j) + 1
            return lst_shita[i][j]        

    def hidari(i, j):
        if lst_hidari[i][j] != -1:
            return lst_hidari[i][j]
        if lst[i][j] == '#':
            lst_hidari[i][j] = 0
            return 0
        else:
            lst_hidari[i][j] = hidari(i, j-1) + 1
            return lst_hidari[i][j]        

    def migi(i, j):
        if lst_migi[i][j] != -1:
            return lst_migi[i][j]
        if lst[i][j] == '#':
            lst_migi[i][j] = 0
            return 0
        else:
            lst_migi[i][j] = migi(i, j+1) + 1
            return lst_migi[i][j]        







    H, W = map(int, input().split())
    lst = [['#'] * (W+2)]
    lst += [['#'] + list(input()) + ['#'] for i in range(H)]
    lst += [['#'] * (W+2)]


    lst_ue = [[-1] * (W+2) for i in range(H+2)]
    lst_shita = [[-1] * (W+2) for i in range(H+2)]
    lst_migi = [[-1] * (W+2) for i in range(H+2)]
    lst_hidari = [[-1] * (W+2) for i in range(H+2)]
    
    ans = 0
    for i in range(1,H+1):
        for j in range(1, W+1):
            if lst[i][j] != '#':
                ans = max(ans, ue(i,j)+shita(i,j)+hidari(i,j)+migi(i,j)-3)
                if ans == H + W -1:
                    print (ans)
                    sys.exit()
    print (ans)
    return

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
a,b = map(int,input().split(" "))
ar = []
for i in range(a):
    l = list(input())
    ar.append(l)
mx = 0
for i in range(a):
    for j in range(b):
        if ar[i][j] == ".":
            count = 1
            k = i
            l = j
            while True:
                if k == 0:
                    break
                k -= 1
                if ar[k][l] == ".":
                    count += 1
                else:
                    break
            # print(count,end="")
            k = i
            while True:
                if l == 0:
                    break
                l -= 1
                if ar[k][l] == ".":
                    count += 1
                else:
                    break
            # print(count,end="")
            l = j
            while True:
                if k == a-1:
                    break
                k += 1
                if ar[k][l] == ".":
                    count += 1
                else:
                    break
            # print(count,end="")
            k = i
            while True:
                if l == b-1:
                    break
                l += 1
                if ar[k][l] == ".":
                    count += 1
                else:
                    break
            if count > mx:
                mx = count
print(mx)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sat Apr 20 20:47:47 2019

@author: Owner
"""

import numpy as np
import sys
import collections
import scipy.misc
import math
from operator import itemgetter
import itertools
import copy

#素因数を並べる
def prime_decomposition(n):
  i = 2
  table = []
  while i * i <= n:
    while n % i == 0:
      n /= i
      table.append(int(i))
    i += 1
  if n > 1:
    table.append(int(n))
  return table   
# 桁数を吐く
def digit(i):
    if i > 0:
        return digit(i//10) + [i%10]
    else:
        return []
    
def getNearestValueIndex(list, num):
    """
    概要: リストからある値に最も近い値のインデックスを取得する関数
    @param list: データ配列
    @param num: 対象値
    @return 対象値に最も近い値
    """

    # リスト要素と対象値の差分を計算し最小値のインデックスを取得
    idx = np.abs(np.asarray(list) - num).argmin()
    return idx

def find_index(l, x, default=False):
    if x in l:
        return l.index(x)
    else:
        return default

class UnionFind(object):
    def __init__(self, n=1):
        self.par = [i for i in range(n)]
        self.rank = [0 for _ in range(n)]
        self.size = [1 for _ in range(n)]

    def find(self, x):
        """
        x が属するグループを探索
        """
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]

    def union(self, x, y):
        """
        x と y のグループを結合
        """
        x = self.find(x)
        y = self.find(y)
        if x != y:
            if self.rank[x] < self.rank[y]:
                x, y = y, x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1
            self.par[y] = x
            self.size[x] += self.size[y]

    def is_same(self, x, y):
        """
        x と y が同じグループか否か
        """
        return self.find(x) == self.find(y)

    def get_size(self, x):
        """
        x が属するグループの要素数
        """
        x = self.find(x)
        return self.size[x]
    

"""
N, X = map(int, input().split())

x = list(map(int, input().split()))

P = [0]*N
Y = [0]*N
for n in range(N):
    P[n], Y[n] = map(int, input().split())

# 多次元配列の宣言（あとでintにすること。）（タプルにすること。）
dp = np.zeros((N+1, 4,4,4))
    
all(nstr.count(c) for c in '753')

# 複数配列を並び替え
ABT = zip(A, B, totAB)
result = 0
# itemgetterには何番目の配列をキーにしたいか渡します
sorted(ABT,key=itemgetter(2))
A, B, totAB = zip(*ABT)
A.sort(reverse=True)

# 2進数のbit判定
(x >> i) & 1

# dp最小化問題
dp = [np.inf]*N
for n in range(N):
    if n == 0:
        dp[n] = 0
    else:
        for k in range(1,K+1):
            if n-k >= 0:
                dp[n] = min(dp[n], dp[n-k] + abs(h[n]-h[n-k]))
            else:
                break
# 累積和
add = 1 # 問題によって決まる
res = 0
sums = [0]*(len(nums)+1)
for i in range(len(nums)):
    sums[i+1] = sums[i] + nums[i]
for i in range(0, len(nums), 2):
    left = i
    right = min(i+add, len(nums))
    tmp = sums[right] - sums[left]
    res = max(tmp, res)

#２分探索
li, ri = bisect.bisect_left(p_ac, l[i]-1), bisect.bisect_right(p_ac, r[i]-1)    

#ソート関数
org_list = [3, 1, 4, 5, 2]
new_list = sorted(org_list)
print(org_list)
print(new_list)
# [3, 1, 4, 5, 2]
# [1, 2, 3, 4, 5]

#Distance Transformation
    for h in range(0,H):
        for w in range(0,W):
            if h == 0 and w == 0:
                pass
            elif h == 0:
                D[H-h-1][W-w-1]= min(D[H-h-1][W-w-1], D[H-h-1][W-w]+1)
            elif w == 0:   
                D[H-h-1][W-w-1]= min(D[H-h-1][W-w-1], D[H-h][W-w-1]+1)
            else:
                D[H-h-1][W-w-1]= min(D[H-h-1][W-w-1], D[H-h][W-w-1]+1, D[H-h-1][W-w]+1, D[H-h][W-w]+2)
            d_max = max(D[H-h-1][W-w-1], d_max)

"""


H, W = list(map(int, input().split()))
# 多次元配列の宣言（あとでintにすること。）（タプルにすること。）
A = [""]*H 
#INF = np.Inf
INF = 2000

#D = np.zeros((H, W))
D = np.zeros((H+1, W+1))
for h in range(H):
    A[h] = input()
    for w in range(W):
        if A[h][w] == '.':
            D[h][w] = 1           

def main(): 
    G = np.zeros((H+1, W+1))
    T = np.zeros((H+1, W+1)) # 自分以外横に何マスつながっているか
    Y = np.zeros((H+1, W+1)) # 自分以外縦に何マスつながっているか
    
    for h in range(H):
        cnt = 0
        for w in range(W):
            if D[h][w] == 0:
                cnt = 0
                continue
            else:
                Y[h][w] += cnt
                cnt+=1    
    for h in range(H):
        for w in reversed(range(W)):
            if D[h][w] == 0:
               continue
            else:
               Y[h][w] = max(Y[h][w], Y[h][w+1])                           
            
    for w in range(W):
        cnt = 0
        for h in range(H):
            if D[h][w] == 0:
                cnt = 0
                continue
            else:
                T[h][w] += cnt
                cnt+=1
    for w in range(W):
        for h in reversed(range(H)):
            if D[h][w] == 0:
                continue
            else:
                T[h][w] = max(T[h][w], T[h+1][w])
    #G = D+Y+T
    #res = np.amax(G)
    res = 0
    for w in range(W):
        for h in range(H):
            res = max(res, D[h][w]+Y[h][w]+T[h][w])
    print(int(res))
                
            
            
                

    
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
l = input().split()
H = int(l[0])
W = int(l[1])

S = []
for i in range(H):
    l = input().split()
    S.append(l[0])

# print(H, W, S)

# TS = []
# for i in range(W):
#     tmp = ''
#     for j in range(H):
#         tmp += S[j][i]
#     TS.append(tmp)

# print(TS)

HL = []
for i in range(H):
    l = 0
    begin = 0
    tmp = []
    for j in range(W):
        if S[i][j] == '.':
            l += 1
        else:
            tmp.extend([l]*(j - begin))
            tmp.append(0)
            begin = j + 1
            l = 0
    if begin != W:
        tmp.extend([l]*(W - begin))
    HL.append(tmp)

WL = []
for i in range(W):
    l = 0
    begin = 0
    tmp = []
    for j in range(H):
        if S[j][i] == '.':
            l += 1
        else:
            tmp.extend([l]*(j - begin))
            tmp.append(0)
            begin = j + 1
            l = 0
    if begin != H:
        tmp.extend([l]*(H - begin))
    WL.append(tmp)

max_l = 0
for i in range(H):
    for j in range(W):
        l = HL[i][j] + WL[j][i]
        max_l = max(l, max_l)
print(max_l - 1)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
from pprint import pprint


def solve(MM, H, W):
    # L, R, U, D = [], [], [], []
    # for i in range(H):
    #     L.append([0] * W)
    #     R.append([0] * W)
    #     U.append([0] * W)
    #     D.append([0] * W)

    L, R, U, D = np.zeros((H, W), dtype=int), np.zeros((H, W), dtype=int), np.zeros((H, W), dtype=int), np.zeros((H, W), dtype=int)

    for i in range(H):
        i_r = H - 1 - i

        # L, U
        for j in range(W):
            j_r = W - 1 - j

            # L
            if MM[i][j]:
                if j == 0:
                    L[i][j] = 1
                else:
                    L[i][j] = L[i][j - 1] + 1

            # R
            if MM[i][j_r]:
                if j_r == W - 1:
                    R[i][j_r] = 1
                else:
                    R[i][j_r] = R[i][j_r + 1] + 1

            # U
            if MM[i][j]:
                if i == 0:
                    U[i][j] = 1
                else:
                    U[i][j] = U[i - 1][j] + 1

            # D
            if MM[i_r][j]:
                if i_r == H - 1:
                    D[i_r][j] = 1
                else:
                    D[i_r][j] = D[i_r + 1][j] + 1

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


def stdin():
    H, W = [int(x) for x in input().split()]
    MM = []

    for i in range(H):
        line = str(input())
        MM.append([])

        for j in range(W):
            MM[i].append(True)
            if line[j] == '#':
                MM[i][j] = False
    # pprint(MM)

    solve(MM, H, W)


if __name__ == '__main__':
    stdin()

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
from pprint import pprint


def solve(MM, H, W):
    # L, R, U, D = [], [], [], []
    # for i in range(H):
    #     L.append([0] * W)
    #     R.append([0] * W)
    #     U.append([0] * W)
    #     D.append([0] * W)

    L, R, U, D = np.zeros((H, W), dtype=int), np.zeros((H, W), dtype=int), np.zeros((H, W), dtype=int), np.zeros((H, W), dtype=int)
    SUM = np.zeros((H, W), dtype=int)

    ans = 0

    for i in range(H):
        i_r = H - 1 - i

        # L, U
        for j in range(W):
            j_r = W - 1 - j

            # L
            if MM[i][j]:
                if j == 0:
                    L[i][j] = 1
                else:
                    L[i][j] = L[i][j - 1] + 1

            # R
            if MM[i][j_r]:
                if j_r == W - 1:
                    R[i][j_r] = 1
                else:
                    R[i][j_r] = R[i][j_r + 1] + 1

            # U
            if MM[i][j]:
                if i == 0:
                    U[i][j] = 1
                else:
                    U[i][j] = U[i - 1][j] + 1

            # D
            if MM[i_r][j]:
                if i_r == H - 1:
                    D[i_r][j] = 1
                else:
                    D[i_r][j] = D[i_r + 1][j] + 1

            SUM[i][j] += L[i][j] + U[i][j]
            SUM[i][j_r] += R[i][j_r]
            SUM[i_r][j] += D[i_r][j]

    # pprint(L)
    # pprint(R)
    # pprint(U)
    # pprint(D)

    # 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(SUM.max() - 3)


def stdin():
    H, W = [int(x) for x in input().split()]
    MM = []

    for i in range(H):
        line = str(input())
        MM.append([])

        for j in range(W):
            MM[i].append(True)
            if line[j] == '#':
                MM[i][j] = False
    # pprint(MM)

    solve(MM, H, W)


if __name__ == '__main__':
    stdin()

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
from pprint import pprint


def solve(MM, H, W):
    # L, R, U, D = [], [], [], []
    # for i in range(H):
    #     L.append([0] * W)
    #     R.append([0] * W)
    #     U.append([0] * W)
    #     D.append([0] * W)

    L, R, U, D = np.zeros((H, W), dtype=int), np.zeros((H, W), dtype=int), np.zeros((H, W), dtype=int), np.zeros((H, W), dtype=int)

    for i in range(H):
        i_r = H - 1 - i

        # L, U
        for j in range(W):
            j_r = W - 1 - j

            # L
            if MM[i][j]:
                if j == 0:
                    L[i][j] = 1
                else:
                    L[i][j] = L[i][j - 1] + 1

            # R
            if MM[i][j_r]:
                if j_r == W - 1:
                    R[i][j_r] = 1
                else:
                    R[i][j_r] = R[i][j_r + 1] + 1

            # U
            if MM[i][j]:
                if i == 0:
                    U[i][j] = 1
                else:
                    U[i][j] = U[i - 1][j] + 1

            # D
            if MM[i_r][j]:
                if i_r == H - 1:
                    D[i_r][j] = 1
                else:
                    D[i_r][j] = D[i_r + 1][j] + 1

    # pprint(L)
    # pprint(R)
    # pprint(U)
    # pprint(D)

    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)


def stdin():
    H, W = [int(x) for x in input().split()]
    MM = []

    for i in range(H):
        line = str(input())
        MM.append([])

        for j in range(W):
            MM[i].append(True)
            if line[j] == '#':
                MM[i][j] = False
    # pprint(MM)

    solve(MM, H, W)


if __name__ == '__main__':
    stdin()

Submission status: Time Limit Exceeded

Solution code:
from pprint import pprint


def solve(MM, H, W):
    L, R, U, D = [], [], [], []
    for i in range(H):
        L.append([0] * W)
        R.append([0] * W)
        U.append([0] * W)
        D.append([0] * W)

    for i in range(H):
        i_r = H - 1 - i

        # L, U
        for j in range(W):
            j_r = W - 1 - j

            # L
            if MM[i][j]:
                if j == 0:
                    L[i][j] = 1
                else:
                    L[i][j] = L[i][j - 1] + 1

            # R
            if MM[i][j_r]:
                if j_r == W - 1:
                    R[i][j_r] = 1
                else:
                    R[i][j_r] = R[i][j_r + 1] + 1

            # U
            if MM[i][j]:
                if i == 0:
                    U[i][j] = 1
                else:
                    U[i][j] = U[i - 1][j] + 1

            # D
            if MM[i_r][j]:
                if i_r == H - 1:
                    D[i_r][j] = 1
                else:
                    D[i_r][j] = D[i_r + 1][j] + 1

    # pprint(L)
    # pprint(R)
    # pprint(U)
    # pprint(D)

    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)


def stdin():
    H, W = [int(x) for x in input().split()]
    MM = []

    for i in range(H):
        line = str(input())
        MM.append([])

        for j in range(W):
            MM[i].append(True)
            if line[j] == '#':
                MM[i][j] = False
    # pprint(MM)

    solve(MM, H, W)


if __name__ == '__main__':
    stdin()

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
def solve(h, w, s):
    A = np.zeros((h+2, w+2), dtype=np.uint8)
    for i in range(h):
        for j in range(w):
            A[i+1, j+1] = 1 if (s[i][j] == "#") else 0
    A[[0,h+1],:] = 1
    A[:,[0,w+1]] = 1
    B = np.zeros(A.shape, dtype=np.int32)
    for i in range(1, h+1):
        x = np.where(A[i] == 1)[0]
        for j in range(len(x)-1):
            l, r = x[j] + 1, x[j+1]
            B[i,l:r] = r - l
    C = np.zeros(A.shape, dtype=np.int32)
    for j in range(1, w+1):
        y = np.where(A[:,j] == 1)[0]
        for i in range(len(y)-1):
            t, b = y[i] + 1, y[i+1]
            C[t:b,j] = b - t
    return np.max(B+C) - 1

h, w = map(int, input().split())
s = [input() for i in range(h)]
print(solve(h, w, s))

Submission status: Time Limit Exceeded

Solution code:
hw = list(map(int, input().split()))
# "."は障害物なし、"#"は障害物
s = [input() for l in range(hw[0])]
result = 1

for i in range(hw[0]):
    for j in range(hw[1]):
        if s[i][j] == ".":     
            ray = 1
            
            r = 1
            while j+r<hw[1] and s[i][j+r] != "#":
                ray += 1
                r += 1
               
            l = 1
            while j-l>-1 and s[i][j-l] != "#":
                ray += 1
                l += 1
                
            d = 1
            while i+d<hw[0] and s[i+d][j] != "#":
                ray += 1
                d += 1
            
            t = 1
            while i-t>-1 and s[i-t][j] != "#":
                ray += 1
                t += 1
                
            if result < ray:
                result = ray
            
print(result)

Submission status: Time Limit Exceeded

Solution code:
import sys

def calc(a, H, W):
    # 横方向
    for j in range(H):
        prev = 0
        start = 0
        for i in range(W + 1):
            if a[j][i] > 0:
                if prev == 0:
                    start = i
                    prev = 1
            else:
                if prev == 1:
                    num = i - start
                    for k in range(start, i):
                        a[j][k] = num
                    prev = 0
    # 縦方向
    for i in range(W):
        prev = 0
        start = 0
        for j in range(H + 1):
            if a[j][i] > 0:
                if prev == 0:
                    start = j
                    prev = 1
            else:
                if prev == 1:
                    num = j - start
                    for k in range(start, j):
                        a[k][i] += num - 1
                    prev = 0


sin = sys.stdin
H, W = map(int, sin.readline().split())
a = [[0] * (W + 1) for _ in range(H + 1)]

for j in range(H):
    for i, c in enumerate(sin.readline()):
        if c == '.':
            a[j][i] = 1

calc(a, H, W)

ans = 0
for row in a:
    ans = max(ans, max(row))
print(ans)

Submission status: Time Limit Exceeded

Solution code:

    
def main():
    
    h, w = [int(x) for x in input().rstrip(" ").split(" ")]
    
    from collections import defaultdict
    maze = defaultdict(str)
    for r in range(h):
        line = input().rstrip(" ")
        
        for c in range(w):
            maze[r,c] = line[c]
    
    counter_left = defaultdict(int)
    counter_right = defaultdict(int)
    
    for r in range(h):
        
        for c in range(w):
            if maze[r,c] == "#":
                counter_left[r,c] = 0
            else:
                counter_left[r,c] = counter_left[r,c-1] + 1
            reverse_c = (w - 1) - c
            if maze[r,reverse_c] == "#":
                counter_right[r,reverse_c] = 0
            else:
                counter_right[r,reverse_c] = counter_right[r,reverse_c+1] + 1
        
    counter_up = defaultdict(int)
    counter_down = defaultdict(int)
    for c in range(w):
        for r in range(h):
            if maze[r,c] == "#":
                counter_up[r,c] = 0
            else:
                counter_up[r,c] = counter_up[r-1,c] + 1
            revserse_r = (h - 1) - r
            if maze[revserse_r,c] == "#":
                counter_down[revserse_r,c] = 0
            else:
                counter_down[revserse_r,c] = counter_down[revserse_r+1,c] + 1

    best = 0
    for r in range(h):
        for c in range(w):
            score = counter_left[r,c] + counter_right[r,c] + counter_up[r,c] + counter_down[r,c] - 3
            if score > best:
                best = score
    
    print(best)
    
    
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    
    h, w = [int(x) for x in input().rstrip(" ").split(" ")]
    
    from collections import defaultdict
    maze = defaultdict(str)
    for r in range(h):
        line = input().rstrip(" ")
        
        for c in range(w):
            maze[r,c] = line[c]
    
    counter_left = defaultdict(int)
    
    for r in range(h):
        
        for c in range(w):
            if maze[r,c] == "#":
                counter_left[r,c] = 0
            else:
                counter_left[r,c] = counter_left[r,c-1] + 1
    
    counter_right = defaultdict(int)
    for r in range(h):
        
        for i in range(w):
            c = (w - 1) - i
            if maze[r,c] == "#":
                counter_right[r,c] = 0
            else:
                counter_right[r,c] = counter_right[r,c+1] + 1
    
    counter_up = defaultdict(int)
    for c in range(w):
        for r in range(h):
            if maze[r,c] == "#":
                counter_up[r,c] = 0
            else:
                counter_up[r,c] = counter_up[r-1,c] + 1
            
    counter_down = defaultdict(int)
    for c in range(w):
        for i in range(h):
            r = (h - 1) - i
            if maze[r,c] == "#":
                counter_down[r,c] = 0
            else:
                counter_down[r,c] = counter_down[r+1,c] + 1
    

    best = 0
    for r in range(h):
        for c in range(w):
            score = counter_left[r,c] + counter_right[r,c] + counter_up[r,c] + counter_down[r,c] - 3
            if score > best:
                best = score
    
    print(best)
    
    
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
#D

h,w = (int(x) for x in input().split())

s = []
for i in range(h):
    x = input()
    s.append(x)

y = [[0] * w for i in range(h)]
t = [[0] * w for i in range(h)]

#横
for i in range(h):
    mi = -1
    count = 0
    for j in range(w):
        this_s = s[i][j]
        
        if this_s == ".":
            count += 1
            if mi == -1:
                mi = j
        if this_s == "#" and not(mi == -1):
            y[i][mi:j] = [count] * (j-mi)
            mi = -1
            count = 0
    else:
        if not(mi == -1):
            y[i][mi:w] = [count] * (w-mi)
#print(y)
        
#縦
for j in range(w):
    mi = -1
    count = 0
    for i in range(h):
        this_s = s[i][j]
        
        if this_s == ".":
            count += 1
            if mi == -1:
                mi = i
        if this_s == "#" and not(mi == -1):
            for k in range(mi,i):
                t[k][j] = count
            mi = -1
            count = 0
    else:
        if not(mi == -1):
            #t[mi:h][j] = [count] * (h-mi)
            for k in range(mi,h):
                t[k][j] = count
#print(t)

count = 0
for i in range(h):
    for j in range(w):
        this_count = y[i][j] + t[i][j] -1
        count = max(count, this_count)
        
print(count)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

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

light_up_H = np.zeros((H, W))
light_up_W = np.zeros((H, W))

count_W = [0 for _ in range(W)]
dots_W = [[] for _ in range(W)]
count_H = 0
dots_H = []

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

    for j in range(W):
        if S[j] == '#':
            light_up_W[dots_W[j], j] = count_W[j]
            light_up_H[i, dots_H] = count_H
            count_W[j] = 0
            dots_W[j] = []
            count_H = 0
            dots_H = []
        else:
            count_W[j] += 1
            dots_W[j].append(i)
            count_H += 1
            dots_H.append(j)

    light_up_H[i, dots_H] = count_H
    count_H = 0
    dots_H = []

for j in range(W):
    light_up_W[dots_W[j], j] = count_W[j]

print(int(np.max(light_up_H + light_up_W)) - 1)