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:
def solve(string):
    h, w, *s = string.split()
    h, w = map(int, [h, w])
    hg = []
    hi = []
    vg = []
    vi = []

    for _s in s:
        gn = 0
        _hi = [0] * w
        _hg = []
        c = 0
        for i, __s in enumerate(_s):
            if __s == "#":
                _hi[i] = -1
                gn += 1
                _hg.append(c)
                c = 0
                continue
            _hi[i] = gn
            c += 1
        _hg.append(c)
        hg.append(_hg)
        hi.append(_hi)
    for _s in zip(*s):
        gn = 0
        _vi = [0] * h
        _vg = []
        c = 0
        for i, __s in enumerate(_s):
            if __s == "#":
                _vi[i] = -1
                gn += 1
                _vg.append(c)
                c = 0
                continue
            _vi[i] = gn
            c += 1
        _vg.append(c)
        vg.append(_vg)
        vi.append(_vi)

    ans = 0
    for i in range(h):
        for j in range(w):
            ans = max(ans, hg[i][hi[i][j]] + vg[j][vi[j][i]])
    return str(ans - 1)


if __name__ == '__main__':
    n, m = map(int, input().split())
    print(solve('{} {}\n'.format(n, m) + '\n'.join([input() for _ in range(n)])))

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
'''
allinputs = iter(input().splitlines())
input = lambda : next(allinputs)
#'''
H, W = map(int, input().split())
S = [input() for _ in range(H)]
R = [[0] * W for _ in range(H)]
L = [[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] == '.':
			if j > 0:
				L[i][j] = L[i][j - 1] + 1
			elif j == 0:
				L[i][j] = 1
				
			if i > 0:
				U[i][j] = U[i - 1][j] + 1
			elif i == 0:
				U[i][j] = 1
		
		if S[i][W - j - 1] == '.':
			if j > 0:
				R[i][W - j - 1] = R[i][W - j] + 1
			elif j == 0:
				R[i][W - j - 1] = 1
		
		if S[H - i - 1][j] == '.':
			if i > 0:
				D[H - i - 1][j] = D[H - i][j] + 1
			elif i == 0:
				D[H - i - 1][j] = 1
			
ans = 0
for i in range(H):
	for j in range(W):
		tmp = L[i][j] + R[i][j] + U[i][j] + D[i][j] - 3
		if tmp > ans:
			ans = tmp
			
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10000)
'''
allinputs = iter(input().splitlines())
input = lambda : next(allinputs)
#'''
H, W = map(int, input().split())
S = [input() for _ in range(H)]

memo = [[[0] * 4 for _ in range(W)] for _ in range(H)]
dx = [-1, 1, 0, 0]
dy = [0, 0, -1, 1]

def dfs(x, y, i):
	nx = x + dx[i]
	ny = y + dy[i]
	if nx >= 0 and nx < H and ny >= 0 and ny < W and S[nx][ny] == '.':
		memo[x][y][i] = dfs(nx, ny, i) + 1
		return memo[x][y][i]
	else:
		return 0
		
ans = 0

for sx in range(H):
	for sy in range(W):
		if S[sx][sy] == '.':
			tmp = 1

			for j in range(4):
				nx = sx + dx[j]
				ny = sy + dy[j]
				if nx >= 0 and nx < H and ny >= 0 and ny < W and memo[nx][ny][j] > 0:
					memo[sx][sy][j] = memo[nx][ny][j] + 1
					tmp += memo[sx][sy][j]
				else:
					tmp += dfs(sx, sy, j)
				
			if tmp > ans:
				ans = tmp
				
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10000)
'''
allinputs = iter(input().splitlines())
input = lambda : next(allinputs)
#'''
H, W = map(int, input().split())
S = [input() for _ in range(H)]

memo = [[[0] * 4 for _ in range(W)] for _ in range(H)]
dx = [-1, 1, 0, 0]
dy = [0, 0, -1, 1]

def dfs(x, y, i):
	nx = x + dx[i]
	ny = y + dy[i]
	if nx >= 0 and nx < H and ny >= 0 and ny < W and memo[nx][ny][i] > 0:
		memo[x][y][i] = memo[nx][ny][i] + 1
		return memo[x][y][i]
	elif nx >= 0 and nx < H and ny >= 0 and ny < W and S[nx][ny] == '.':
		memo[x][y][i] = dfs(nx, ny, i) + 1
		return memo[x][y][i]
	else:
		return 0
		
