The map of Berland is a rectangle of the size n × m, which consists of cells of size 1 × 1. Each cell is either land or water. The map is surrounded by the ocean. 

Lakes are the maximal regions of water cells, connected by sides, which are not connected with the ocean. Formally, lake is a set of water cells, such that it's possible to get from any cell of the set to any other without leaving the set and moving only to cells adjacent by the side, none of them is located on the border of the rectangle, and it's impossible to add one more water cell to the set such that it will be connected with any other cell.

You task is to fill up with the earth the minimum number of water cells so that there will be exactly k lakes in Berland. Note that the initial number of lakes on the map is not less than k. 


-----Input-----

The first line of the input contains three integers n, m and k (1 ≤ n, m ≤ 50, 0 ≤ k ≤ 50) — the sizes of the map and the number of lakes which should be left on the map.

The next n lines contain m characters each — the description of the map. Each of the characters is either '.' (it means that the corresponding cell is water) or '*' (it means that the corresponding cell is land).

It is guaranteed that the map contain at least k lakes.


-----Output-----

In the first line print the minimum number of cells which should be transformed from water to land. 

In the next n lines print m symbols — the map after the changes. The format must strictly follow the format of the map in the input data (there is no need to print the size of the map). If there are several answers, print any of them. 

It is guaranteed that the answer exists on the given data.


-----Examples-----
Input
5 4 1
****
*..*
****
**.*
..**

Output
1
****
*..*
****
****
..**

Input
3 3 0
***
*.*
***

Output
1
***
***
***



-----Note-----

In the first example there are only two lakes — the first consists of the cells (2, 2) and (2, 3), the second consists of the cell (4, 3). It is profitable to cover the second lake because it is smaller. Pay attention that the area of water in the lower left corner is not a lake because this area share a border with the ocean.

Difficulty: interview

Starter code:

Solutions:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time

sys.setrecursionlimit(10**7)
inf = 10**10
mod = 10**9 + 7

def f():
    n,m,k = list(map(int, input().split()))
    a = [[inf]*(m+2)]
    g = []
    for _ in range(n):
        a.append([inf] + [_ for _ in input()] + [inf])
        g.append([False]*m)
    a.append([[inf]*(m+2)])

    c = 0
    for i in range(1,n+1):
        if a[i][1] == '.':
            a[i][1] = -1
        if a[i][-2] == '.':
            a[i][-2] = -1
    for j in range(1,m+1):
        if a[1][j] == '.':
            a[1][j] = -1
        if a[-2][j] == '.':
            a[-2][j] = -1

    def ff(n1,n2):
        for i in range(1,n+1):
            for j in range(1,m+1):
                if a[i][j] == n1:
                    a[i][j] = n2
    ff('*', inf)

    for i in range(1,n+1):
        for j in range(1,m+1):
            if a[i][j] == '.':
                mc = [inf]
                if a[i-1][j] != '.': mc.append(a[i-1][j])
                if a[i+1][j] != '.': mc.append(a[i+1][j])
                if a[i][j+1] != '.': mc.append(a[i][j+1])
                if a[i][j-1] != '.': mc.append(a[i][j-1])
                mm = min(mc)
                if mm < inf:
                    a[i][j] = mm
                    for t in [_ for _ in mc if _ < inf and _ != mm]:
                        ff(t,mm)
                else:
                    a[i][j] = c
                    c += 1
    cnt = [0] * c
    for i in range(1,n+1):
        for j in range(1,m+1):
            if -1 < a[i][j] < c:
                cnt[a[i][j]] += 1
    cnt2 = [_ for _ in cnt if _ > 0]
    r = 0
    for _i in range(len(cnt2) - k):
        cnt2 = [_ for _ in cnt if _ > 0]
        mm = min(cnt2)
        ind = cnt.index(mm)
        cnt[ind] = 0
        r += mm
        for i in range(1,n+1):
            for j in range(1,m+1):
                if a[i][j] == ind:
                    a[i][j] = '*'

    print(r)

    for i in range(1,n+1):
        s = ''
        for j in range(1,m+1):
            c = a[i][j]
            if c == '*':
                s += c
            elif c == inf:
                s += '*'
            else:
                s += '.'
        print(s)

f()


#!/usr/bin/env python3

from collections import deque


def main():
    DELTAS = (
        (-1, 0),
        (0, 1),
        (1, 0),
        (0, -1),
    )

    try:
        while True:
            n, m, k = list(map(int, input().split()))
            grid = [list(input()) for i in range(n)]
            lakes = [ ]
            q = deque()
            for i in range(n):
                for j in (0, m - 1):
                    if grid[i][j] == '.':
                        grid[i][j] = '?'
                        q.append((i, j))
                        while q:
                            y, x = q[0]
                            q.popleft()
                            for dy, dx in DELTAS:
                                ny = y + dy
                                nx = x + dx
                                if 0 <= ny < n and 0 <= nx < m and grid[ny][nx] == '.':
                                    grid[ny][nx] = '?'
                                    q.append((ny, nx))
            for i in (0, n - 1):
                for j in range(m):
                    if grid[i][j] == '.':
                        grid[i][j] = '?'
                        q.append((i, j))
                        while q:
                            y, x = q[0]
                            q.popleft()
                            for dy, dx in DELTAS:
                                ny = y + dy
                                nx = x + dx
                                if 0 <= ny < n and 0 <= nx < m and grid[ny][nx] == '.':
                                    grid[ny][nx] = '?'
                                    q.append((ny, nx))
            for i in range(n):
                for j in range(m):
                    if grid[i][j] == '.':
                        grid[i][j] = '?'
                        q.append((i, j))
                        area = 1
                        while q:
                            y, x = q[0]
                            q.popleft()
                            for dy, dx in DELTAS:
                                ny = y + dy
                                nx = x + dx
                                if 0 <= ny < n and 0 <= nx < m and grid[ny][nx] == '.':
                                    grid[ny][nx] = '?'
                                    q.append((ny, nx))
                                    area += 1

                        lakes.append((area, i, j))

            lakes.sort(reverse=True)
            print(sum(x[0] for x in lakes[k:]))
            for _, i, j in lakes[k:]:
                grid[i][j] = '*'
                q.append((i, j))
                while q:
                    y, x = q[0]
                    q.popleft()
                    for dy, dx in DELTAS:
                        ny = y + dy
                        nx = x + dx
                        if 0 <= ny < n and 0 <= nx < m and grid[ny][nx] == '?':
                            grid[ny][nx] = '*'
                            q.append((ny, nx))

            print('\n'.join("".join(ls).replace('?', '.') for ls in grid))

    except EOFError:
        pass


