Problem:


Score : 200 points

Problem Statement

There is a grid of squares with H horizontal rows and W vertical columns.
The square at the i-th row from the top and the j-th column from the left is represented as (i, j).
Each square is black or white.
The color of the square is given as an H-by-W matrix (a_{i, j}).
If a_{i, j} is ., the square (i, j) is white; if a_{i, j} is #, the square (i, j) is black.

Snuke is compressing this grid.
He will do so by repeatedly performing the following operation while there is a row or column that consists only of white squares:

Operation: choose any one row or column that consists only of white squares, remove it and delete the space between the rows or columns.

It can be shown that the final state of the grid is uniquely determined regardless of what row or column is chosen in each operation.
Find the final state of the grid.

Constraints

1 \leq H, W \leq 100

a_{i, j} is . or #.

There is at least one black square in the whole grid.

Input

Input is given from Standard Input in the following format:

H W
a_{1, 1}...a_{1, W}
:
a_{H, 1}...a_{H, W}

Output

Print the final state of the grid in the same format as input (without the numbers of rows and columns); see the samples for clarity.

Sample Input 1

4 4
##.#
....
##.#
.#.#

Sample Output 1

###
###
.##

The second row and the third column in the original grid will be removed.

Sample Input 2

3 3
#..
.#.
..#

Sample Output 2

#..
.#.
..#

As there is no row or column that consists only of white squares, no operation will be performed.

Sample Input 3

4 5
.....
.....
..#..
.....

Sample Output 3

#

Sample Input 4

7 6
......
....#.
.#....
..#...
..#...
......
.#..#.

Sample Output 4

..#
#..
.#.
.#.
#.#



Sample input:
4 4
##.#
....
##.#
.#.#


Sample output:
###
###
.##

Submission status: Accepted

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

def main():
    H, W = map(int, readline().rstrip().split())
    mat = []
    for _ in range(H):
        inp = readline().rstrip()
        if '#' not in inp:
            continue
        mat.append(list(inp))
    
    ws = []
    for w in range(W):
        flg = False
        for h in range(len(mat)):
            if mat[h][w] == '#':
                flg = True
                break
        if flg:
            ws.append(w)
    
    ans = [''.join([l[j] for j in range(len(l)) if j in ws]) for l in mat]
    print(*ans, sep='\n')


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import numpy as np


h, w = map(int, input().split())
a = []
for i in range(h):
    a.append(list(input()[:]))
a = np.array(a)

n = 0
for i in range(h):
    if all(a[n, :]=='.'):
        a = np.delete(a, n, axis=0)
    else:
        n += 1
n = 0
for i in range(w):
    if all(a[:, n]=='.'):
        a = np.delete(a, n, axis=1)
    else:
        n += 1

for i in range(len(a)):
    for j in range(len(a[0])):
        print(a[i, j], end='')
    print()

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
a=[list(input()) for a in range(h)]
a2=[b for b in a if b.count("#")]
x=[b for b in list(zip(*a2)) if b.count("#")]
y=list(zip(*x))
for a in y:
    print("".join(a))

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
masulist=[list(input()) for a in range(h)]
masulist2=[b for b in masulist if b.count("#")]
x=[b for b in list(zip(*masulist2)) if b.count("#")]
y=list(zip(*x))
for a in y:
    print("".join(a))

Submission status: Accepted

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

h = []
for x in range(H):
    if all(a == '.' for a in grid[x]):
        h.append(x)
w = []
for y in range(W):
    if all(grid[x][y] == '.' for x in range(H)):
        w.append(y)


for x in range(H):
    if x in h:
        continue
    ans = []
    for y in range(W):
        if y not in w:
            ans.append(grid[x][y])
    print(*ans, sep='')

Submission status: Accepted

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

line = []
for i in range(h):
    vec.append(input())
    if '#' in vec[i]:
        line.append(i)

row = []
for i in range(w):
    for j in range(h):
        if vec[j][i] == '#':
            row.append(i)
            break

for i in line:
    for j in row:
        print(vec[i][j], end='')
    
    print()

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
vec =[]
for i in range(h):
    a = list(input())
    if '#' in a:
        vec.append(a)
    else:
        h -= 1
        