ans = 0

for sx in range(H):
	for sy in range(W):
		if S[sx][sy] == '.':
			tmp = 1

			for j in range(4):
				nx = sx + dx[j]
				ny = sy + dy[j]
				if nx >= 0 and nx < H and ny >= 0 and ny < W and memo[nx][ny][j] > 0:
					memo[sx][sy][j] = memo[nx][ny][j] + 1
					tmp += memo[sx][sy][j]
				else:
					tmp += dfs(sx, sy, j)
				
			if tmp > ans:
				ans = tmp
				
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10000)
'''
allinputs = iter(input().splitlines())
input = lambda : next(allinputs)
#'''
H, W = map(int, input().split())
S = [input() for _ in range(H)]

memo = [[[0] * 4 for _ in range(W)] for _ in range(H)]
dx = [-1, 1, 0, 0]
dy = [0, 0, -1, 1]

def dfs(x, y, i):
	nx = x + dx[i]
	ny = y + dy[i]
	if nx >= 0 and nx < H and ny >= 0 and ny < W and memo[nx][ny][i] > 0:
		memo[x][y][i] = memo[nx][ny][i] + 1
		return memo[x][y][i]
	elif nx >= 0 and nx < H and ny >= 0 and ny < W and S[nx][ny] == '.':
		memo[x][y][i] = dfs(nx, ny, i) + 1
		return memo[x][y][i]
	else:
		return 0
		
ans = 0

for sx in range(H):
	for sy in range(W):
		if S[sx][sy] == '.':
			tmp = 1
			for j in range(4):
				tmp += dfs(sx, sy, j)
				
			if tmp > ans:
				ans = tmp
				
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10000)
'''
allinputs = iter(input().splitlines())
input = lambda : next(allinputs)
#'''
H, W = map(int, input().split())
S = [input() for _ in range(H)]

memo = [[[0] * 4 for _ in range(W)] for _ in range(H)]
dx = [-1, 1, 0, 0]
dy = [0, 0, -1, 1]

def dfs(x, y, i):
	nx = x + dx[i]
	ny = y + dy[i]
	if nx >= 0 and nx < H and ny >= 0 and ny < W and memo[nx][ny][i] > 0:
		memo[x][y][i] = memo[nx][ny][i] + 1
		return memo[x][y][i]
	elif nx >= 0 and nx < H and ny >= 0 and ny < W and S[nx][ny] == '.':
		memo[x][y][i] = dfs(nx, ny, i) + 1
		return memo[x][y][i]
	else:
		return 0
		
ans = 0

for sx in range(H):
	for sy in range(W):
		if S[sx][sy] == '.':
			tmp = 1
			for j in range(4):
				tmp += dfs(sx, sy, j)
				
			if tmp > ans:
				ans = tmp
				
print(ans)

Submission status: Time Limit Exceeded

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

def calc(s):
    res = []
    for v in s:
        v.append('-')
        tmp = []
        nc = v[0]
        nn = 0
        for c in v:
            if nc == c:
                nn += 1
                continue
            else:
                if nc == '#':
                    tmp.extend([0 for _ in range(nn)])
                else:
                    tmp.extend( [nn for _ in range(nn)])
                nc = c
                nn = 1
        res.append(tmp)
    return res

def calc2(s):
    res = []
    for j in range(w):
        tmp = []
        nc = s[0][j]
        nn = 0
        for i in range(h):
            c = s[i][j]
            if nc == c:
                nn += 1
                continue
            else:
                if nc == '#':
                    tmp.extend([0 for _ in range(nn)])
                else:
                    tmp.extend( [nn for _ in range(nn)])
                nc = c
                nn = 1
        if nc == '#':
            tmp.extend([0 for _ in range(nn)])
        else:
            tmp.extend( [nn for _ in range(nn)])
        res.append(tmp)
    return res

tate = calc2(S)
yoko = calc(S)

print(max([tate[j][i] + yoko[i][j] for i in range(h) for j in range(w) if tate[j][i] * yoko[i][j] != 0]) - 1)

Submission status: Time Limit Exceeded

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