main()


from pprint import pprint
import itertools
import collections

n,m,k = [int(s) for s in input().split()]

g = [[0]*m for _ in range(n)]

for i in range(n):
    r = input()
    for (j,c) in enumerate(r):
        if c == '.':
            g[i][j] = 1

sidecomps = set()
curlab = 1
labels = [[0]*m for _ in range(n)]
for i in range(n):
    for j in range(m):
        if g[i][j] != 1 or labels[i][j] > 0:
            continue
        #print(i,j)
        labels[i][j] = curlab
        stack = [(i,j)]
        if i == 0 or j == 0 or i == n-1 or j == m-1:
            sidecomps.add(curlab)

        while stack:
            (i1, j1) = stack.pop()
            #print(i1,j1)
            for (i2,j2) in [(i1-1,j1),(i1+1,j1),(i1,j1-1),(i1,j1+1)]:
                if (0 <= i2 and i2 < n and 0 <= j2 and j2 < m\
                        and g[i2][j2] == 1 and labels[i2][j2] == 0):
                    labels[i2][j2] = curlab
                    stack.append((i2,j2))
                    if i2 == 0 or j2 == 0 or i2 == n-1 or j2 == m-1:
                        sidecomps.add(curlab)
        curlab += 1

lakes = collections.defaultdict(list)
for i,j in itertools.product(list(range(n)), list(range(m))):
    lab = labels[i][j]
    if lab > 0 and lab not in sidecomps:
        lakes[lab].append((i,j))
#pprint(dict(lakes))

lakesizes = sorted((len(lakes[lab]),lab) for lab in lakes)
num_remove = len(lakesizes)-k
ans = 0
for _,lab in lakesizes[:num_remove]:
    for (i,j) in lakes[lab]:
        g[i][j] = 0
        ans += 1

print(ans)
for i in range(n):
    print(''.join('.' if g[i][j] else '*' for j in range(m)))


def ex(i, j, n, m):
	if i < n and i >= 0 and j < m and j >= 0:
		return True
	else:
		return False
n, m, k = map(int, input().split())
A = []
for i in range(n):
	A.append(list(input()))
B = [[False for i in range(m)] for j in range(n)]
L = []
for i in range(n):
	for j in range(m):
		if not B[i][j] and A[i][j] == '.':
			sides = False
			Oz = [[i, j]]
			count = 1
			Q = [[i, j]]
			B[i][j] = True
			while len(Q) > 0:
				x, y = Q[0][0], Q[0][1]
				if x == 0 or x == n - 1 or y == 0 or y == m - 1:
					sides = True
				if  ex(x - 1, y, n, m) and not B[x - 1][y] and A[x - 1][y] == '.':
					Q.append([x - 1, y])
					B[x - 1][y] = True
					count += 1
					Oz.append([x - 1, y])
				if  ex(x + 1, y, n, m) and not B[x + 1][y] and A[x + 1][y] == '.':
					Q.append([x + 1, y])
					B[x + 1][y] = True
					count += 1
					Oz.append([x + 1, y])
				if  ex(x, y - 1, n , m) and not B[x][y - 1] and A[x][y - 1] == '.':
					Q.append([x, y - 1])
					B[x][y - 1] = True
					count += 1
					Oz.append([x, y - 1])
				if  ex(x, y + 1, n, m) and not B[x][y + 1] and A[x][y + 1] == '.':
					Q.append([x, y + 1])
					B[x][y + 1] = True
					count += 1
					Oz.append([x, y + 1])
				Q.pop(0)
			if not sides:
				L.append([count, Oz])
L.sort()
s = len(L)
ans = 0
for i in range(s - k):
	ans += L[i][0]
	for j in L[i][1]:
		A[j[0]][j[1]] = '*'
print(ans)
for i in range(n):
	for j in range(m):
		print(A[i][j], end='')
	print()


#!/usr/bin/env python3

from collections import Counter, defaultdict

n, m, k = [int(x) for x in input().split()]

mp = [["."]*(m+2)] + [list("." + input().strip() + ".") for _ in range(n)] + [["."]*(m+2)]
uf = {}
for i in range(n+2):
  for j in range(m+2):
    uf[(i, j)] = (i, j)

def find(u):
  if u != uf[u]:
    uf[u] = find(uf[u])
  return uf[u]

def union(u, v):
  pu = find(u)
  pv = find(v)
  if pu != pv:
    uf[pv] = pu

for i in range(n+2):
  for j in range(m+2):
    if mp[i][j] == '*':
      continue
    if i <= n and mp[i+1][j] == '.':
      union((i, j), (i+1, j))
    if j <= m and mp[i][j+1] == '.':
      union((i, j), (i, j+1))

d = defaultdict(int)
f0 = find((0, 0))
for i in range(n+2):
  for j in range(m+2):
    if mp[i][j] == '*':
      continue
    f = find((i, j))
    if f != f0:
      d[f] += 1
l = sorted([(v, k) for k, v in list(d.items())])
ll = len(l)
ps = set([u for _, u in l[:ll-k]])
r = 0
for i in range(1, n+1):
  for j in range(1, m+1):
    if mp[i][j] == '.' and find((i, j)) in ps:
      mp[i][j] = '*'
      r += 1

print(r)
print(("\n".join(
  "".join(m[1:-1])
  for m in mp[1:-1]
)))


import sys, math
def bfs(a):
    Q = [a]
    ctr = 0
    used = [[0] * m for i in range(n)]
    while ctr < len(Q):
        h = Q[ctr]
        ctr += 1
        z[h[0]][h[1]] = 'o'
        if h[0] - 1 >= 0 and z[h[0] - 1][h[1]] == '.' and not used[h[0] - 1][h[1]]:
            Q.append([h[0] - 1, h[1]])
            used[h[0] - 1][h[1]] = 1
        if h[0] + 1 < n and z[h[0] + 1][h[1]] == '.' and not used[h[0] + 1][h[1]]:
            Q.append([h[0] + 1, h[1]])
            used[h[0] + 1][h[1]] = 1
        if h[1] - 1 >= 0 and z[h[0]][h[1] - 1] == '.' and not used[h[0]][h[1] - 1]:
            Q.append([h[0], h[1] - 1])
            used[h[0]][h[1] - 1] = 1
        if h[1] + 1 < m and z[h[0]][h[1] + 1] == '.' and not used[h[0]][h[1] + 1]:
            Q.append([h[0], h[1] + 1])
            used[h[0]][h[1] + 1] = 1