#print(h, w)
#print(vec)


num = []
for i in range(w):
    for j in range(h):
        if vec[j][i] == '#':
            num.append(i)
            break
    
for i in range(h):
    for j in num:
        print(vec[i][j], end='')
    
    print()

Submission status: Accepted

Solution code:
from functools import reduce
H, W =  map(int,input().split())
list1 = [input() for i in range(H)]
row_index = []
column_index =[]
[row_index.append(i) for i, elem in  enumerate(list1) if elem.count(".") < W]
[column_index.append(i) for i,elem in enumerate(zip(*list1)) if elem.count(".")<H]
for i in  row_index:
  for j in column_index:
    print(list1[i][j],end="")
  print("")

Submission status: Accepted

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

def rem(a):
    l = set([])
    c = set([])
    for i in range(len(a)):
        if ''.join(a[i]) == '.'*len(a[i]):
            l.add(i)
    for j in range(len(a[0])):
        col = ''.join([a[i][j] for i in range(len(a))])
        if col == '.'*len(a):
            c.add(j)
    if len(l) == 0 and len(c) == 0:
        return a
    else:
        a_new = []
        for i in range(len(a)):
            if i in l: continue
            line = []
            for j in range(len(a[0])):
                if j in c: continue
                line.append(a[i][j])
            a_new.append(line)
        return rem(a_new)
ans = rem(a)
for line in range(len(ans)):
    print(''.join(ans[line]))

Submission status: Accepted

Solution code:
H,W=map(int,input().split())
mas=[list(s for s in input()) for _ in range(H)]
height=[]
width=[]
for i in range(H):
    for j in range(W):
        if mas[i][j]=='#':
            height.append(i)
            width.append(j)

height=set(height)
height=list(height)
width=set(width)
width=list(width)
for a in height:
    ans=[]
    for b in width:
        ans.append(mas[a][b])
    print(*ans,sep='')

Submission status: Accepted

Solution code:
def ii():return int(input())
def iim():return map(int,input().split())
def iil():return list(map(int,input().split()))
def ism():return map(str,input().split())
def isl():return list(map(str,input().split()))

h,w = iim()
l = []
for i in range(h):
    s = list(input())
    if '#' in s:
        l.append(s)

h = len(l)
for j in range(w)[::-1]:
    flag = True
    for k in range(h):
        if l[k][j] == '#':
            flag = False
    if flag:
        for n in range(h):
            l[n].pop(j)

for m in range(h):
    print(''.join(l[m]))

Submission status: Accepted

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

h_ans = h
del_list_h = []
for i in range(h):
    for j in range(w):
        if a_l[i][j] == '#':
            break
    else:
        del_list_h.append(i)
        h_ans -= 1

for i in del_list_h[::-1]:
    del a_l[i]

del_list_w = []
for j in range(w):
    for i in range(h_ans):
        if a_l[i][j] == '#':
            break
    else:
        del_list_w.append(j)

for j in del_list_w[::-1]:
    for k in range(h_ans):
        del a_l[k][j]

for ans in a_l:
    print(''.join(ans))

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
S=[]
for i in range(h):
    s=input()
    if all([k=="." for k in s]):
        continue
    S.append(s)
if len(S)!=0:
    C=[]
    for j in range(w):
        if all([S[i][j]=="." for i in range(len(S))]):
            C.append(j)

for i in range(len(S)):
    for j in range(w):
        if j in C:
            continue
        print(S[i][j],end="")
    print("",end="\n")

Submission status: Accepted

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

for i in range(h):
    tmp = input()
    if(tmp != w*"."):
        A.append(tmp)

D = []

for j in range(w):
    flag = True
    for B in A:
        if(B[j] == "#"):
            flag = False
    if(flag):
        D.append(j)

for B in A:
    for j in range(w):
        if(j not in D):
            print(B[j], end="")
    print()

Submission status: Accepted

Solution code:
import sys
input= lambda: sys.stdin.readline().rstrip()
def pin(type=int):
    return map(type,input().split())