def calc(s):
    res = []
    for v in s:
        v.append('-')
        tmp = []
        nc = v[0]
        nn = 0
        for c in v:
            if nc == c:
                nn += 1
                continue
            else:
                if nc == '#':
                    tmp.extend([0 for _ in range(nn)])
                else:
                    tmp.extend( [nn for _ in range(nn)])
                nc = c
                nn = 1
        res.append(tmp)
    return res

def calc2(s):
    res = []
    for j in range(w):
        tmp = []
        nc = s[0][j]
        nn = 0
        for i in range(h+1):
            if i == h:
                c = '-'
            else:
                c = s[i][j]
            if nc == c:
                nn += 1
                continue
            else:
                if nc == '#':
                    tmp.extend([0 for _ in range(nn)])
                else:
                    tmp.extend( [nn for _ in range(nn)])
                nc = c
                nn = 1
        res.append(tmp)
    return res

tate = calc2(S)
yoko = calc(S)

print(max([tate[j][i] + yoko[i][j] for i in range(h) for j in range(w) if tate[j][i] * yoko[i][j] != 0]) - 1)

Submission status: Time Limit Exceeded

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

def calc(s):
    res = []
    for v in s:
        v.append('-')
        tmp = []
        nc = ''
        nn = 0
        for c in v:
            if nc == '':
                nc = c
                nn += 1
                continue
            if nc == c:
                nn += 1
                continue
            else:
                if nc == '#':
                    tmp.extend([0 for _ in range(nn)])
                else:
                    tmp.extend( [nn for _ in range(nn)])
                nc = c
                nn = 1
        res.append(tmp)
    return res

def calc2(s):
    res = []
    for j in range(w):
        tmp = []
        nc = ''
        nn = 0
        for i in range(h+1):
            if i == h:
                c = '-'
            else:
                c = s[i][j]
            if nc == '':
                nc = c
                nn += 1
                continue
            if nc == c:
                nn += 1
                continue
            else:
                if nc == '#':
                    tmp.extend([0 for _ in range(nn)])
                else:
                    tmp.extend( [nn for _ in range(nn)])
                nc = c
                nn = 1
        res.append(tmp)
    return res

tate = calc2(S)
yoko = calc(S)

print(max([tate[j][i] + yoko[i][j] for i in range(h) for j in range(w) if tate[j][i] * yoko[i][j] != 0]) - 1)

Submission status: Time Limit Exceeded

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

yoko = []
for v in s:
    v.append('-')
    tmp = []
    nc =v[0]
    nn = 0
    for c in v:
        if nc == c:
            nn += 1
            continue
        else:
            if nc == '#':
                tmp.extend([0 for _ in range(nn)])
            else:
                tmp.extend( [nn for _ in range(nn)])
            nc = c
            nn = 1
    yoko.append(tmp)

tate = []
for j in range(w):
    tmp = []
    nc = s[0][j]
    nn = 0
    for i in range(h):
        c = s[i][j]
        if nc == c:
            nn += 1
            continue
        else:
            if nc == '#':
                tmp.extend([0 for _ in range(nn)])
            else:
                tmp.extend( [nn for _ in range(nn)])
            nc = c
            nn = 1
    if nc == '#':
        tmp.extend([0 for _ in range(nn)])
    else:
        tmp.extend( [nn for _ in range(nn)])
    tate.append(tmp)

print(max([tate[j][i] + yoko[i][j] for i in range(h) for j in range(w) if tate[j][i] * yoko[i][j] != 0]) - 1)

Submission status: Time Limit Exceeded

Solution code:
def getlist():
	return map(int, input().split())

L = []

H, W = getlist()
for i in range(H):
	S = list(input())
	L.append(S)

ans1 = [[0 for i in range(W)] for j in range(H)]
ans2 = [[0 for i in range(W)] for j in range(H)]

for i in range(H):
	val = 0
	l = []
	for j in range(W):
		if L[i][j] == "#":
			for k in l:
				ans1[i][k] = val
			val = 0
			l = []
		else:
			val += 1
			l.append(j)
	for k in l:
		ans1[i][k] = val

for i in range(W):
	val = 0
	l = []
	for j in range(H):
		if L[j][i] == "#":
			for k in l:
				ans2[k][i] = val
			val = 0
			l = []
		else:
			val += 1
			l.append(j)
	for k in l:
		ans2[k][i] = val


