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 main():
    h,w=map(int,input().split())
    s=[list(input()) for _ in range(h)]
    xyw=[[0]*w for _ in range(h)]
    xyh=[[0]*w for _ in range(h)]
    chk=[]
    for i in range(h):
        cnt=0
        for j in range(w):
            if s[i][j]=='#':
                while chk:
                    a=chk.pop()
                    xyw[i][a]=cnt
                cnt=0
                continue
            elif s[i][j]=='.' and j==w-1:
                cnt+=1
                chk.append(j)
                while chk:
                    a=chk.pop()
                    xyw[i][a]=cnt
                continue
            else:
                cnt+=1
                chk.append(j)
    for i in range(w):
        cnt=0
        for j in range(h):
            if s[j][i]=='#':
                while chk:
                    a=chk.pop()
                    xyh[a][i]=cnt
                cnt=0
                continue
            elif s[j][i]=='.' and j==h-1:
                cnt+=1
                chk.append(j)
                while chk:
                    a=chk.pop()
                    xyh[a][i]=cnt
                continue
            else:
                cnt+=1
                chk.append(j)
    ans=0
    for i in range(h):
        for j in range(w):
            ans=max(ans,xyw[i][j]+xyh[i][j]-1)
    print(ans)
    
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
h, w = map(int, input().split())
grid = [['#' for i in range(w+2)]]
for i in range(h):
  grid.append(['#'] + [s for s in str(input())] + ['#'])
grid.append(['#' for i in range(w+2)])

U = [[0 for j in range(w+2)] for i in range(h+2)]
D = [[0 for j in range(w+2)] for i in range(h+2)]
L = [[0 for j in range(w+2)] for i in range(h+2)]
R = [[0 for j in range(w+2)] for i in range(h+2)]

for i in range(1, h+1):
  for j in range(1, w+1):
    if grid[i][j] == '#': continue
    U[i][j] = U[i-1][j] + 1
    L[i][j] = L[i][j-1] + 1
for i in range(h, 0, -1):
  for j in range(w, 0, -1):
    if grid[i][j] == '#': continue
    D[i][j] = D[i+1][j] + 1
    R[i][j] = R[i][j+1] + 1

ans = 0
for i in range(1, h+1):
  for j in range(1, w+1):
    if grid[i][j] == '#': continue
    cnt = U[i][j] + D[i][j] + L[i][j] + R[i][j] - 3
    ans = max(ans, cnt)
    #print(i, j, cnt)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
h, w = map(int, input().split())
grid = [['#' for i in range(w+2)]]
for i in range(h):
  grid.append(['#'] + [s for s in str(input())] + ['#'])
grid.append(['#' for i in range(w+2)])
#print(grid)
ans = 0
for i in range(1, h+1):
  for j in range(1, w+1):
    if grid[i][j] == '#': continue
    cnt = 1
    u, d = i-1, i+1
    l, r = j-1, j+1
    while True:
      if grid[u][j] == '#': break
      cnt += 1
      u -= 1
    while True:
      if grid[d][j] == '#': break
      cnt += 1
      d += 1
    while True:
      if grid[i][l] == '#': break
      cnt += 1
      l -= 1
    while True:
      if grid[i][r] == '#': break
      cnt += 1
      r += 1
    ans = max(ans, cnt)
    #print(i, j, cnt)
print(ans)

Submission status: Time Limit Exceeded

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

S = [list(input()) for _ in range(H)]
M = [[0 for _ in range(W)] for _ in range(H)]

for i in range(H):
    cnt = 0
    for j in range(W):
        if S[i][j] == '#':
            if cnt == 0:continue
            if j == 0:continue
            for _j in range(1,cnt+1):
                M[i][j-_j] = cnt
            cnt = 0
        elif j == W-1:
            for _j in range(cnt+1):
                M[i][j-_j] = cnt+1
            cnt = 0
        else:
            cnt += 1