def bfs1(a, b):
    Q = [a]
    ctr = 0
    while ctr < len(Q):
        h = Q[ctr]
        ctr += 1
        if z[h[0]][h[1]] == b:
            continue
        z[h[0]][h[1]] = b
        if h[0] - 1 >= 0 and z[h[0] - 1][h[1]] == '.':
            Q.append([h[0] - 1, h[1]])
        if h[0] + 1 < n and z[h[0] + 1][h[1]] == '.':
            Q.append([h[0] + 1, h[1]])
        if h[1] - 1 >= 0 and z[h[0]][h[1] - 1] == '.':
            Q.append([h[0], h[1] - 1])
        if h[1] + 1 < m and z[h[0]][h[1] + 1] == '.':
            Q.append([h[0], h[1] + 1])
    ctr = 0
    for i in range(n):
        for j in range(m):
            if z[i][j] == b:
                ctr += 1
    return ctr
        
n, m, k = map(int, input().split())
z = [list(input()) for i in range(n)]
for i in range(n):
    if z[i][0] == '.':
        bfs([i, 0])
    if z[i][m - 1] == '.':
        bfs([i, m - 1])
for i in range(m):
    if z[0][i] == '.':
        bfs([0, i])
    if z[n - 1][i] == '.':
        bfs([n - 1, i])
ctr = 0
z1 = []
for i in range(n):
    for j in range(m):
        if z[i][j] == '.':
            z1.append([bfs1([i, j], ctr), ctr])
            ctr += 1
z1.sort()
dif = ctr - k
ans = 0
for i in range(dif):
    u = z1[i][1]
    ans += z1[i][0]
    for j in range(n):
        for k in range(m):
            if z[j][k] == u:
                z[j][k] = '*'
for i in range(n):
    for j in range(m):
        if z[i][j] != '*' and z[i][j] != '.':
            z[i][j] = '.'
print(ans)
for i in range(n):
    print(*z[i], sep = '')
    
            
            





import sys
import copy
sys.setrecursionlimit(10000000)

h, w, k = list(map(int,input().split()))
ground = [list(input()) for j in range(h)]
ground2 = copy.deepcopy(ground)

dx = [1,0,-1,0]
dy = [0,1,0,-1]
cnt = 0

def dfs(x,y,count):
    ground[y][x] = str(count)
    #print(y,x)
    for k in range(4):
        if 0 <= x + dx[k] < w and 0 <= y + dy[k] < h:
            if ground[y+dy[k]][x+dx[k]] == ".":
                dfs(x+dx[k], y+dy[k], count)

for j in range(h):
    for i in range(w):
        if ground[j][i] == ".":
            dfs(i, j, cnt)
            cnt += 1

arr = [[0, i] for i in range(cnt)]

for num in range(cnt):
    f = True
    for j in range(h):
        for i in range(w):
            #print(num,j,i,ground[j][i])
            if ground[j][i] == str(num):
                arr[num][0] += 1
                if i == 0 or i == w - 1 or j == 0 or j == h - 1:
                    f = False
                    arr[num][0] += 10**10
                    break
                    #print(num,j,i)
            if not f:
                break
        if not f:
            break

arr.sort()

for j in range(len(arr)):
    if arr[j][0] > 10**10:
        cnt -= 1

num_lake = cnt
ans = 0
"""
print(cnt,arr)
print(k)
"""
if cnt == k:
    print(ans)
    for j in range(h):
        print("".join(ground2[j][:]))
    return

for num in range(cnt):
    groundbef = ground[:][:]
    f = True
    cells = 0
    for j in range(h):
        for i in range(w):
            if ground[j][i] == str(arr[num][1]):
                if i < 0 or i > w - 1 or j < 0 or j > h - 1:
                    f = False
                    print("F", i,j)
                    break
                ground[j][i] = "*"
                cells += 1

    if not f:
        ground = groundbef[:][:]
        num_lake -= 1

    else:
        num_lake -= 1
        ans += cells

    if num_lake == k :
        break

for j in range(h):
    for i in range(w):
        if ground[j][i] == "*":
            continue
        else:
            ground[j][i] = "."

print(ans)
for j in range(h):
    print("".join(ground[j][:]))



"""
import sys
sys.setrecursionlimit(10000000)

h, w, k = map(int,input().split())
ground = [list(input()) for j in range(h)]

dx = [1,0,-1,0]
dy = [0,1,0,-1]
cnt = 0

def dfs(x,y,count):
    ground[y][x] = str(count)
    #print(y,x)
    for k in range(4):
        if 0 <= x + dx[k] < w and 0 <= y + dy[k] < h:
            if ground[y+dy[k]][x+dx[k]] == ".":
                dfs(x+dx[k], y+dy[k], count)

for j in range(h):
    for i in range(w):
        if ground[j][i] == ".":
            #print(cnt, i, j)
            dfs(i, j, cnt)
            cnt += 1

arr = [[0, i] for i in range(cnt)]
for num in range(cnt):
    for j in range(h):
        for i in range(w):
            if ground[j][i] == str(num):
                arr[num][0] += 1

num_lake = cnt
arr.sort()
ans = 0

for num in range(cnt):
    groundbef = ground[:][:]
    f = True
    cells = 0
    for j in range(h):
        for i in range(w):
            if ground[j][i] == str(arr[num][1]):
                if i == 0 or i == w - 1 or j == 0 or j == h - 1:
                    f = False
                    break
                ground[j][i] = "*"
                cells += 1

            if not f:
                break

        if not f:
            break

    if not f:
        ground = groundbef[:][:]
        num_lake -= 1

    else:
        num_lake -= 1
        ans += cells

    if num_lake == k + 1:
        break

for j in range(h):
    for i in range(w):
        if ground[j][i] == "*":
            continue
        else:
            ground[j][i] = "."

print(ans)
for j in range(h):
    print("".join(ground[j][:]))
"""













"""
#c
n, m = map(int,input().split())
a = list(map(int,input().split()))

"""


"""
l = int(input())
s = input()

left = 0
ans = [0] * 2
current = ""

for i in range(l):
    if s[i] == "_":
        if current != "":
            if left == 0:
                ans[0] = max(ans[0], len(current))
            else:
                ans[1] += 1
                #print(1, current)

        current = ""
        continue

    if s[i] == "(":
        ans[0] = max(ans[0], len(current))
        left += 1
        current = ""

    elif s[i] == ")":
        if current != "":
            ans[1] += 1
            #print(1, current)
        left = 0
        current = ""
    else:
        current += s[i]
    #print(current)

if current != "":
    ans[0] = max(ans[0], len(current))

print(ans[0], ans[1])
"""