ans = -1
for i in range(H):
	for j in range(W):
		x = ans1[i][j] + ans2[i][j] - 1
		if x > ans:
			ans = x

print(ans)

Submission status: Time Limit Exceeded

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

yoko = []
for v in s:
    v.append('-')
    tmp = []
    nc =v[0]
    nn = 0
    for c in v:
        if nc == c:
            nn += 1
            continue
        else:
            if nc == '#':
                tmp = tmp + [0 for _ in range(nn)]
            else:
                tmp = tmp + [nn for _ in range(nn)]
            nc = c
            nn = 1
    yoko.append(tmp)

tate = []
for j in range(w):
    tmp = []
    nc = s[0][j]
    nn = 0
    for i in range(h):
        c = s[i][j]
        if nc == c:
            nn += 1
            continue
        else:
            if nc == '#':
                tmp = tmp + [0 for _ in range(nn)]
            else:
                tmp = tmp + [nn for _ in range(nn)]
            nc = c
            nn = 1
    if nc == '#':
        tmp = tmp + [0 for _ in range(nn)]
    else:
        tmp = tmp + [nn for _ in range(nn)]
    tate.append(tmp)

print(max([tate[j][i] + yoko[i][j] for i in range(h) for j in range(w) if tate[j][i] * yoko[i][j] != 0]) - 1)

Submission status: Time Limit Exceeded

Solution code:
# ABC129 D
H, W = map(int, input().split(' '))

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

grid = []
grid.append(list(('#'*W).strip()))
for i in range(H-2):
    grid.append(list(('#'+input()+'#').strip()))
grid.append(list(('#'*W).strip()))

for i in range(1, H-1):
    for j in range(1, W-1):
        if grid[i][j] == '.':
            L[i][j] = L[i][j-1] + 1

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

for j in range(1, W-1):
    for i in range(1, H-1):
        if grid[i][j] == '.':
            U[i][j] = U[i-1][j] + 1

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

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

ans = 0
for i in range(1,H-1):
    ans = max(ans, max(total[i]))
print (ans)

Submission status: Time Limit Exceeded

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

def calc(s):
    res = []
    for v in s:
        v.append('-')
        tmp = []
        nc = ''
        nn = 0
        for c in v:
            if nc == '':
                nc = c
                nn += 1
                continue
            if nc == c:
                nn += 1
                continue
            else:
                if nc == '#':
                    tmp = tmp + [0 for _ in range(nn)]
                else:
                    tmp = tmp + [nn for _ in range(nn)]
                nc = c
                nn = 1
        res.append(tmp)
    return res

def calc2(s):
    res = []
    for j in range(w):
        v = ['-']
        tmp = []
        nc = ''
        nn = 0
        for i in range(h+1):
            if i == h:
                c = '-'
            else:
                c = s[i][j]
            if nc == '':
                nc = c
                nn += 1
                continue
            if nc == c:
                nn += 1
                continue
            else:
                if nc == '#':
                    tmp = tmp + [0 for _ in range(nn)]
                else:
                    tmp = tmp + [nn for _ in range(nn)]
                nc = c
                nn = 1
        res.append(tmp)
    return res

tate = np.asarray(calc2(S)).T
yoko = np.asarray(calc(S))
print((tate + yoko).max() - 1)

Submission status: Time Limit Exceeded

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

def calc(s):
    res = []
    for v in s:
        v.append('-')
        tmp = []
        nc = ''
        nn = 0
        for c in v:
            if nc == '':
                nc = c
                nn += 1
                continue
            if nc == c:
                nn += 1
                continue
            else:
                if nc == '#':
                    tmp = tmp + [0 for _ in range(nn)]
                else:
                    tmp = tmp + [nn for _ in range(nn)]
                nc = c
                nn = 1
        res.append(tmp)
    return res

def calc2(s):
    res = []
    for j in range(w):
        v = ['-']
        tmp = []
        nc = ''
        nn = 0
        for i in range(h+1):
            if i == h:
                c = '-'
            else:
                c = s[i][j]
            if nc == '':
                nc = c
                nn += 1
                continue
            if nc == c:
                nn += 1
                continue
            else:
                if nc == '#':
                    tmp = tmp + [0 for _ in range(nn)]
                else:
                    tmp = tmp + [nn for _ in range(nn)]
                nc = c
                nn = 1
        res.append(tmp)
    return res

