• + 1 comment

    Here is a really simple but hopefully readable solution:

    #!/bin/python3
    
    import re
    n, m = map(int, input().split())        
    matrix = [ input() for _ in range(n) ] 
    
    # a very Pythonic way to transpose and flatten a list of iterables - barely readable but you know what it's doing
    transpose = ''.join([c for ls in zip(*matrix) for c in ls])
    
    # Lookaheads and Lookbehinds let us use a simple 'sub' to replace every sandwiched cluster of symbols.
    print(re.sub(r"(?<=\w)[!@#$%& ]+(?=\w)", " ", transpose))