"""
x = list(map(int,input().split()))
ans = 10 ** 10
for i in range(1, 101):
    dis = 0
    for j in range(3):
        dis += abs(x[j] - i)
    ans = min(ans, dis)
print(ans)
"""


import sys
import copy
sys.setrecursionlimit(10000000)

h, w, k = list(map(int,input().split()))
ground = [list(input()) for j in range(h)]
ground2 = copy.deepcopy(ground)

dx = [1,0,-1,0]
dy = [0,1,0,-1]
cnt = 0

def dfs(x,y,count):
    ground[y][x] = str(count)
    #print(y,x)
    for k in range(4):
        if 0 <= x + dx[k] < w and 0 <= y + dy[k] < h:
            if ground[y+dy[k]][x+dx[k]] == ".":
                dfs(x+dx[k], y+dy[k], count)

for j in range(h):
    for i in range(w):
        if ground[j][i] == ".":
            dfs(i, j, cnt)
            cnt += 1

arr = [[0, i] for i in range(cnt)]

for num in range(cnt):
    f = True
    for j in range(h):
        for i in range(w):
            #print(num,j,i,ground[j][i])
            if ground[j][i] == str(num):
                arr[num][0] += 1
                if i == 0 or i == w - 1 or j == 0 or j == h - 1:
                    f = False
                    arr[num][0] += 10**10
                    break
                    #print(num,j,i)
            if not f:
                break
        if not f:
            break

arr.sort()

for j in range(len(arr)):
    if arr[j][0] > 10**10:
        cnt -= 1

num_lake = cnt
ans = 0
"""
print(cnt,arr)
print(k)
"""
if cnt == k:
    print(ans)
    for j in range(h):
        print("".join(ground2[j][:]))
    return

for num in range(cnt):
    groundbef = ground[:][:]
    f = True
    cells = 0
    for j in range(h):
        for i in range(w):
            if ground[j][i] == str(arr[num][1]):
                if i < 0 or i > w - 1 or j < 0 or j > h - 1:
                    f = False
                    print("F", i,j)
                    break
                ground[j][i] = "*"
                cells += 1

    if not f:
        ground = groundbef[:][:]
        num_lake -= 1

    else:
        num_lake -= 1
        ans += cells

    if num_lake == k :
        break

for j in range(h):
    for i in range(w):
        if ground[j][i] == "*":
            continue
        else:
            ground[j][i] = "."

print(ans)
for j in range(h):
    print("".join(ground[j][:]))


import sys
sys.setrecursionlimit(100000)

def find(x,y,tag):
    if mplake[x][y] == tag or mp[x][y] == "*":
        return
    mplake[x][y] = tag
    lakes[tag] += 1
    for i in [(1,0),(-1,0),(0,1),(0,-1)]:
        if 0 <= x+i[0] and x+i[0] < n and 0 <= y + i[1] and y+i[1] < m:
            find(x+i[0],y+i[1],tag)

def fill(x,y,tag):
    nonlocal cnt
    if mplake[x][y] == tag:
        mplake[x][y] = -1
        mp[x][y] = "*"
        cnt += 1
        for i in [(1,0),(-1,0),(0,1),(0,-1)]:
            if 0 <= x+i[0] and x+i[0] < n and 0 <= y + i[1] and y+i[1] < m:
                fill(x+i[0],y+i[1],tag)

n, m, k = [int(i) for i in input().split()]
mp = []
mplake = []
for i in range(n):
    mp.append(list(input()))
    mplake.append([-1 for i in range(m)])

start = [(0,0)]
lakes = [0]
cnt = 0

for i in range(n):
    find(i,0,0)
    find(i,m-1,0)
for i in range(m):
    find(0,i,0)
    find(n-1,i,0)

for i in range(1,n-1):
    for j in range(1,m-1):
        if mp[i][j] == "." and mplake[i][j] == -1:
            start.append((i,j))
            lakes.append(0)
            find(i,j,len(lakes)-1)

r = sorted([(lakes[i],start[i]) for i in range(1,len(lakes))])

for i in range(len(r)-k):
    fill(r[i][1][0],r[i][1][1],mplake[r[i][1][0]][r[i][1][1]])

print(cnt)
for i in range(n):
    print("".join(mp[i]))



def make_set(a):
    parent[a] = a
    size[a] = 1
    
      
def find_set(a):
        if a == parent[a]:
            return a
        else:
            parent[a] = find_set(parent[a])
        return parent[a]
    

def union_sets(a, b):
    a = find_set(a)
    b = find_set(b)
    if a != b:
        if size[a] < size[b]:
            a, b = b, a
        parent[b] = a
        size[a] += size[b]
        
        
        
        
def is_valid(a):
    i = a[0]
    j = a[1]
    if 0<=i<=n-1 and 0<=j<=m-1:
        if A[i][j] == '.':
            if (i,j) not in parent:
                return False
            else:
                return True 
        else:
            return True
    else:
        return True
        
        
        
        
        
parent = dict()
size = dict()
n,m,ks = map(int, input().split())
A = [0] * n
for i in range(n):
    A[i] = list(input())

for i in range(n):
    for j in range(m):
        if i != 0 and j != 0 and i != n - 1 and j != m - 1:
        
            if A[i][j] == '.':
                if is_valid((i-1,j)) and is_valid((i,j-1)):
                    
                    make_set((i,j))
                    if (i-1,j) in parent:
                        union_sets((i-1,j), (i,j))
                    if (i,j-1) in parent:
                        union_sets((i,j-1), (i,j))
                else:
                    if A[i][j] =='.':
                        dels = []
                        if (i-1,j) in parent:
                            a1 = find_set((i-1,j))
                           
                            for k in parent:
                                if find_set(k) == a1:
                                    
                                    dels.append(k)
                      
                        for f in dels:
                            
                            parent.pop(f)
                        dels = []
                        if (i,j-1) in parent:
                            
                            a1 = find_set((i,j-1))
                            
                            for k in parent:
                                if find_set(k) == a1:
                                    dels.append(k)
                        
                        for f in dels:
                            
                            parent.pop(f)            
                        
        else:
            if A[i][j] =='.':
                dels = []
                if (i-1,j) in parent:
                    a1 = find_set((i-1,j))
                   
                    for k in parent:
                        if find_set(k) == a1:
                            
                            dels.append(k)
              
                for f in dels:
                    
                    parent.pop(f)
                dels = []
                if (i,j-1) in parent:
                    
                    a1 = find_set((i,j-1))
                    
                    for k in parent:
                        if find_set(k) == a1:
                            dels.append(k)
                
                for f in dels:
                    
                    parent.pop(f)    