tate = calc2(S)
yoko = calc(S)
print(max([tate[j][i] + yoko[i][j] for i in range(h) for j in range(w) if tate[j][i] * yoko[i][j] != 0]) - 1)

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10000)
'''
allinputs = iter(input().splitlines())
input = lambda : next(allinputs)
#'''
H, W = map(int, input().split())
S = [input() for _ in range(H)]

dx = [-1, 1, 0, 0]
dy = [0, 0, -1, 1]

def dfs(x, y, count, i):
	nx = x + dx[i]
	ny = y + dy[i]
	if nx >= 0 and nx < H and ny >= 0 and ny < W and S[nx][ny] == '.':
		return dfs(nx, ny, count + 1, i)
	else:
		return count
		
ans = 0

for sx in range(H):
	for sy in range(W):
		if S[sx][sy] == '.':
			tmp = 1
			for i in range(4):
				tmp += dfs(sx, sy, 0, i)
				
			if tmp > ans:
				ans = tmp
				
print(ans)

Submission status: Time Limit Exceeded

Solution code:
h, w = map(int, input().split())
S = [list(input()) for _ in range(h)]
def t(s):
  return [[s[i][j] for i in range(len(s))] for j in range(len(s[0]))]
def calc(s):
  res = []
  for v in s:
    v.append('-')
    tmp = []
    nc = ''
    nn = 0
    for c in v:
      if nc == '':
        nc = c
        nn += 1
        continue
      if nc == c:
        nn += 1
        continue
      else:
        if nc == '#':
          tmp = tmp + [0 for _ in range(nn)]
        else:
          tmp = tmp + [nn for _ in range(nn)]
        nc = c
        nn = 1
    res.append(tmp)
  return res

tate = t(calc(t(S)))
yoko = calc(S)
print(max([tate[i][j] + yoko[i][j] for i in range(h) for j in range(w) if tate[i][j] * yoko[i][j] != 0])-1)

Submission status: Time Limit Exceeded

Solution code:
h, w = map(int, input().split())
S = [list(input()) for _ in range(h)]
def t(s):
  return [[s[i][j] for i in range(len(s))] for j in range(len(s[0]))]
def calc(s):
  res = []
  for v in s:
    v.append('-')
    tmp = []
    nc = ''
    nn = 0
    for c in v:
      if nc == '':
        nc = c
        nn += 1
        continue
      if nc == c:
        nn += 1
        continue
      else:
        if nc == '#':
          tmp = tmp + [0 for _ in range(nn)]
        else:
          tmp = tmp + [nn for _ in range(nn)]
        nc = c
        nn = 1
    res.append(tmp)
  return res

tate = calc(t(S))
yoko = calc(S)
print(max([tate[j][i] + yoko[i][j] for i in range(h) for j in range(w) if tate[j][i] * yoko[i][j] != 0])-1)

Submission status: Time Limit Exceeded

Solution code:
h, w = map(int, input().split())
S = [list(input()) for _ in range(h)]
def t(s):
  return [[s[i][j] for i in range(len(s))] for j in range(len(s[0]))]
def calc(s):
  res = []
  for v in s:
    v.append('-')
    tmp = []
    nc = ''
    nn = 0
    for c in v:
      if nc == '':
        nc = c
        nn += 1
        continue
      if nc == c:
        nn += 1
        continue
      else:
        if nc == '#':
          tmp = tmp + [0 for _ in range(nn)]
        else:
          tmp = tmp + [nn for _ in range(nn)]
        nc = c
        nn = 1
    res.append(tmp)
  return res

tate = t(calc(t(S)))
yoko = calc(S)
print(max([tate[i][j] + yoko[i][j] for i in range(h) for j in range(w) if tate[i][j] * yoko[i][j] != 0])-1)

Submission status: Time Limit Exceeded

Solution code:
h, w = map(int, input().split())
S = [list(input()) for _ in range(h)]
def t(s):
  return [[s[i][j] for i in range(len(s))] for j in range(len(s[0]))]