cnt = 0
ans = 0
for j in range(W):
    for i in range(H):
        if S[i][j] == '#':
            if cnt == 0:continue
            if i == 0:continue
            for _i in range(1,cnt+1):
                ans = max([ans, M[i-_i][j] + cnt - 1 if M[i-_i][j] > 0 else cnt])
            cnt = 0
        elif i == H-1:
            for _i in range(cnt+1):
                ans = max([ans, M[i-_i][j] + cnt if M[i-_i][j] > 0 else cnt + 1])
            cnt = 0
        else:
            cnt += 1

print(ans)

Submission status: Time Limit Exceeded

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

S = [list(input()) for _ in range(H)]
M = [[0 for _ in range(W)] for _ in range(H)]

for i in range(H):
    cnt = 0
    for j in range(W):
        if S[i][j] == '#':
            if cnt == 0:continue
            if j == 0:continue
            for _j in range(1,cnt+1):
                M[i][j-_j] = cnt
            cnt = 0
        elif j == W-1:
            for _j in range(cnt+1):
                M[i][j-_j] = cnt+1
            cnt = 0
        else:
            cnt += 1

cnt = 0
for j in range(W):
    for i in range(H):
        if S[i][j] == '#':
            if cnt == 0:continue
            if i == 0:continue
            for _i in range(1,cnt+1):
                M[i-_i][j] = M[i-_i][j] + cnt - 1 if M[i-_i][j] > 0 else cnt
            cnt = 0
        elif i == H-1:
            for _i in range(cnt+1):
                M[i-_i][j] = M[i-_i][j] + cnt if M[i-_i][j] > 0 else cnt + 1
            cnt = 0
        else:
            cnt += 1

ans = 0
for i in range(H):
    ans = max([ans, max(M[i])])

print(ans)

Submission status: Time Limit Exceeded

Solution code:
h, w = map(int, input().split())
s = []
a = [[0 for i in range(w)] for j in range(h)]
for i in range(h):
	ss = input()
	s.append(ss)
	b = 0
	for j,k in enumerate(ss):
		if k == "#":
			b = 0
			a[i][j] == 0
		else:
			b += 1
			a[i][j] = b
	mx = 0
	for j in range(w-1,-1,-1):
		if a[i][j] == 0:
			mx = 0
			continue
		else:
			mx = max(a[i][j],mx)
			a[i][j] = mx

c = [[0 for i in range(h)] for j in range(w)]
for i in range(w):
	v = ""
	b = 0
	for j in range(h):
		v += s[j][i]
	for j,k in enumerate(v):
		if k == "#":
			b = 0
			c[i][j] == 0
		else:
			b += 1
			c[i][j] = b
	mx = 0
	for j in range(h-1,-1,-1):
		if c[i][j] == 0:
			mx = 0
			continue
		else:
			mx = max(c[i][j],mx)
			c[i][j] = mx

wk = 0
ans = 0
for i in range(h):
	for j in range(w):
		wk = a[i][j] + c[j][i] -1
		ans = max(ans,wk)
print (ans)

Submission status: Time Limit Exceeded

Solution code:
h, w = map(int, input().split())
s = []
for i in range(h):
	ss = input()
	s.append(ss)
t = []
for i in range(w):
	v = ""
	for j in range(h):
		v += s[j][i]
	t.append(v)
ans = 0
for i in range(h):
	for j in range(w):
		if s[i][j] == "#":
			continue
		wk = 0
		s1 = s[i][j:]
		xx = s1.find("#")
		if xx == -1:
			xx = w-j
		wk += xx
		s2 = s[i][:j]
		xx2 = s2.rfind("#")
		if xx2 == -1:
			xx2 = j+1
		else:
			xx2 = j-xx2
		wk += xx2
		s3 = t[j][i:]
		xx3 = s3.find("#")
		if xx3 == -1:
			xx3 = h-i
		#print(s3,xx3)
		wk += xx3
		s4 = t[j][:i]
		xx4 = s4.rfind("#")
		if xx4 == -1:
			xx4 = i+1
		else:
			xx4 = i-xx4
		wk += xx4
		wk -= 3
		#print (wk)
		#print (xx3+xx4-1)
		ans = max(ans,wk)
print (ans)

Submission status: Time Limit Exceeded

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

up = [[0 for w in range(W)] for h in range(H)]
down = [[0 for w in range(W)] for h in range(H)]
left = [[0 for w in range(W)] for h in range(H)]
right = [[0 for w in range(W)] for h in range(H)]