ans = 0
ass = 0
ans2 = []
for j in parent:
    a1 = find_set(j)
    if a1 not in ans2:
        ans2.append(a1)
        ans +=1
for j in range(ans-ks):
    mins = float('infinity')
    numer = 0
    new_sizes = dict()
    for t in parent:
        a1 = find_set(t)
        if a1 in new_sizes:
            new_sizes[a1] +=1
        else:
            new_sizes[a1] = 1
    
    for t in new_sizes:
        if new_sizes[t] < mins:
            mins = new_sizes[t]
            numer = t
    ass += mins 
    dels = []
    for t in parent:
        
        if find_set(t) == numer:
            dels.append(t)
            
            A[t[0]][t[1]] = '*'      
    for t in dels:
        parent.pop(t)
print(ass)
for j in range(n):
    print(''.join(A[j]))

x,y,k=list(map(int,input().split()))
a=[input().strip() for i in range(x)]
m=[[0 if j=='*' else 1 for j in i] for i in a]
m1=[[0 if j=='*' else 1 for j in i] for i in a]
lk=[]

def bfs(m,i,j):
	q=set()
	s=0
	f=1
	q.add((i,j))
	while q:
		ax,ay=q.pop()
		m[ax][ay]=0
		s+=1
		if ax==0 or ay==0 or ax==x-1 or ay==y-1:f=0
		if ax<x-1 and m[ax+1][ay]:
			q.add((ax+1,ay))
		if ax>0 and m[ax-1][ay]:
			q.add((ax-1,ay))
		if ay<y-1 and m[ax][ay+1]:
			q.add((ax,ay+1))
		if ay>0 and m[ax][ay-1]:
			q.add((ax,ay-1))
	return f,s

for i in range(x):
	for j in range(y):
		if m1[i][j]:
			f,s=bfs(m1,i,j)
			if f:
				lk.append((s,i,j))
lk.sort()
k1=len(lk)
sal=0
for e in range(k1-k):
	s,i,j=lk[e]
	sal+=s
	bfs(m,i,j)
print(sal)
for i in range(x):
	print(''.join(['.' if j else '*' for j in m[i]]))


def find_neigh(t,x,y):
    return [(i,j) for (i,j) in [(x-1,y),(x+1,y),(x,y-1),(x,y+1),(x,y+1)]
            if t[i][j]=='.']

def lake(table):
    return [(i,j) for i in range(0,len(table)-1)
                  for j in range(0, len(table[i])-1)
            if table[i][j] == '.']

stack=set()
def the_lake(table,x,y):
    queue=[(x,y)]
    counted=set()
    counted.add((x,y))
    stack.add((x,y))
    while queue:
        start=queue.pop()
        for (i,j) in find_neigh(table,*start):
            if (i,j) in counted:
                continue
            stack.add((i,j))
            queue.append((i,j))
            counted.add((i,j))
    for (i,j) in counted:
        if table[i+1][j]=='0' or table[i-1][j]=='0':
            return
        if table[i][j+1]=='0' or table[i][j-1]=='0':
            return
    return counted

def island(table,n,m,k):
    lakes=[]
    count=0
    for (i,j) in lake(table):
        if (i,j) in stack:
            continue
        tlake=the_lake(table,i,j)
        if tlake==None:
            continue
        lakes.append(tlake)
    lakes=sorted(lakes,key=len)
    for p in range(0,len(lakes)-k):
        for i,j in lakes[p]:
            row=list(table[i])
            row[j]='*'
            count+=1
            table[i]=''.join(row)
    print(count)
    for i in range(1,n+1):
        print(table[i][1:m+1])
    
n,m,k=input().split(' ')
carta=[]
for i in range(int(n)):
    row='0'+input()+'0'
    carta.append(row)
carta=['0'*(int(m)+2)]+carta+['0'*(int(m)+2)]
island(carta,int(n),int(m),int(k))

def mark(z):
  while z: z = z[0]
  return z

def mark2(z):
  while z and type(z) == list: z = z[0]
  return z
  
__debug = None  
  
n, m, kk = map(int, input().split())
data = [list(input()) for i in range(n)]

ocean = []
earth = []
work = [[] for i in range(m*n)]

for k in range(m):
  if data[0][k] == '.':
    work[m*0 + k].append(ocean)
  if data[n-1][k] == '.':
    work[m*(n-1) + k].append(ocean)
for i in range(n):
  if data[i][0] == '.':
    work[m*i + 0].append(ocean)
  if data[i][m-1] == '.':
    work[m*i + m-1].append(ocean)

for i in range(n):
  for k in range(m):
    if data[i][k] == '.':
      z = mark(work[m*i + k])
      if i and data[i-1][k] == '.':
        zi = mark(work[m*(i-1) + k])
        if z is not zi:
          if z is ocean:
            zi.append(z)
          else:  
            z.append(zi)
            z = zi
      if k and data[i][k-1] == '.':
        zk = mark(work[m*i + k-1])
        if z is not zk:
          if z is ocean:
            zk.append(z)
          else:  
            z.append(zk)

isl = {}
no = 1    

for i in range(n):
  for k in range(m):
    if data[i][k] == '.':
      if __debug: print(i, end=" ")
      if __debug: print(k, end=": ")
      z = mark2(work[m*i + k])
      if z:
        isl[z].append((i,k)) 
        if __debug: print(z, end=", old")
      elif z is not ocean:
        z.append(no)
        if __debug: print(no, end=", new")
        isl[no] = [(i,k)] 
        no += 1
      if __debug: print()

if __debug: print(isl)
isl_s = list(isl.values())
if __debug: print(isl_s)
isl_s.sort(key = len)
if __debug: print(isl_s)
delta = len(isl_s) - kk
if __debug: print(len(isl_s), end=" ")
if __debug: print(kk)
for t in range(delta):
  for i, k in isl_s[t]:
    data[i][k] = '*'
    
print(sum(map(len, isl_s[:delta])))
for i in range(n):
  print("".join(data[i]))

import sys
sys.setrecursionlimit(100000)

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

arr = []
for i in range(n):
    krr = []
    p = input()
    for x in p:
        if(x=='*'):
            krr.append(0)
        else:
            krr.append(-1)
    arr.append(krr)

lakes = [-1]
islakes = [-1]
lakesp = [[-1,-1]]

def go(x,y,c):
    if(x<0)or(x>=n)or(y<0)or(y>=m):
        return
    if(arr[x][y]==0):
        return
    if(arr[x][y]!=-1):
        return
    arr[x][y] = c
    if(x==0)or(x==n-1)or(y==0)or(y==m-1):
        islakes[c] = 0
    lakes[c] += 1
    go(x-1,y,c)
    go(x,y-1,c)
    go(x+1,y,c)
    go(x,y+1,c)