#%%code
def resolve():
    H,W=pin()
    maze=[list(input()) for oo in range(H)]
    vacanth=[]
    vacantw=[]
    for h in range(H):
        if "#" not in maze[h]:vacanth.append(h)
    for w in range(W):
        flag=1
        for h2 in range(H):
            if maze[h2][w]=="#":
                flag=0
                break
        if flag==1:vacantw.append(w)

    for i in range(H):
        if i in vacanth:continue
        temp=""
        for j in range(W):
            if j in vacantw:continue
            temp+=maze[i][j]
        print(temp)
    
#%%submit! 
resolve()

Submission status: Accepted

Solution code:
import sys
# input = sys.stdin.readline
from itertools import accumulate

def readlines(h, w):
    for _ in range(h):
        row = input()
        if not all(cell == "." for cell in row):
            yield row

def main():
    h, w = map(int, input().split())
    grid = list(readlines(h, w))

    columns = set()
    for i in range(w):
        if all(row[i]=="." for row in grid):
            columns.add(i)
    
    for i in range(len(grid)):
        for j in range(w):
            if j not in columns:
                print(grid[i][j], end="")
        print()


main()

Submission status: Accepted

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

a = list(zip(*[i for i in a if '#' in i]))
a = list(zip(*[i for i in a if '#' in i]))

for n in range(len(a)):
    print("".join(a[n]))

Submission status: Accepted

Solution code:
a,b = list(map(int,input().split()))
A = []
AAA = 0
list = [["" for i in range(b)] for j in range(a)]
for i in range(a):
    n = input()
    if n.count(".") == b:
       pass 
    else:
        for j in range(b):
            list[i][j] = n[j]
for i in range(b):
    for j in range(a):
        A.append(list[j][i])
    if len(A) == A.count(".")+A.count(""):
        for k in range(a):
            list[k][i] = ""
    A = []
for i in range(a):
    for j in range(b):
        if list[i][j] != "":
            print(list[i][j],end="")
            AAA = 1
    if AAA == 1:
        print()
    AAA = 0

Submission status: Accepted

Solution code:
# from math import factorial,sqrt,ceil,gcd
# from itertools import permutations as permus
# from collections import deque,Counter
# import re
# from functools import lru_cache # 簡単メモ化 @lru_cache(maxsize=1000)
# from decimal import Decimal, getcontext
# # getcontext().prec = 1000
# # eps = Decimal(10) ** (-100)

import numpy as np
# import networkx as nx
# from scipy.sparse.csgraph import shortest_path, dijkstra, floyd_warshall, bellman_ford, johnson
# from scipy.sparse import csr_matrix
# from scipy.special import comb

# slist = "abcdefghijklmnopqrstuvwxyz"
# 盤面の受け取り
H,W = map(int,input().split())
board = []
for i in range(H):
    word = input()
    if "#" in word:
        board.append(list(word))

board = np.array(board).T
ans = []
for b in board:
    if "#" in b:
        ans.append(b)

ans = np.array(ans).T

for a in ans:
    print(*a,sep="")
# print(*ans)   # unpackして出力。間にスペースが入る
# for row in board:
#     print(*row,sep="")    #unpackして間にスペース入れずに出力する
# print("{:.10f}".format(ans))
# print("{:0=10d}".format(ans))

Submission status: Accepted

Solution code:
def myAnswer(H:int,W:int,A:list) -> None:
   ans = []
   for a in A:
      for i in a:
         if(i == "#"):
            ans.append(a)
            break
         
   ansT = list(zip(*ans))
   ans = []
   for t in ansT:
      for i in t:
         if(i == "#"):
            ans.append(t)
            break
   ans = list(zip(*ans))
   for a in ans:
      string = ""
      for i in a:
         string +=i
      print(string)


def modelAnswer():
   return
def main():
   H,W = map(int,input().split())
   A = []
   for _ in range(H):
      A.append(list(input()))
   myAnswer(H,W,A[:])
if __name__ == '__main__':
   main()

Submission status: Accepted

