#include <stdio.h>
#include <cmath>
#include <algorithm>
#include <cfloat>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <iostream>
#include <set>
#include <map>
#include <time.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


enum ROLL{
	L_90,
	R_90,
	V_180,
	NO_ROLL,
};


enum DIR{
	LEFT,
	RIGHT,
};

int H,W;
char first_table[40][40],ans_table[40][40];
int loc[40];

void printANS(ROLL roll){
	switch(roll){
	case L_90:
		for(int col = 0; col <= W-1; col++){
			for(int row = H-1; row >= 0; row--){
				printf("%c",ans_table[row][col]);
			}
			printf("\n");
		}
		break;
	case R_90:
		for(int col = W-1; col >= 0; col--){
			for(int row = 0; row <= H-1; row++){
				printf("%c",ans_table[row][col]);
			}
			printf("\n");
		}
		break;
	case V_180:
		for(int row = H-1; row >= 0; row--){
			for(int col = W-1; col >= 0; col--){
				printf("%c",ans_table[row][col]);
			}
			printf("\n");
		}
		break;
	case NO_ROLL:
		for(int row = 0; row < H; row++){
			for(int col = 0; col < W; col++){
				printf("%c",ans_table[row][col]);
			}
			printf("\n");
		}
		break;
	}
}

int main(){


	scanf("%d %d",&H,&W);

	int start_row,start_col,city_num = 0;

	for(int row = 0; row < H; row++){
		scanf("%s",first_table[row]);
		for(int col = 0; col < W; col++){
			if(first_table[row][col] == '@'){
				start_row = row;
				start_col = col;
			}else if(first_table[row][col] == '*'){
				city_num++;
			}
		}
	}

	ROLL roll;

	if(start_col == W-1 && start_row != H-1){
		roll = L_90;

		for(int row = 0; row <= H-1; row++){
			for(int col = W-1; col >= 0; col--){
				ans_table[W-1-col][row] = first_table[row][col];
			}
		}
		swap(H,W);

	}else if(start_col == 0 && start_row != 0){
		roll = R_90;

		for(int row = H-1; row >= 0; row--){
			for(int col = 0; col <= W-1; col++){
				ans_table[col][H-1-row] = first_table[row][col];
			}
		}
		swap(H,W);

	}else if((start_row%4 == 2) || (start_col != 0 && start_row == H-1)){
		roll = V_180;

		for(int row = H-1; row >= 0; row--){
			for(int col = W-1; col >= 0; col--){
				ans_table[H-1-row][W-1-col] = first_table[row][col];
			}
		}

	}else{
		roll = NO_ROLL;

		for(int row = 0; row <= H-1; row++){
			for(int col = 0; col <= W-1; col++){
				ans_table[row][col] = first_table[row][col];
			}
		}
	}

	for(int row = 0; row <= H-1; row++){
		for(int col = 0; col <= W-1; col++){
			if(ans_table[row][col] == '@'){
				start_row = row;
				start_col = col;
			}
		}
	}

	for(int row = 0; row < H; row++)loc[row] = -1;


	loc[start_row] = start_col;

	for(int col = start_col-1; col >= 0; col--){
		if(ans_table[start_row][col] == '*'){
			city_num--;
			if(city_num == 0){
				printANS(roll);
				return 0;
			}
		}else{
			ans_table[start_row][col] = '#';
		}
	}
	int pre_col = 0;

	DIR dir = RIGHT;
	for(int row = start_row+1; row <= H-2; row++){
		if(row%2 == 1){
			ans_table[row][pre_col] = '#';
		}else{
			if(dir == RIGHT){
				loc[row] = (W-3)/2;
				for(int col = 0; col <= (W-3)/2; col++){
					if(ans_table[row][col] == '*'){
						city_num--;
						if(city_num == 0){
							printANS(roll);
							return 0;
						}
					}else{
						ans_table[row][col] = '#';
					}
				}
				pre_col = (W-3)/2;
				dir = LEFT;
			}else{
				loc[row] = pre_col;
				for(int col = pre_col; col >= 0; col--){
					if(ans_table[row][col] == '*'){
						city_num--;
						if(city_num == 0){
							printANS(roll);
							return 0;
						}
					}else{
						ans_table[row][col] = '#';
					}
				}
				pre_col = 0;
				dir = RIGHT;
			}
		}
	}

	for(int col = 0; col <= W-1; col++){
		if(ans_table[H-1][col] == '*'){
			city_num--;
			if(city_num == 0){
				printANS(roll);
				return 0;
			}
		}else{
			ans_table[H-1][col] = '#';
		}
	}

	dir = LEFT;
	pre_col = W-1;
	for(int row = H-2; row >= start_row; row--){
		if(row%2 == 1){
			ans_table[row][pre_col] = '#';
		}else{
			if(dir == RIGHT){
				for(int col = pre_col; col <= W-1; col++){
					if(ans_table[row][col] == '*'){
						city_num--;
						if(city_num == 0){
							printANS(roll);
							return 0;
						}
					}else{
						ans_table[row][col] = '#';
					}
				}
				loc[row] = pre_col;
				pre_col = W-1;
				dir = LEFT;

			}else{ //dir == LEFT

				for(int col = pre_col; col >= loc[row]+2; col--){
					if(ans_table[row][col] == '*'){
						city_num--;
						if(city_num == 0){
							printANS(roll);
							return 0;
						}
					}else{
						ans_table[row][col] = '#';
					}
				}
				pre_col = loc[row]+2;
				loc[row] += 2;
				dir = RIGHT;
			}
		}
	}

	for(int row = start_row-1; row >= 1; row--){
		if(row%2 == 1){
			ans_table[row][pre_col] = '#';
		}else{
			if(dir == RIGHT){
				for(int col = pre_col; col <= W-1; col++){
					if(ans_table[row][col] == '*'){
						city_num--;
						if(city_num == 0){
							printANS(roll);
							return 0;
						}
					}else{
						ans_table[row][col] = '#';
					}
				}
				loc[row] = pre_col;
				pre_col = W-1;
				dir = LEFT;
			}else{
				for(int col = pre_col; col >= (W-3)/2+2; col--){
					if(ans_table[row][col] == '*'){
						city_num--;
						if(city_num == 0){
							printANS(roll);
							return 0;
						}
					}else{
						ans_table[row][col] = '#';
					}
				}
				loc[row] = (W-3)/2+2;
				pre_col = (W-3)/2+2;
				dir = RIGHT;
			}
		}
	}

	for(int col = W-1; col >= 0; col--){
		if(ans_table[0][col] == '*'){
			city_num--;
			if(city_num == 0){
				printANS(roll);
				return 0;
			}
		}else{
			ans_table[0][col] = '#';
		}
	}
	pre_col = 0;

	dir = RIGHT;
	for(int row = 1; row <= start_row-1 ;row++){
		if(row%2 == 1){
			ans_table[row][pre_col] = '#';
		}else{
			if(dir == RIGHT){

				for(int col = pre_col; col <= loc[row]-2; col++){
					if(ans_table[row][col] == '*'){
						city_num--;
						if(city_num == 0){
							printANS(roll);
							return 0;
						}
					}else{
						ans_table[row][col] = '#';
					}
				}
				pre_col = (W-3)/2;
				dir = LEFT;

			}else{ //dir == LEFT

				for(int col = pre_col; col >= 0; col--){
					if(ans_table[row][col] == '*'){
						city_num--;
						if(city_num == 0){
							printANS(roll);
							return 0;
						}
					}else{
						ans_table[row][col] = '#';
					}
				}
				pre_col = 0;
				dir = RIGHT;
			}
		}
	}

	return 0;
}

Measured memory_bytes: 3132