color = 0
for x in range(n):
    for y in range(m):
        if(arr[x][y]==-1):
            color += 1
            lakes.append(0)
            islakes.append(1)
            go(x,y,color)
            lakesp.append([x,y])

def re(x,y):
    if(x<0)or(x>=n)or(y<0)or(y>=m):
        return
    if(arr[x][y]==0):
        return
    arr[x][y] = 0
    re(x-1,y)
    re(x,y-1)
    re(x+1,y)
    re(x,y+1)

def min():
    min = 1000000000
    mini = 0
    for i in range(1,color+1):
        if(islakes[i]):
            if(min>lakes[i]):
                min = lakes[i]
                mini = i
    return mini

cnt = 0
nn = 0
for i in range(1,color+1):
    if(islakes[i]==0):
        continue
    nn += 1

p = nn-k
for i in range(1,color+1):
    if(p==0):
        break
    j = min()
    cnt += lakes[j]
    lakes[j] = 1000000000
    re(lakesp[j][0],lakesp[j][1])
    p -= 1

print(cnt)
for x in arr:
    s = ''
    for y in x:
        if(y==0):
            s += '*'
        else:
            s += '.'
    print(s)

import sys

sys.setrecursionlimit(5000)

n, m, k = [int(x) for x in input().split()]

land = []
cc = 0
visit = [[0 for _ in range(m)] for _ in range(n)]
for i in range(n):
    land.append(list(input()))
edge = False

def DFS(i, j, c):
    nonlocal land, n, m, k, cc, visit, edge
    if i <= 0 or j <= 0 or i >= n-1 or j >= m-1:
        edge = True
        return 0
    if visit[i][j] != 0: return 0
    if land[i][j] == '*': return 0
    visit[i][j] = 1
    cc += 1
    if land[i + 1][j] == '.': DFS(i + 1, j, c + 1)
    if land[i - 1][j] == '.': DFS(i - 1, j, c + 1)
    if land[i][j + 1] == '.': DFS(i, j + 1, c + 1)
    if land[i][j - 1] == '.': DFS(i, j - 1, c + 1)

def DFS2(i, j):
    nonlocal land, n, m, k, cc
    if i < 0 or j < 0 or i >= n or j >= m: return 0
    if land[i][j] != '.': return 0
    land[i][j] = '*'
    DFS2(i+1,j)
    DFS2(i - 1, j)
    DFS2(i, j+1)
    DFS2(i, j-1)
    return 0


ccc = []
pos = []

for i in range(n):
    for j in range(m):
        cc = 0
        edge = False
        if visit[i][j] == 0:
            DFS(i,j,0)
            if edge is True: continue
            if cc > 0:
                ccc.append([cc, i, j])


ccc.sort()

red =len(ccc) - k

if red == 0:
    print(0)
    for i in range(n):
        print(''.join(land[i]))
    return

red_list = ccc[0:red]

p = [x[0] for x in red_list]
print(sum(p))

for i in range(len(red_list)):
    DFS2(red_list[i][1], red_list[i][2])

for i in range(n):
    print(''.join(land[i]))

def mark(z):
  while z: z = z[0]
  return z

def mark2(z):
  while z and type(z) == list: z = z[0]
  return z
  
__debug = None  
  
n, m, kk = map(int, input().split())
data = [list(input()) for i in range(n)]

ocean = []
earth = []
work = [[] for i in range(m*n)]

for k in range(m):
  if data[0][k] == '.':
    work[m*0 + k].append(ocean)
  if data[n-1][k] == '.':
    work[m*(n-1) + k].append(ocean)
for i in range(1,n-1):
  if data[i][0] == '.':
    work[m*i + 0].append(ocean)
  if data[i][m-1] == '.':
    work[m*i + m-1].append(ocean)

for i in range(1,n):
  for k in range(1,m):
    if data[i][k] == '.':
      z = mark(work[m*i + k])
      if i and data[i-1][k] == '.':
        zi = mark(work[m*(i-1) + k])
        if z is not zi:
          if z is ocean:
            zi.append(z)
          else:  
            z.append(zi)
            z = zi
      if k and data[i][k-1] == '.':
        zk = mark(work[m*i + k-1])
        if z is not zk:
          if z is ocean:
            zk.append(z)
          else:  
            z.append(zk)

isl = {}
no = 1    

for i in range(1,n-1):
  for k in range(1,m-1):
    if data[i][k] == '.':
      if __debug: print(i, end=" ")
      if __debug: print(k, end=": ")
      z = mark2(work[m*i + k])
      if z:
        isl[z].append((i,k)) 
        if __debug: print(z, end=", old")
      elif z is not ocean:
        z.append(no)
        if __debug: print(no, end=", new")
        isl[no] = [(i,k)] 
        no += 1
      if __debug: print()

if __debug: print(isl)
isl_s = list(isl.values())
if __debug: print(isl_s)
isl_s.sort(key = len)
if __debug: print(isl_s)
delta = len(isl_s) - kk
if __debug: print(len(isl_s), end=" ")
if __debug: print(kk)
for t in range(delta):
  for i, k in isl_s[t]:
    data[i][k] = '*'
    
print(sum(map(len, isl_s[:delta])))
for i in range(n):
  print("".join(data[i]))

import sys
from collections import defaultdict, OrderedDict

sys.setrecursionlimit(10**5)
n, m, k = list(map(int, input().split()))

g = []
for i in range(n):
    g.append(list(input()))

dy = [1, -1, 0, 0]
dx = [0, 0, 1, -1]
d = defaultdict(list)


def dfs(g, u, visited):
    visited[u[0]][u[1]] = True
    # print(visited)

    for i in range(4):
        y = u[0] + dy[i]
        x = u[1] + dx[i]
        # print('y = {0} x = {1}'.format(y, x))
        if y >= 0 and y < n and x >= 0 and x < m \
           and g[y][x] == '.' and not visited[y][x]:
            # if u == (2, 3):
                # print('y = {0} x = {1}'.format(y, x))
            dfs(g, (y, x), visited)


def dfs_(g, u, visited, component):
    visited[u[0]][u[1]] = True
    d[component].append(u)

    for i in range(4):
        y = u[0] + dy[i]
        x = u[1] + dx[i]
        if y >= 0 and y < n and x >= 0 and x < m \
           and g[y][x] == '.' and not visited[y][x]:
            dfs_(g, (y, x), visited, component)

# dfs from the outer edge and mark the bad ones
visited = [[False for j in range(m)] for i in range(n)]