# up and left
for h in range(H):
    for w in range(W):
        if S[h][w] == "#":
            up[h][w] = 0
            left[h][w] = 0
        else:
            # up
            if h == 0:
                up[h][w] = 1
            else:
                up[h][w] = up[h - 1][w] + 1
            # left
            if w == 0:
                left[h][w] = 1
            else:
                left[h][w] = left[h][w - 1] + 1

# down and right
for h in reversed(range(H)):
    for w in reversed(range(W)):
        if S[h][w] == "#":
            down[h][w] = 0
            right[h][w] = 0
        else:
            # down
            if h == H - 1:
                down[h][w] = 1
            else:
                down[h][w] = down[h + 1][w] + 1
            # right
            if w == W - 1:
                right[h][w] = 1
            else:
                right[h][w] = right[h][w + 1] + 1


ans = 0
for h in range(H):
    for w in range(W):
        masu = -3
        masu += up[h][w]
        masu += down[h][w]
        masu += left[h][w]
        masu += right[h][w]
        if masu > ans:
            ans = masu

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys

def main():
    input = sys.stdin.readline

    def tate(x,y):
        down = 0
        while True:
            if s[y+down+1][x] == '.':
                down += 1
            else:
                break

        return down

    def yoko(x,y):
        right = 0
        while True:
            if s[y][x+right+1] == '.':
                right += 1
            else:
                break

        return right

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

    s = []
    s.append(['#' for _ in range(w+2)])
    for _ in range(h):
        s.append(list('#'+input()+'#'))
    s.append(['#' for _ in range(w+2)])

    for i in range(h+2):
        s[i] = tuple(s[i])
    s = tuple(s)

    dp = [[[0,0] for _ in range(w+2)]for _ in range(h+2)]

    ans = 0
    for i in range(1,h+1):
        for j in range(1,w+1):
            if s[i][j] == '.':
                if s[i-1][j] == '.':
                    t = dp[i-1][j][0]
                else:
                    t = tate(j,i)
                if s[i][j-1] == '.':
                    y = dp[i][j-1][1]
                else:
                    y = yoko(j,i)

                ans = max(t+y,ans)
                dp[i][j] = [t,y]

    print(ans+1)
if __name__ == '__main__':
  main()

Submission status: Time Limit Exceeded

Solution code:
import sys

input = sys.stdin.readline

def tate(x,y):
    down = 0
    while True:
        if s[y+down+1][x] == '.':
            down += 1
        else:
            break

    return down

def yoko(x,y):
    right = 0
    while True:
        if s[y][x+right+1] == '.':
            right += 1
        else:
            break

    return right

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

s = []
s.append(['#' for _ in range(w+2)])
for _ in range(h):
    s.append(list('#'+input()+'#'))
s.append(['#' for _ in range(w+2)])

for i in range(h+2):
    s[i] = tuple(s[i])
s = tuple(s)

dp = [[[0,0] for _ in range(w+2)]for _ in range(h+2)]

ans = 0
for i in range(1,h+1):
    for j in range(1,w+1):
        if s[i][j] == '.':
            if s[i-1][j] == '.':
                t = dp[i-1][j][0]
            else:
                t = tate(j,i)
            if s[i][j-1] == '.':
                y = dp[i][j-1][1]
            else:
                y = yoko(j,i)

            ans = max(t+y,ans)
            dp[i][j] = [t,y]

print(ans+1)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
from copy import deepcopy
import sys

input = sys.stdin.readline

def tate(x,y):
    down = 0
    while True:
        if s[y+down+1][x] == '.':
            down += 1
        else:
            break

    return down

def yoko(x,y):
    right = 0
    while True:
        if s[y][x+right+1] == '.':
            right += 1
        else:
            break

    return right

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

s = []
s.append(['#' for _ in range(w+2)])
for _ in range(h):
    s.append(list('#'+input()+'#'))
s.append(['#' for _ in range(w+2)])

for i in range(h+2):
    s[i] = tuple(s[i])
s = tuple(s)