Solution code:
def resolve():
    '''
    code here
    '''
    import numpy as np
    H, W = [int(item) for item in input().split()]
    grid = [[item for item in input()] for _ in range(H)]

    temp_grid = []
    for line in grid:
        temp = [item == '#' for item in line]
        if any(temp):
            temp_grid.append(line)
            
    temp_grid = np.array(temp_grid)
    temp_grid2 = []
    for j in range(W):
        line = temp_grid[:,j]
        temp = [item == '#' for item in line]
        if any(temp):
            temp_grid2.append(line)

    
    temp_grid2 = np.array(temp_grid2)
    res = np.rot90(temp_grid2, 3)
    
    for line in res:
        print(''.join(line[::-1]))




if __name__ == "__main__":
    resolve()

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
a=[[j for j in input()] for i in range(h)]
b=[x for x in a if "#" in x]  #1つでも#があれば、bにいれる　つまり全部...ならば除外される
c=zip(*[y for y in zip(*b) if "#" in y]) #zip(*)で転置行列になる！！！
for d in c:print("".join(d))

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
a = []
colrow = []

for i in range(h):
    b = list(input())
    if set(b) == {"."}:
            continue
    a.append(b)

for x in zip(*a): #各リストの1番目から順番に出力
    if set(x) == {"."}:
        continue
    colrow.append(x)

for x in zip(*colrow):
    print(*x, sep="")

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
a = []
delcol = []
cnt = 0

for i in range(h):
    b = list(input())
    if set(b) == {"."}:
            continue
    a.append(b)

for i in range(w):
    for j in range(len(a)):
        if a[j][i] != ".":
            break
        else:
            cnt += 1
        if cnt == len(a):
            delcol.append(i)
    cnt = 0

for i in delcol [::-1]: #列を削除
    for j in range(len(a)):
        del a[j][i]

for i in range(len(a)):
    print(*a[i], sep="")

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
a = []
delrow = []
delcol = []
cnt = 0

for i in range(h):
    a.append(list(input()))

for i in range(h):
    if set(a[i]) == {"."}:
        delrow.append(i)

for i in range(w):
    for j in range(h):
        if a[j][i] != ".":
            break
        else:
            cnt += 1
        if cnt == h:
            delcol.append(i)
    cnt = 0
        
for i in delrow [::-1]: #行を削除
    del a[i]
for i in delcol [::-1]: #列を削除
    for j in range(len(a)):
        del a[j][i]

for i in range(len(a)):
    print(*a[i], sep="")

Submission status: Accepted

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

grid = []
del_i = []
del_n = []

for i in range(num[0]):
  grid.append(list(input()))

  if "#" in grid[i] and "." in grid[i]:
    continue
  elif "#" in grid[i]:
    continue
  else:
    del_i.append(i)

check_1, check_2 = 0, 0

for n in range(num[1]):
  check_1, check_2 = 0, 0
  for i in range(num[0]):
    if grid[i][n] == "#":
      check_1 += 1
      if check_2 > 0:
        break
      # if check_1 == num[0]:
      #   del_n.append(n)
    else:
      check_2 += 1
      if check_1 > 0:
        break
      if check_2 == num[0]:
        del_n.append(n)

for i in range(num[0]):
  if i in del_i:
    continue
  else:
    m = 0
    for n in del_n:
      n = n - m
      del grid[i][n]
      m += 1
    print(''.join(grid[i]))

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
a = []
delrow = []
delcol = []
cnt = 0

for i in range(h):
    a.append(list(input()))

for i in range(h):
    for j in range(w):
        if a[i][j] != ".":
            break
        else:
            cnt += 1
        if cnt == w:
            delrow.append(i)
    cnt = 0

for i in range(w):
    for j in range(h):
        if a[j][i] != ".":
            break
        else:
            cnt += 1
        if cnt == h:
            delcol.append(i)
    cnt = 0

for i in delrow [::-1]: #行を削除
    del a[i]
for i in delcol [::-1]: #列を削除
    for j in range(len(a)):
        del a[j][i]

for i in range(len(a)):
    print(*a[i], sep="")

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
grid = []
for i in range(h):
    temp = list(input())
    if set(temp) != {'.'}:
        grid.append(temp)