def calc(s):
  res = []
  for v in s:
    v.append('-')
    tmp = []
    nc = ''
    nn = 0
    for c in v:
      if nc == '':
        nc = c
        nn += 1
        continue
      if nc == c:
        nn += 1
        continue
      else:
        if nc == '#':
          tmp = tmp + [0 for _ in range(nn)]
        else:
          tmp = tmp + [nn for _ in range(nn)]
        nc = c
        nn = 1
    res.append(tmp)
  return res

tate = t(calc(t(S)))
yoko = calc(S)
print(max([tate[i][j] + yoko[i][j] - 1 for i in range(h) for j in range(w)]))

Submission status: Time Limit Exceeded

Solution code:
h, w = map(int, input().split())
S = [list(input()) for _ in range(h)]
def t(s):
  return [[s[i][j] for i in range(len(s))] for j in range(len(s[0]))]
def calc(s):
  res = []
  for v in s:
    v.append('-')
    tmp = []
    nc = ''
    nn = 0
    for c in v:
      if nc == '':
        nc = c
        nn += 1
        continue
      if nc == c:
        nn += 1
        continue
      else:
        if nc == '#':
          tmp = tmp + [0 for _ in range(nn)]
        else:
          tmp = tmp + [nn for _ in range(nn)]
        nc = c
        nn = 1
    res.append(tmp)
  return res

tate = t(calc(t(S)))
yoko = calc(S)
print(max([tate[i][j] + yoko[i][j] - 1 for i in range(h) for j in range(w)]))

Submission status: Time Limit Exceeded

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

dp_l = [[0 for j in range(W)] for i in range(H)]
dp_r = [[0 for j in range(W)] for i in range(H)]
dp_u = [[0 for j in range(W)] for i in range(H)]
dp_d = [[0 for j in range(W)] for i in range(H)]

for i in range(H):
    cnt = 0
    for j in range(W):
        if s[i][j] == '#':
            cnt = 0
        else:
            dp_l[i][j] = cnt
            cnt += 1

    cnt = 0
    for j in range(W)[::-1]:
        if s[i][j] == '#':
            cnt = 0
        else:
            dp_r[i][j] = cnt
            cnt += 1

for j in range(W):
    cnt = 0
    for i in range(H):
        if s[i][j] == '#':
            cnt = 0
        else:
            dp_d[i][j] = cnt
            cnt += 1

    cnt = 0
    for i in range(H)[::-1]:
        if s[i][j] == '#':
            cnt = 0
        else:
            dp_u[i][j] = cnt
            cnt += 1


count_max = 0
for i in range(H):
    for j in range(W):
        if s[i][j] == '#':
            continue
        count_max = max(count_max,
            dp_l[i][j]
            + dp_r[i][j]
            + dp_u[i][j]
            + dp_d[i][j]
            + 1)

print(count_max)

Submission status: Time Limit Exceeded

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

for i in range(H):
    Si = list(input())
    for j in range(W):
        if Si[j] == '#':
            S[i][j] = 1

# row
S_row = [[0]*W for _ in range(H)]
for i in range(H):
    c = 0
    for j in range(W):
        if S[i][j] == 1:
            c = 0
        else:
            c += 1
        S_row[i][j] = c
    # reverse
    for j in range(W-2, -1, -1):
        if S_row[i][j+1] > 0 and S_row[i][j] > 0:
            S_row[i][j] = S_row[i][j+1]

# col
S_col = [[0]*W for _ in range(H)]
for j in range(W):
    c = 0
    for i in range(H):
        if S[i][j] == 1:
            c = 0
        else:
            c += 1
        S_col[i][j] = c
    # reverse
    for i in range(H-2, -1, -1):
        if S_col[i+1][j] > 0 and S_col[i][j] > 0:
            S_col[i][j] = S_col[i+1][j]

# res
res = max([max([S_row[i][j] + S_col[i][j] - 1 for j in range(W)]) for i in range(H)])
print(res)

Submission status: Time Limit Exceeded

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

dp_l = [[0 for j in range(W)] for i in range(H)]
dp_r = [[0 for j in range(W)] for i in range(H)]
dp_u = [[0 for i in range(H)] for j in range(W)]
dp_d = [[0 for i in range(H)] for j in range(W)]


def mark_table_for_dp():
    for i in range(H):
        mark_line_for_dp(s[i], dp_l[i], False)
        mark_line_for_dp(s[i], dp_r[i], True)
    for j in range(W):
        mark_line_for_dp(s_vrt[j], dp_u[j], True)
        mark_line_for_dp(s_vrt[j], dp_d[j], False)