q = deque()
dp = [[[0,0] for _ in range(w+2)]for _ in range(h+2)]

ans = 0
for i in range(1,h+1):
    for j in range(1,w+1):
        if s[i][j] == '.':
            if s[i-1][j] == '.' and s[i][j-1] == '.':
                ans = max(ans,dp[i-1][j][0] + dp[i][j-1][1])
                dp[i][j] = [dp[i-1][j][0],dp[i][j-1][1]]
            elif s[i-1][j] == '.' and s[i][j-1] == '#':
                response = yoko(j,i)
                ans = max(ans,dp[i-1][j][0] + response)
                dp[i][j] = [dp[i-1][j][0],response]
            elif s[i-1][j] == '#' and s[i][j-1] == '.':
                response = tate(j,i)
                ans = max(ans,dp[i][j-1][1] + response)
                dp[i][j] = [response,dp[i][j-1][1]]
            else:
                t = tate(j,i)
                y = yoko(j,i)
                ans = max(t+y,ans)
                dp[i][j] = [t,y]

print(ans+1)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
from copy import deepcopy
import sys

input = sys.stdin.readline

def tate(x,y):
    down = 0
    while True:
        if s[y+down+1][x] == '.':
            down += 1
        else:
            break

    return down

def yoko(x,y):
    right = 0
    while True:
        if s[y][x+right+1] == '.':
            right += 1
        else:
            break

    return right

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

s = []
s.append(['#' for _ in range(w+2)])
for _ in range(h):
    s.append(list('#'+input()+'#'))
s.append(['#' for _ in range(w+2)])

q = deque()
dp = [[[0,0] for _ in range(w+2)]for _ in range(h+2)]

ans = 0
for i in range(1,h+1):
    for j in range(1,w+1):
        if s[i][j] == '.':
            if s[i-1][j] == '.' and s[i][j-1] == '.':
                ans = max(ans,dp[i-1][j][0] + dp[i][j-1][1])
                dp[i][j] = [dp[i-1][j][0],dp[i][j-1][1]]
            elif s[i-1][j] == '.' and s[i][j-1] == '#':
                response = yoko(j,i)
                ans = max(ans,dp[i-1][j][0] + response)
                dp[i][j] = [dp[i-1][j][0],response]
            elif s[i-1][j] == '#' and s[i][j-1] == '.':
                response = tate(j,i)
                ans = max(ans,dp[i][j-1][1] + response)
                dp[i][j] = [response,dp[i][j-1][1]]
            else:
                t = tate(j,i)
                y = yoko(j,i)
                ans = max(t+y,ans)
                dp[i][j] = [t,y]

print(ans+1)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
from copy import deepcopy
import sys

input = sys.stdin.readline

def tate(x,y):
    down = 0
    while True:
        if s[y+down+1][x] == '.':
            down += 1
        else:
            break

    return down

def yoko(x,y):
    right = 0
    while True:
        if s[y][x+right+1] == '.':
            right += 1
        else:
            break

    return right

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

s = []
s.append(['#' for _ in range(w+2)])
for _ in range(h):
    s.append(list('#'+input()+'#'))
s.append(['#' for _ in range(w+2)])

q = deque()
dp = [[[0,0] for _ in range(w+2)]for _ in range(h+2)]

ans = 0
tmp = deepcopy(s)
for i in range(1,h+1):
    for j in range(1,w+1):
        if s[i][j] == '.':
            if s[i-1][j] == '.' and s[i][j-1] == '.':
                ans = max(ans,dp[i-1][j][0] + dp[i][j-1][1])
                dp[i][j] = [dp[i-1][j][0],dp[i][j-1][1]]
            elif s[i-1][j] == '.' and s[i][j-1] == '#':
                response = yoko(j,i)
                ans = max(ans,dp[i-1][j][0] + response)
                dp[i][j] = [dp[i-1][j][0],response]
            elif s[i-1][j] == '#' and s[i][j-1] == '.':
                response = tate(j,i)
                ans = max(ans,dp[i][j-1][1] + response)
                dp[i][j] = [response,dp[i][j-1][1]]
            else:
                t = tate(j,i)
                y = yoko(j,i)
                ans = max(t+y,ans)
                dp[i][j] = [t,y]
            s = deepcopy(tmp)