transpose = [list(x) for x in zip(*grid)]

ans = []
for l in transpose:
    if set(l) != {'.'}:
        ans.append(l)

ans = [list(x) for x in zip(*ans)]
for l in ans:
    print(''.join(l))

Submission status: Accepted

Solution code:
#!/usr/local/bin python
# -*- coding: utf-8 -*-
#
# ~/PycharmProjects/atcoder/B-GridCompression.py
#
import numpy as np

h, w = map(int, input().split())
a = []
for i in range(h):
    a_i = input()
    if a_i != '.' * w:
        a.append(list(a_i))

b = [""] * len(a)

for j in range(w):
    for i in range(len(a)):
        if a[i][j] == "#":
            break

    if a[i][j] == "#":
        for ii in range(len(a)):
            b[ii] = b[ii] + a[ii][j]

for i in range(len(a)):
    print(b[i])

Submission status: Accepted

Solution code:
### ABC107 B - Grid Compression
h, w = map(int, input().split())
grid = [[x for x in str(input())] for _ in range(h)]

del_h = [False] * h
del_w = [False] * w

for i in range(h):
  if grid[i].count('#') == 0:
    del_h[i] = True

for j in range(w):
  del_w[j] = True
  for i in range(h):
    if grid[i][j] == '#':
      del_w[j] = False
      break

for i in range(h):
  if del_h[i]: continue
  row = ''.join([grid[i][j] for j in range(w) if not del_w[j]])
  print(row)

Submission status: Accepted

Solution code:
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    #from collections import defaultdict
    from itertools import combinations, permutations, accumulate, groupby, product
    from bisect import bisect_left,bisect_right
    from heapq import heapify, heappop, heappush
    from math import floor, ceil
    #from operator import itemgetter

    #inf = 10**17
    #mod = 10**9 + 7

    h,w = map(int, input().split())
    a = [list(input().rstrip()) for _ in range(h)]
    tate = []
    yoko = []
    for i in range(h):
        if '#' not in a[i]:
            yoko.append(i)
    for i in range(w):
        for j in range(h):
            if a[j][i] == '#':
                break
        else:
            tate.append(i)
    res = []
    for i in range(h):
        temp = []
        if i in yoko:
            continue
        for j in range(w):
            if j in tate:
                continue
            temp.append(a[i][j])
        res.append(temp)
    for i in res:
        print(''.join(i))

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
### ABC107 B - Grid Compression
h, w = map(int, input().split())
grid = [[x for x in str(input())] for _ in range(h)]

del_h = [False] * h
del_w = [False] * w

for i in range(h):
  if grid[i].count('#') == 0:
    del_h[i] = True

for j in range(w):
  del_w[j] = True
  for i in range(h):
    if grid[i][j] == '#':
      del_w[j] = False
      break

for i in range(h):
  if del_h[i]: continue
  row = ''.join([grid[i][j] for j in range(w) if not del_w[j]])
  print(row)

Submission status: Accepted

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

A = [A[i] for i in range(h) if any(x == '#' for x in A[i])] 
h = len(A)
ans = [[] for i in range(h)]
for i in range(w):
    if any(x[i] == '#' for x in A):
        for j, x in enumerate(A):
            ans[j].append(x[i])
print(*[''.join(row) for row in ans], sep='\n')

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/abc107/tasks/abc107_b
from copy import deepcopy
h, w = map(int, input().split())
matrix = []
for _ in range(h):
    row = list(input())
    matrix.append(row)

# delete row
rows = []
for i in range(h):
    white = False
    for j in range(w):
        if matrix[i][j] == '#':
            break
    else:
        white = True
    if white:
        rows.append(i)
t = []
for i in range(h):
    if i in rows:
        continue
    t.append(matrix[i])
h -= len(rows)
matrix = t

# delete col
cols = []
for i in range(w):
    white = False
    for j in range(h):
        if matrix[j][i] == '#':
            break
    else:
        white = True
    if white:
        cols.append(i)

t = []
for i in range(h):
    row = []
    for j in range(w):
        if j in cols:
            continue
        row.append(matrix[i][j])
    t.append(row)