for i in range(m):
    if g[0][i] == '.' and not visited[0][i]:
        dfs(g, (0, i), visited)
    if g[n - 1][i] == '.' and not visited[-1][i]:
        dfs(g, (n - 1, i), visited)

for i in range(1, n - 1):
    if g[i][0] == '.' and not visited[i][0]:
        dfs(g, (i, 0), visited)
    if g[i][m - 1] == '.' and not visited[i][m - 1]:
        dfs(g, (i, m - 1), visited)

component = 0
for i in range(1, n - 1):
    for j in range(1, m - 1):
        if g[i][j] == '.' and not visited[i][j]:
            dfs_(g, (i, j), visited, component)
            component += 1


# print(d)
l = sorted(list(d.items()), key=lambda x: len(x[1]))

i = 0
count = 0
while component > k:
    component -= 1
    count += len(l[i][1])
    for u in l[i][1]:
        g[u[0]][u[1]] = '*'
    i += 1

print(count)

for i in range(n):
    print(''.join(g[i]))


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
def delete(j,m):
    nonlocal a
    i=0
    while(i<len(oz[j])):
        w,b=oz[j][i],oz[j][i+1]
        i+=2
        a[w][b]='*'
def poi(i,j,k):
    visited[i][j]=True
    oz[k]+=i,j
    ozsi[k]+=1
    if (j-1)>=0 and a[i][j-1]=='.' and not visited[i][j-1]:
        poi(i,j-1,k)
    if (j+1)<=(m+1) and a[i][j+1]=='.'and not visited[i][j+1]:
        poi(i,j+1,k)
    if (i-1)>=0 and a[i-1][j]=='.'and not visited[i-1][j]:
        poi(i-1,j,k)
    if (i+1)<=(n+1) and a[i+1][j] == '.'and not visited[i+1][j]:
        poi(i+1, j,k)
n,m,vb=map(int,input().split())
a=[]
oz=[]
ozsi=[]
visited=[[False for r in range(m+2)] for t in range(n+2)]
a.append(['.']*(m+2))
sys.setrecursionlimit(3000)
for i in range(n):
    s='.'+input()+'.'
    a.append([str(x) for x in s])
k=0
a.append(['.']*(m+2))
for i in range(0,n+2):
    for j in range(0,m+2):
        if a[i][j]=='.':
            if not visited[i][j]:
                ozsi.append(0)
                oz.append([])
                poi(i,j,k)
                k += 1
t=len(ozsi)-vb-1
e=0
for i in range(t):
    min=10**10
    minj=0
    for j in range(1,len(ozsi)):
        if ozsi[j]<min:
            min=ozsi[j]
            minj=j
    e+=min
    delete(minj,min)
    ozsi[minj]=10**10
print(e)
for i in range(1,n+1):
    for j in range(1,m+1):
        print(a[i][j],end='')
    print()

from collections import Counter, defaultdict

n, m, k = [int(x) for x in input().split()]

mp = [["."] * (m + 2)] + [list("." + input().strip() + ".") for _ in range(n)] + [["."] * (m + 2)]
uf = {}
for i in range(n + 2):
    for j in range(m + 2):
        uf[(i, j)] = (i, j)


def find(u):
    if u != uf[u]:
        uf[u] = find(uf[u])
    return uf[u]


def union(u, v):
    pu = find(u)
    pv = find(v)
    if pu != pv:
        uf[pv] = pu


for i in range(n + 2):
    for j in range(m + 2):
        if mp[i][j] == '*':
            continue
        if i <= n and mp[i + 1][j] == '.':
            union((i, j), (i + 1, j))
        if j <= m and mp[i][j + 1] == '.':
            union((i, j), (i, j + 1))

d = defaultdict(int)
f0 = find((0, 0))
for i in range(n + 2):
    for j in range(m + 2):
        if mp[i][j] == '*':
            continue
        f = find((i, j))
        if f != f0:
            d[f] += 1
l = sorted([(v, k) for k, v in list(d.items())])
ll = len(l)
ps = set([u for _, u in l[:ll - k]])
r = 0
for i in range(1, n + 1):
    for j in range(1, m + 1):
        if mp[i][j] == '.' and find((i, j)) in ps:
            mp[i][j] = '*'
            r += 1

print(r)
print("\n".join( "".join(m[1:-1])for m in mp[1:-1]))


corr = lambda x, y: 0 <= x < n and 0 <= y < m and a[x][y] == '.'
ocean = lambda x, y: x in (0, n - 1) or y in (0, m - 1)
D = (0, 1), (0, -1), (1, 0), (-1, 0)
from sys import setrecursionlimit
setrecursionlimit(10 ** 5)

def dfs(x, y):
    nonlocal size, flag
    was[x][y] = clr
    size += 1
    if ocean(x, y): flag = True
    for dx, dy in D:
        nx, ny = x + dx, y + dy
        if corr(nx, ny) and not was[nx][ny]:
            dfs(nx, ny)

read = lambda: map(int, input().split())
n, m, k = read()
a = [input() for i in range(n)]
s = []
was = [[0] * m for i in range(n)]
clr = 1
for i in range(n):
    for j in range(m):
        if not was[i][j] and a[i][j] == '.':
            flag = False
            size = 0
            dfs(i, j)
            if not  flag:
                s.append((size, clr))
            clr += 1
s.sort()
ans = 0
color = set()
for i in range(len(s) - k):
    ans += s[i][0]
    color.add(s[i][1])
b = [list(a[i]) for i in range(n)]
for i in range(n):
    for j in range(m):
        if was[i][j] in color:
            b[i][j] = '*'
print(ans)
[print(''.join(i)) for i in b]


class DSU:
    def __init__(self, n):
        self.parents = [-1] * n
        self.size = [0] * n

    def make(self, v):
        if self.parents[v] != -1:
            return
        self.parents[v] = v
        self.size[v] = 1

    def find(self, v):
        if self.parents[v] in (v, -1):
            return self.parents[v]
        self.parents[v] = self.find(self.parents[v])
        return self.parents[v]

    def join(self, v, u):
        v = self.find(v)
        u = self.find(u)
        if v == u or -1 in (u, v):
            return
        if self.size[v] < self.size[u]:
            v, u = u, v
        self.parents[u] = v
        self.size[v] += self.size[u]

def get_cart2line(m): 
    return lambda x, y: x * m + y