print(ans+1)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
from copy import deepcopy

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

s = []
s.append(['#' for _ in range(w+2)])
for _ in range(h):
    s.append(list('#'+input()+'#'))
s.append(['#' for _ in range(w+2)])

q = deque()
dp = [[[0,0] for _ in range(w+2)]for _ in range(h+2)]

def yoko(x,y):
    global q
    global s
    s[y][x] = '#'
    res = 0
    q.append([x,y])

    while q:
        x,y = q.popleft()
        if s[y][x-1] == '.':
            q.append([x-1,y])
            res += 1
            s[y][x-1] = '#'
        if s[y][x+1] == '.':
            q.append([x+1,y])
            res += 1
            s[y][x+1] = '#'

    return res

def tate(x,y):
    global q
    global s
    s[y][x] = '#'
    res = 0
    q.append([x,y])

    while q:
        x,y = q.popleft()
        if s[y-1][x] == '.':
            q.append([x,y-1])
            res += 1
            s[y-1][x] = '#'
        if s[y+1][x] == '.':
            q.append([x,y+1])
            res += 1
            s[y+1][x] = '#'

    return res

ans = 0
tmp = deepcopy(s)
for i in range(1,h+1):
    for j in range(1,w+1):
        if s[i][j] == '.':
            if s[i-1][j] == '.' and s[i][j-1] == '.':
                ans = max(ans,dp[i-1][j][0] + dp[i][j-1][1])
                dp[i][j] = [dp[i-1][j][0],dp[i][j-1][1]]
            elif s[i-1][j] == '.' and s[i][j-1] == '#':
                response = yoko(j,i)
                ans = max(ans,dp[i-1][j][0] + response)
                dp[i][j] = [dp[i-1][j][0],response]
            elif s[i-1][j] == '#' and s[i][j-1] == '.':
                response = tate(j,i)
                ans = max(ans,dp[i][j-1][1] + response)
                dp[i][j] = [response,dp[i][j-1][1]]
            else:
                t = tate(j,i)
                y = yoko(j,i)
                ans = max(t+y,ans)
                dp[i][j] = [t,y]
            s = deepcopy(tmp)

print(ans+1)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
from copy import deepcopy

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

s = []
s.append(['#' for _ in range(w+2)])
for _ in range(h):
    s.append(list('#'+input()+'#'))
s.append(['#' for _ in range(w+2)])

q = deque()

def bfs(x,y):
    tmp_x = x
    tmp_y = y
    global q
    global s
    s[y][x] = '#'
    res = 0
    q.append([x,y])

    while q:
        x,y = q.popleft()
        if s[y][x-1] == '.':
            q.append([x-1,y])
            res += 1
            s[y][x-1] = '#'
        if s[y][x+1] == '.':
            q.append([x+1,y])
            res += 1
            s[y][x+1] = '#'
    q.append([tmp_x,tmp_y])
    while q:
        x,y = q.popleft()
        if s[y-1][x] == '.':
            q.append([x,y-1])
            res += 1
            s[y-1][x] = '#'
        if s[y+1][x] == '.':
            q.append([x,y+1])
            res += 1
            s[y+1][x] = '#'
    return res

ans = 0
tmp = deepcopy(s)
for i in range(1,h+1):
    for j in range(1,w+1):
        if s[i][j] == '.':
            ans = max(bfs(j,i),ans)
            s = deepcopy(tmp)

print(ans+1)

Submission status: Time Limit Exceeded

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

max_light = 0
for i in range(h):
    for j in range(w):
        n_light = 1
        if field[i][j] == '#':
            continue
        
        c = 1
        while True:
            if (j+c > w-1) or (j+c < 0) or (field[i][j+c] == '#'):
                break
            
            else:
                n_light += 1
                c += 1
        
        c = 1
        while True:
            if (j-c > w-1) or (j-c < 0) or (field[i][j-c] == '#'):
                break
            
            else:
                n_light += 1
                c += 1
        
        c = 1
        while True:
            if (i+c > h-1) or (i+c < 0) or (field[i+c][j] == '#'):
                break
            
            else:
                n_light += 1
                c += 1
        
        c = 1
        while True:
            if (i-c > h-1) or (i-c < 0) or (field[i-c][j] == '#'):
                break
            
            else:
                n_light += 1
                c += 1
        
        max_light = max(max_light,n_light)