w -= len(cols)
matrix = t
for r in matrix:
    print(''.join(r))

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
a=[]
for i in range(h):
    a1=list(input())
    if a1!=['.']*w:
        a.append(a1)
    else:
        h-=1
i=0
while i < w:
    if [a[j][i]=='.' for j in range(h)]==[True]*h:
        w-=1
        a=[a[j][:i]+a[j][i+1:] for j in range(h)]
        #print(a)
    else:
        i+=1
for i in range(h):
    print(''.join(a[i]))

Submission status: Accepted

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

A = [list(input()) for i in range(H)]

l=[False]*H
c=[False]*W

for i in range(H):
  for j in range(W):
    if A[i][j]=='#':
      l[i]=True
      c[j]=True

ans=['' for i in range(H)]
for i in range(H):
  for j in range(W):
    if l[i] and c[j]:
      ans[i]+=A[i][j]
for i in range(H):
  if str(ans[i])!='':
    print(str(ans[i]))

Submission status: Accepted

Solution code:
gyo,retu = map(int,input().split())
ans = [0]*gyo
check = [0]*retu
flag1 = "ng"
for i in range(gyo):
    dum = input()
    for j in range(retu):
        if dum[j] == "#":
            flag1 = "ok"
            check[j] = 1
    if flag1 == "ok":
        ans[i] = dum
        flag1 = "ng"

for l in range(gyo-1,-1,-1):
    if ans[l] == 0:
        ans.pop(l)
ans_len = len(ans)
#print(ans_len)
for k in range(retu-1,-1,-1):
    #print(k,check[k])
    #print(ans)
    if check[k] == 0:
        for m in range(ans_len):
            dum3 = list(ans[m])
            dum3.pop(k)
            ans[m] = dum3
            #dum2 = ans[m]
            #dum2 = dum2.pop(k)
            #ans[m] = dum2

for n in ans:
  print("".join(n))

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/abc107/tasks/abc107_b
# 愚直かな

import sys
sys.setrecursionlimit(1 << 25)
ra = range
enu = enumerate

read = sys.stdin.readline


def read_map_as(H, replace={'#': 1, '.': 0}):
    '''
    文字列のmapを置換して読み込み。デフォでは#→1,.→0
    '''
    ret = []
    for _ in range(H):
        ret.append([replace[s] for s in read()[:-1]])
        # 内包表記はpypyでは若干遅いことに注意
        # #numpy使うだろうからこれを残しておくけど
    return ret


H, W = map(int, input().split())
A = read_map_as(H)
import numpy as np
A = np.array(A)
# 削除する行の取得
del_i = set()
for i in range(H):
    if A[i].sum() == 0:
        del_i.add(i)

del_j = set()
for j in range(W):
    if A[:, j].sum() == 0:
        del_j.add(j)

# print
for i in range(H):
    if i in del_i:
        continue
    for j in range(W):
        if j in del_j:
            continue
        print('#' if A[i, j] == 1 else '.', end='')
    print()

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
a = [input() for _ in range(h)]
r = [False]*h
c = [False]*w
for i in range(h):
    for j in range(w):
        if a[i][j] == "#":
            r[i] = True
            c[j] = True
for i in range(h):
    if r[i]:
        for j in range(w):
            if c[j]:
                print(a[i][j], end = "")
        print()

Submission status: Accepted

Solution code:
H ,W = map(int, input().split())
matrix = []
for i in range(H):
    m = list(input())
    matrix.append(m)
flag = True
while(flag):
    flag = False
    for i in range(H):
        if matrix[i].count(".")==W:
            del matrix[i]
            H -= 1
            flag = True
            break
    for i in range(W):
        cnt = 0
        for j in range(H):
            if matrix[j][i]==".":
                cnt += 1
        if cnt==H:
            for k in range(H):
                del matrix[k][i]
            flag = True
            W -= 1
            break
for i in matrix:
    print("".join(i))

Submission status: Accepted

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

for i in range(h):
    if a[i].count(".")==w:
        b.append(i)

for i in reversed(b):
    a.pop(i)