def main():
    n, m, k = map(int, input().split())
    g = [list(input()) for _ in range(n)]
    
    ocean = 0
    dsu = DSU(n * m)
    dsu.make(ocean)
    
    line = get_cart2line(m)
    
    for i in range(n):
        a, b = line(i, 0), line(i, m - 1)
        dsu.make(a)
        dsu.make(b)
        dsu.join(ocean, a)
        dsu.join(ocean, b)

    for i in range(m):
        a, b = line(0, i), line(n-1, i)
        dsu.make(a)
        dsu.make(b)
        dsu.join(ocean, a)
        dsu.join(ocean, b)
        
    for i in range(n):
        for j in range(m):
            if g[i][j] == '.':
                dsu.make(line(i, j))
                if j > 0:
                    if g[i][j-1] == '.':
                        dsu.join(line(i, j-1), line(i, j))
                if i > 0:
                    if g[i-1][j] == '.':
                        dsu.join(line(i-1, j), line(i, j))

    s = set()
    for i in range(n):
        for j in range(m):
            tmp = dsu.find(line(i, j))
            if tmp not in (-1, dsu.find(ocean)):
                s.add(tmp)
    s = [(dsu.find(i), dsu.size[dsu.find(i)]) for i in s]
    s.sort(key=lambda x: x[1])
    if len(s) == k:
        print(0)
        print(*(''.join(r) for r in g), sep='\n')
        return
    s = s[:len(s) - k]
    ans = sum((x[1] for x in s))
    s = {x[0] for x in s}
    for i in range(n):
        for j in range(m):
            if dsu.find(line(i, j)) in s:
                g[i][j] = '*'
    print(ans)
    print(*(''.join(r) for r in g), sep='\n')

main()


def main():
    def dfs(x, y):
        nonlocal inner
        if land[y][x] == '.':
            s = 1
            land[y][x] = '*'
            if x:
                s += dfs(x - 1, y)
            else:
                inner = False
            if x + 1 < m:
                s += dfs(x + 1, y)
            else:
                inner = False
            if y:
                s += dfs(x, y - 1)
            else:
                inner = False
            if y + 1 < n:
                s += dfs(x, y + 1)
            else:
                inner = False
            return s
        return 0

    n, m, k = list(map(int, input().split()))
    land = [list(input()) for _ in range(n)]
    sav = [row[:] for row in land]
    lakes = []
    for y, row in enumerate(land):
        for x, f in enumerate(row):
            inner = True
            s = dfs(x, y)
            if s and inner:
                lakes.append((s, x, y))
    land, res = sav, 0
    for _, x, y in sorted(lakes)[:len(lakes) - k]:
        res += dfs(x, y)
    print(res)
    for row in land:
        print(''.join(row))


def __starting_point():
    from sys import setrecursionlimit

    setrecursionlimit(3000)
    main()

__starting_point()

def main():
    def dfs(x, y):
        nonlocal inner
        if d[x, y] == '.':
            d[x, y], s = '*', 1
            for uv in (x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1):
                try:
                    s += dfs(*uv)
                except KeyError:
                    inner = False
            return s
        return 0

    n, m, k = list(map(int, input().split()))
    d = {(x, y): c for y in range(n) for x, c in enumerate(input())}
    sav, lakes = d.copy(), []
    for y in range(n):
        for x in range(m):
            inner = True
            s = dfs(x, y)
            if s and inner:
                lakes.append((s, x, y))
    d, res = sav, 0
    for _, x, y in sorted(lakes)[:len(lakes) - k]:
        res += dfs(x, y)
    print(res)
    for y in range(n):
        print(''.join(d[x, y] for x in range(m)))


def __starting_point():
    from sys import setrecursionlimit

    setrecursionlimit(6000)
    main()

__starting_point()

import sys
def dfs(i, j):
    nonlocal used, ans
    used[i][j] = True
    ans.append((i, j))
    if (i - 1 >= 0 and j >= 0 and not used[i - 1][j]):
        dfs(i - 1, j)
    if (i >= 0 and j + 1 <= m - 1 and not used[i][j + 1]):
        dfs(i, j + 1)  
    if (i + 1 <= n - 1 and j <= m - 1 and not used[i + 1][j]):
        dfs(i + 1, j)
    if (i <= n - 1 and j - 1 >= 0 and not used[i][j - 1]):
        dfs(i, j - 1)


sys.setrecursionlimit(1000000)
n, m, k = map(int, input().split())
used = [[True for i in range(m)] for j in range(n)]
hole = [[True for i in range(m)] for j in range(n)]
for i in range(n):
    h = input()
    for j in range(m):
        if (h[j] == "."):
            used[i][j] = False
            hole[i][j] = False
ansi = []
for i in range(n):
    for j in range(m):
        ans = []
        if (not used[i][j]):
            dfs(i, j)
        if len(ans):
            f = False
            for elem in ans:
                if (not elem[0]) or (not elem[1]) or (elem[0] == n - 1) or (elem[1] == m - 1):
                    f = True
            if not f:
                ansl = [len(ans)] + ans
                ansi.append(ansl)
ansi.sort()
s = 0
for i in range(len(ansi) - k):
    s += ansi[i][0]
    for j in range(1, ansi[i][0] + 1):
        hole[ansi[i][j][0]][ansi[i][j][1]] = True
print(s)
for i in range(n):
    op = ""
    for j in range(m):
        if hole[i][j]:
            op += "*"
        else:
            op += "."
    print(op)

from typing import List, Tuple


def solve(n: int, m: int, k: int, chart: List[List[str]]):

    visited = [[0] * m for _ in range(n)]

    def dfs(x: int, y: int) -> List[Tuple[int, int]]:
        stack = [(x, y)]
        component = set([(x, y)])
        is_ocean = False
        while stack:
            a, b = stack.pop()
            visited[a][b] = 1
            if a == 0 or a == n - 1 or b == 0 or b == m - 1:
                is_ocean = True
            for ao, bo in [(a, b + 1), (a, b - 1), (a + 1, b), (a - 1, b)]:
                if 0 <= ao < n and 0 <= bo < m:
                    if not visited[ao][bo] and chart[ao][bo] == '.':
                        stack.append((ao, bo))
                        component.add((ao, bo))
        if not is_ocean:
            return list(component)

    lakes = []
    for i in range(1, n - 1):
        for j in range(1, m - 1):
            if not visited[i][j] and chart[i][j] == '.':
                lake = dfs(i, j)
                if lake:
                    lakes.append(lake)
    changes = 0
    for component in sorted(lakes, key=len)[:len(lakes) - k]:
        for x, y in component:
            changes += 1
            chart[x][y] = '*'
    return changes, chart


n, m, k = list(map(int, input().split()))
chart = [list(input()) for _ in range(n)]
changes, new_chart = solve(n, m, k, chart)
print(changes)
for line in new_chart:
    print(''.join(line))