print(max_light)

Submission status: Time Limit Exceeded

Solution code:
h,w = map(int,input().split())
s = [[(l == '.')*1 for l in list(input())] for _ in range(h)]
U = [[0]*w for _ in range(h)]
D = [[0]*w for _ in range(h)]
L = [[0]*w for _ in range(h)]
R = [[0]*w for _ in range(h)]
ans = 0
for i in range(h):
  for j in range(w):
    if i == 0:
      U[i][j] = s[i][j]
      D[h-i-1][j] = s[h-i-1][j]
    else:
      U[i][j] = (U[i-1][j]+1)*s[i][j]
      D[h-i-1][j] = (D[h-i][j]+1)*s[h-i-1][j]
    if j == 0:
      L[i][j] = s[i][j]
      R[i][w-j-1] = s[i][w-j-1]
    else:
      L[i][j] = (L[i][j-1]+1)*s[i][j]
      R[i][w-j-1] = (R[i][w-j]+1)*s[i][w-j-1]
for i in range(h):
  for j in range(w):
    m = L[i][j]+R[i][j]+U[i][j]+D[i][j]-3
    if ans < m:
      ans = m
print(ans)

Submission status: Time Limit Exceeded

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

for i in range(h):
    c = 1
    for j in range(w):
        if s[i][j] == "#": c = 1
        else:
            cnt[i][j] += c
            c += 1
    c = 1
    for j in range(w)[::-1]:
        if s[i][j] == "#": c = 1
        else:
            cnt[i][j] += c
            c += 1

for j in range(w):
    c = 1
    for i in range(h):
        if s[i][j] == "#": c = 1
        else:
            cnt[i][j] += c
            c += 1
    c = 1
    for i in range(h)[::-1]:
        if s[i][j] == "#": c = 1
        else:
            cnt[i][j] += c
            c += 1

print(max(max(i) for i in cnt))

Submission status: Time Limit Exceeded

Solution code:
import sys
input=sys.stdin.readline
h,w=map(int,input().split())
s=[list(input()) for i in range(h)]
u=[[0]*w for i in range(h)]
d=[[0]*w for i in range(h)]
l=[[0]*w for i in range(h)]
r=[[0]*w for i in range(h)]
for i in range(h):
  for j in range(w):
    if s[i][j]=='#':
      d[i][j]=0
      l[i][j]=0
    else:
      if i!=0:
        d[i][j]+=d[i-1][j]+1
      else:
        d[i][j]=1
      if j!=0:
        l[i][j]+=l[i][j-1]+1
      else:
        l[i][j]=1
for i in range(h)[::-1]:
  for j in range(w)[::-1]:
    if s[i][j]=='#':
      r[i][j]=0
      u[i][j]=0
    else:
      if i!=h-1:
        u[i][j]+=u[i+1][j]+1
      else:
        u[i][j]=1
      if j!=w-1:
        r[i][j]+=r[i][j+1]+1
      else:
        r[i][j]=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:
import sys
input=sys.stdin.readline
h,w=map(int,input().split())
s=[list(input()) for i in range(h)]
u=[[0]*w for i in range(h)]
d=[[0]*w for i in range(h)]
l=[[0]*w for i in range(h)]
r=[[0]*w for i in range(h)]
for i in range(w):
  if s[0][i]=='.':
    u[0][i]=1
  for j in range(1,h):
    if s[j][i]=='.':
      u[j][i]+=u[j-1][i]+1
for i in range(w):
  if s[h-1][i]=='.':
    d[h-1][i]=1
  for j in range(h-2,-1,-1):
    if s[j][i]=='.':
      d[j][i]+=d[j+1][i]+1
for i in range(h):
  if s[i][0]=='.':
    l[i][0]=1
  for j in range(1,w):
    if s[i][j]=='.':
      l[i][j]+=l[i][j-1]+1
