• + 0 comments

    import re

    Step 1: Input dimensions

    n, m = map(int, input().split())

    Step 2: Read the matrix

    matrix = [input() for _ in range(n)]

    Step 3: Transpose the matrix and build the decoded string

    decoded = ''.join([matrix[i][j] for j in range(m) for i in range(n)])

    Step 4: Use regex to replace symbols/spaces between alphanumerics with space

    print(re.sub(r'(?<=[A-Za-z0-9])[^A-Za-z0-9]+(?=[A-Za-z0-9])', ' ', decoded))