h-=len(b)
for i in range(w-1,-1,-1):
    flag=True
    for j in range(h-1,-1,-1):
        if a[j][i]=="#":
            flag=False
    if flag:
        for k in range(h-1,-1,-1):
            a[k]=a[k][:i]+a[k][i+1:]

for i in range(h):
    print(a[i])

Submission status: Accepted

Solution code:
h, w = [int(x) for x in input().split()]
A = []
B = []

# 列削除用フラグ初期化 1で削除
rm_pos = [1]*w

# 白マスのみの行はリスト追加しない
for i in range(h):
    a = input()
    if a != '.' * w:
        A += [list(a)]

# 白マスのみの列 検索、. が含まれていない場合は 削除フラグを0に
for j in range(w):
    for i in range(len(A)):
        if A[i][j] != '.':
            rm_pos[j] = 0 
            break

#  del list を使わず
#　列削除フラグが 0 の列のみ新規リストへ追加

for i in range(len(A)):
    tmp = []
    for j, flg in enumerate(rm_pos):
        if flg == 0:
            tmp += A[i][j]
    B += [tmp]

# 回答出力
for i in range(len(B)):
    print(''.join(B[i]))

Submission status: Accepted

Solution code:
h, w = [int(x) for x in input().split()]
A = []
# 列削除用フラグ初期化 1で削除
rm_pos = [1]*w

# 白マスのみの行はリスト追加しない
for i in range(h):
    a = input()
    if a != '.' * w:
        A += [list(a)]

# 白マスのみの列 検索、. が含まれていない場合は 削除フラグを0に
for j in range(w):
    for i in range(len(A)):
        if A[i][j] != '.':
            rm_pos[j] = 0 
            break

#　後ろ側から  列削除フラグが 1 の列を削除
for j in range(w)[::-1]:
    if rm_pos[j] == 1:
        for i in range(len(A)):
            del A[i][j]

# 回答出力
for i in range(len(A)):
    print(''.join(A[i]))

Submission status: Accepted

Solution code:
h, w = [int(x) for x in input().split()]
A = []
rm_pos = []

for i in range(h):
    a = input()
    if a != '.' * w:
        A += [list(a)]

flg = True

# 白マスのみの列 検索
for j in range(w):
    for i in range(len(A)):
        if A[i][j] != '.':
            flg = False
            break
    if flg == True:
        rm_pos += [j]
    flg = True
rm_pos = reversed(rm_pos)

# 列の削除
for j in rm_pos:
    for i in range(len(A)):
        del A[i][j]


# 回答出力
for i in range(len(A)):
    print(''.join(A[i]))

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 6)


def input(): return sys.stdin.readline().rstrip()

H, W = map(int, input().split())
a = [input() for _ in range(H)]

rows =[r for r in range(H) if '#' in a[r]]
columns = [c for c in range (W) if '#' in [a[r][c] for r in range(H)]]

for r in rows:
    for c in columns:
        print(a[r][c], end="")
    print()

Submission status: Accepted

Solution code:
h,w = map(int,input().split(' '))
A = []
Ilist = [1]*w
for p in range(h):
    a=input()
    if '#' in a:
        A.append(a)
        i = 0
        while i < w:
            if a[i] == '#':
                Ilist[i] *= 0
            i += 1
#Ilist[k] = 1であるkを除外。
ans = ''
for a0 in A:
    k = 0
    while k < w:
        if Ilist[k] == 0:
            ans += a0[k]
        k += 1
    ans += '\n'
print(ans)

Submission status: Accepted

Solution code:
def solve():
    H, W = map(int, input().split())
    A = []
    for _ in range(H):
        inp = [s for s in input()]
        if inp != ['.']*W:
            A.append(inp)
    Ans = list(zip(*A))
    P = ['.' for _ in range(len(A))]
    while tuple(P) in Ans:
        Ans.remove(tuple(P))
    Ans = list(zip(*Ans))
    for i in range(len(Ans)):
        Ans[i] = ''.join(Ans[i])
    return Ans
print(*solve(),sep='\n')