def mark_line_for_dp(line, dp, is_reverse):
    cnt = 0
    r = list(range(len(line)))
    if is_reverse:
        r.reverse()
    for i in r:
        if line[i] == '#':
            cnt = 0
            continue
        dp[i] = cnt
        cnt += 1


mark_table_for_dp()


def count_light_cross(i, j):
    return dp_l[i][j] + dp_r[i][j] + dp_u[j][i] + dp_d[j][i] + 1


count_max = 0
for i in range(H):
    for j in range(W):
        if s[i][j] == '#':
            continue
        count = count_light_cross(i, j)
        if count_max < count:
            count_max = count

print(count_max)

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]):
    r = 0
    for j in range(hw[1]):
        if s[i][j] == ".":     
            ray = 1
            
            if r == 0:
                while j+r+1<hw[1] and s[i][j+r+1] != "#":
                    r += 1
                
            ray += r
                
            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
        else:
            r = 0
print(result)

Submission status: Time Limit Exceeded

Solution code:
h,w = map(int,input().split())
s = []
for i in range(h):
    s1 = list(input())
    s.append(s1)
p = []
q = []
r = []
t = []
l = []
for i in range(h):
    count1 = 0
    count2 = 0
    p1 = []
    q1 = []
    for j in range(w):
        if s[i][j] == ".":
            count1 = count1 + 1
            p1.append(count1)
        elif s[i][j] == "#":
            count1 = 0
            p1.append(count1)
        if s[i][w-j-1] == ".":
            count2 = count2 + 1
            q1.append(count2)
        elif s[i][w-j-1] == "#":
            count2 = 0
            q1.append(count2)
    p.append(p1)
    q.append(q1)
for i in range(w):
    count1 = 0
    count2 = 0
    r1 = []
    t1 = []
    for j in range(h):
        if s[j][i] == ".":
            count1 = count1 + 1
            r1.append(count1)
        elif s[j][i] == "#":
            count1 = 0
            r1.append(count1)
        if s[h-j-1][i] == ".":
            count2 = count2 + 1
            t1.append(count2)
        elif s[h-j-1][i] == "#":
            count2 = 0
            t1.append(count2)
    r.append(r1)
    t.append(t1)
for i in range(h):
    for j in range(w):
        l.append(p[i][j]+q[i][w-j-1]+r[j][i]+t[j][h-i-1])
print(max(l)-3)

Submission status: Time Limit Exceeded

Solution code:
H, W = map(int, input().split())
s = [[c for c in input()] for i in range(H)]
s_vrt = [[s[i][j] for i in range(H)] for j in range(W)]
# 0: lr, 1: up
memo = [[[-1, -1] for j in range(W)] for i in range(H)]

def count_light_place(i, j):
    line_lr = s[i]
    line_up = s_vrt[j]
    m = memo[i][j]
    c_lr = m[0]
    if c_lr == -1:
        c_lr = count_light_line(j, line_lr)
        memo[i][j][0] = c_lr

    c_up = m[1]
    if c_up == -1:
        c_up = count_light_line(i, line_up)
        memo[i][j][1] = c_up

    return c_lr + c_up - 1

def count_light_line(k, line):
    count = 0
    for dn in reversed(range(k)):
        if line[dn] == '#':
            break
        count += 1
 
    for up in range(k, len(line)):
        if line[up] == '#':
            break
        count += 1
    return count 


count_max = 0
for i in range(H):
    for j in range(W):
        if s[i][j] == '#':
            continue
        count = count_light_place(i, j)
        if count_max < count:
            count_max = count

print(count_max)

Submission status: Time Limit Exceeded

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

def count_light_place(i, j):
    line_lr = s[i]
    line_up = s_vrt[j]
    return count_light_line(i, line_up) + count_light_line(j, line_lr) - 1


def count_light_line(k, line):
    count = 0
    for dn in reversed(range(k)):
        if line[dn] == '#':
            break
        count += 1
 
    for up in range(k, len(line)):
        if line[up] == '#':
            break
        count += 1
    return count 


count_max = 0
for i in range(H):
    for j in range(W):
        if s[i][j] == '#':
            continue
        count = count_light_place(i, j)
        if count_max < count:
            count_max = count

print(count_max)