for i in range(h):
  if s[i][w-1]=='.':
    r[i][w-1]=1
  for j in range(w-2,-1,-1):
    if s[i][j]=='.':
      r[i][j]+=r[i][j+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())
s=[list(input()) for i in range(h)]
u=[[0]*w for i in range(h)]
d=[[0]*w for i in range(h)]
l=[[0]*w for i in range(h)]
r=[[0]*w for i in range(h)]
for i in range(w):
  if s[0][i]=='.':
    u[0][i]=1
  for j in range(1,h):
    if s[j][i]=='.':
      u[j][i]+=u[j-1][i]+1
for i in range(w):
  if s[h-1][i]=='.':
    d[h-1][i]=1
  for j in range(h-2,-1,-1):
    if s[j][i]=='.':
      d[j][i]+=d[j+1][i]+1
for i in range(h):
  if s[i][0]=='.':
    l[i][0]=1
  for j in range(1,w):
    if s[i][j]=='.':
      l[i][j]+=l[i][j-1]+1
for i in range(h):
  if s[i][w-1]=='.':
    r[i][w-1]=1
  for j in range(w-2,-1,-1):
    if s[i][j]=='.':
      r[i][j]+=r[i][j+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())
s=[list(input()) for i in range(h)]
u=[[0]*w for i in range(h)]
d=[[0]*w for i in range(h)]
l=[[0]*w for i in range(h)]
r=[[0]*w for i in range(h)]
for i in range(w):
  if s[0][i]=='.':
    u[0][i]=1
  for j in range(1,h):
    if s[j][i]=='.':
      u[j][i]+=u[j-1][i]+1
for i in range(w):
  if s[h-1][i]=='.':
    d[h-1][i]=1
  for j in range(h-2,-1,-1):
    if s[j][i]=='.':
      d[j][i]+=d[j+1][i]+1
for i in range(h):
  if s[i][0]=='.':
    l[i][0]=1
  for j in range(1,w):
    if s[i][j]=='.':
      l[i][j]+=l[i][j-1]+1
for i in range(h):
  if s[i][w-1]=='.':
    r[i][w-1]=1
  for j in range(w-2,-1,-1):
    if s[i][j]=='.':
      r[i][j]+=r[i][j+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())
S = [None] * H
for i in range(H):
  S[i] = input()

up = [[0 for w in range(W)] for h in range(H)]
down = [[0 for w in range(W)] for h in range(H)]
left = [[0 for w in range(W)] for h in range(H)]
right = [[0 for w in range(W)] for h in range(H)]

for h in range(H):
  for w in range(W):
    # up
    if h-1 >= 0:
      if S[h-1][w] == "#":
        up[h][w] = 0
      else:
        up[h][w] = up[h-1][w] + 1
    # left
    if w-1 >= 0:
      if S[h][w-1] == "#":
        left[h][w] = 0
      else:
        left[h][w] = left[h][w-1] + 1

    _h = H-1-h
    _w = W-1-w
    # down
    if _h+1 <= H-1:
      if S[_h+1][_w] == "#":
        down[_h][_w] = 0
      else:
        down[_h][_w] = down[_h+1][_w] + 1
    # right
    if _w+1 <= W-1:
      if S[_h][_w+1] == "#":
        right[_h][_w] = 0
      else:
        right[_h][_w] = right[_h][_w+1] + 1

ans = 0
for h in range(H):
  for w in range(W):
    if S[h][w] == "#":
      continue
    masu = 1
    masu += up[h][w]
    masu += down[h][w]
    masu += left[h][w]
    masu += right[h][w]
    if masu > ans:
      ans = masu

print(ans)

Submission status: Time Limit Exceeded

Solution code:
H,W = map(int,input().split())
s = [list(map(lambda x: x == '.', list(input()))) for i in range(H)]
v = [[0]*W for i in range(H)]
h = [[0]*W for i in range(H)]

for i in range(H):
  for j in range(W):
    if not s[i][j]:
      continue
    h[i][j] = v[i][j] = s[i][j]
    if j:
      v[i][j] += v[i][j-1]
    if i:
      h[i][j] += h[i-1][j]