Submission status: Accepted

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

    show_r = []
    show_c = []

    for r, row in enumerate(a):
        show_r.append(any(c == '#' for c in row))

    for c, col in enumerate(zip(*a)):
        show_c.append(any(c == '#' for c in col))

    ans = []
    for r in range(H):
        t = ''
        for c in range(W):
            if show_r[r] and show_c[c]:
                t += a[r][c]
        if t:
            ans.append(t)

    print(*ans, sep='\n')


if __name__ == '__main__':
    main()

# import sys
#
# sys.setrecursionlimit(10 ** 7)
#
# input = sys.stdin.readline
# rstrip()
# int(input())
# map(int, input().split())

Submission status: Accepted

Solution code:
icase=0
if icase==0:
    h,w=map(int,input().split())
    a=[]
    aid=["."]*w
    for i in range(h):
        ai=list(input())
        if ai!=aid:
            a.append(ai)
    b=[""]*len(a)
    for j in range(0,w):
        for i in range(len(a)):
            if a[i][j]=="#":
                break
        if a[i][j]=="#":
            for ii in range(len(a)):
                b[ii]=b[ii]+a[ii][j]
    for i in range(len(a)):
        print(b[i])

Submission status: Accepted

Solution code:
h,w = map(int, input().split())
aij = [list(input()) for i in range(h)]
aij1 = []
for r in aij:
    if r.count(".")==w:
        continue
    aij1.append(r)

h_r = len(aij1)
aij1_t = [[""]*h_r for i in range(w)]
for i in range(h_r):
    for j in range(w):
          aij1_t[j][i] = aij1[i][j]
          
ans_t = []
for c in aij1_t:
    if c.count(".") == h_r:
        continue
    ans_t.append(c)
    
w_r = len(ans_t)
ans = [[""]*w_r for i in range(h_r)]
for i in range(h_r):
    for j in range(w_r):
        ans[i][j] = ans_t[j][i]

for r in ans:
    print("".join(r))

Submission status: Accepted

Solution code:
import numpy as np
h,w=map(int,input().split())
maze=[]
for i in range(h):
  s=list(input())
  if s.count(".")==len(s):continue
  maze.append(s)
maze=np.array(maze).T
ans=[]
for i in range(10**5):
  try:q=maze[i]
  except:break  
  if all(x=="." for x in q):
      continue
  ans.append(q)
ans=((np.array(ans).T).tolist())
for i in ans:
  for j in i:
    print(j,end="")
  print()

Submission status: Accepted

Solution code:
def main():
  h,w = map(int, input().split())
  ans = []
  p = []
  for _ in range(h):
    s = input()
    if '#' in s:
      ans.append(list(s))
  for i in range(len(ans[0])):
    for j in range(len(ans)):
      if ans[j][i] == '#':
        break
      if j == len(ans)-1:
        p.append(i)
  p.sort(reverse=True)
  for i in p:
    for j in range(len(ans)):
      ans[j].pop(i)
  for i in range(len(ans)):
    s = ''.join(ans[i])
    print(s)

if __name__ == '__main__':
  main()

Submission status: Accepted

Solution code:
def main():
  h,w = map(int, input().split())
  ans = []
  p = []
  for _ in range(h):
    s = input()
    if '#' in s:
      ans.append(list(s))
  for i in range(len(ans[0])):
    for j in range(len(ans)):
      if ans[j][i] == '#':
        break
      if j == len(ans)-1:
        p.append(i)
  p.sort(reverse=True)
  for i in p:
    for j in range(len(ans)):
      ans[j].pop(i)
  for i in range(len(ans)):
    s = ''
    for j in range(len(ans[0])):
      s += ans[i][j]
    print(s)

if __name__ == '__main__':
  main()

Submission status: Accepted

Solution code:
h ,w = map(int,input().split())
p = []
cou = 0

for i in range(h):
    s = list(input())
    if s.count("#") != 0:
        p.append(s)
        cou += 1

q = {i for i in range(w)}

for i in range(cou):
    kar = set()
    for j in range(w):
        if p[i][j] == ".":
            kar.add(j)
    q = q & kar
    
s = {i for i in range(w)}
s = s - q
    
for i in range(cou):
    for j in s:
        print(p[i][j],end="")
    print()