hx, hy = W, 0
vx, vy = 0, H
cv, ch = 0, 0
fh, fv = 0, 0
for _ in range(H*W):
  hy -= 1
  if hy < 0:
    hx -= 1
    hy += H
    fh = 1
  vx -= 1
  if vx < 0:
    vy -= 1
    vx += W
    fv = 1
  if fh or not ch or not h[hy][hx]:
    ch = h[hy][hx]
    fh = 0
  else:
    h[hy][hx] = ch
  if fv or not cv or not v[vy][vx]:
    cv = v[vy][vx]
    fv = 0
  else:
    v[vy][vx] = cv
print(max(h[i][j] + v[i][j] for i in range(H) for j in range(W)) - 1)

Submission status: Time Limit Exceeded

Solution code:
H,W = map(int,input().split())
s = [[] for i in range(H)]
for i in range(H): s[i] = list(input())
l = [[0]*W for i in range(H)]
r = [[0]*W for i in range(H)]
d = [[0]*W for i in range(H)]
u = [[0]*W for i in range(H)]

for h in range(H):
    if s[h][0] == ".": l[h][0] = 1
    for w in range(1,W):
        if s[h][w] == ".": l[h][w] = l[h][w-1] + 1
    if s[h][W-1] == ".": r[h][W-1] = 1
    for w in range(1,W):
        if s[h][W-1-w] == ".": r[h][W-1-w] = r[h][W-w] + 1

for w in range(W):
    if s[0][w] == ".": d[0][w] = 1
    for h in range(1,H):
        if s[h][w] == ".": d[h][w] = d[h-1][w] + 1
    if s[H-1][w] == ".": u[H-1][w] = 1
    for h in range(1,H):
        if s[H-1-h][w] == ".": u[H-1-h][w] = u[H-h][w] + 1
ans = 0
for h in range(H):
    for w in range(W):
        ans = max(ans, l[h][w]+r[h][w]+u[h][w]+d[h][w]-3)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
H,W = map(int,input().split())
s = [[] for i in range(H)]
for i in range(H): s[i] = list(input())
l = [[0]*W for i in range(H)]
r = [[0]*W for i in range(H)]
d = [[0]*W for i in range(H)]
u = [[0]*W for i in range(H)]

for h in range(H):
    for w in range(W):
        if s[h][w] == ".":
            l[h][w], d[h][w] = 1,1
            if w != 0: l[h][w] = l[h][w-1] + 1
            if h != 0: d[h][w] = d[h-1][w] + 1
    for w in range(W):
        if s[H-1-h][W-1-w] == ".":
            r[H-1-h][W-1-w] = 1
            if w != 0: r[H-1-h][W-1-w] = r[H-1-h][W-w] + 1

for h in range(H):
    for w in range(W):
        if s[H-1-h][W-1-w] == ".":
            u[H-1-h][W-1-w] = 1
            if h != 0: u[H-1-h][W-1-w] = u[H-h][W-1-w] + 1
ans = 0
for h in range(H):
    for w in range(W):
        ans = max(ans, l[h][w]+r[h][w]+u[h][w]+d[h][w]-3)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
H,W = map(int,input().split())
s = [[] for i in range(H)]
for i in range(H): s[i] = list(input())
l = [[0]*W for i in range(H)]
r = [[0]*W for i in range(H)]
d = [[0]*W for i in range(H)]
u = [[0]*W for i in range(H)]

for h in range(H):
    for w in range(W):
        if s[h][w] == ".":
            l[h][w], d[h][w] = 1,1
            if w != 0: l[h][w] = l[h][w-1] + 1
            if h != 0: d[h][w] = d[h-1][w] + 1
    for w in range(W):
        if s[H-1-h][W-1-w] == ".":
            r[H-1-h][W-1-w],u[H-1-h][W-1-w] = 1,1
            if w != 0: r[H-1-h][W-1-w] = r[H-1-h][W-w] + 1
            if h != 0: u[H-1-h][W-1-w] = u[H-h][W-1-w] + 1

ans = 0
for h in range(H):
    for w in range(W):
        ans = max(ans, l[h][w]+r[h][w]+u[h][w]+d[h][w]-3)

print(ans)