Pavel loves grid mazes. A grid maze is an n × m rectangle maze where each cell is either empty, or is a wall. You can go from one cell to another only if both cells are empty and have a common side.Pavel drew a grid maze with all empty cells forming a connected area. That is, you can go from any empty cell to any other one. Pavel doesn't like it when his maze has too little walls. He wants to turn exactly k empty cells into walls so that all the remaining cells still formed a connected area. Help him.

The first line contains three integers n, m, k (1 ≤ n, m ≤ 500, 0 ≤ k < s), where n and m are the maze's height and width, correspondingly, k is the number of walls Pavel wants to add and letter s represents the number of empty cells in the original maze. Each of the next n lines contains m characters. They describe the original maze. If a character on a line equals ".", then the corresponding cell is empty and if the character equals "#", then the cell is a wall.

Print n lines containing m characters each: the new maze that fits Pavel's requirements. Mark the empty cells that you transformed into walls as "X", the other cells must be left without changes (that is, "." and "#"). It is guaranteed that a solution exists. If there are multiple solutions you can output any of them.

package main

import (
	"fmt"
	"bufio"
	"os"
	//"strconv"
	//"sort"
)

type dir struct{
	x, y int
}

func main() {
	reader := bufio.NewReader(os.Stdin)
	var n, m, k int
	fmt.Fscanf(reader, "%d %d %d\n", &n, &m, &k)
	s := make([][]byte, n)
	for i := 0; i < n; i++ {
		str, _ := reader.ReadString('\n')
		s[i] = []byte(str)
	}
	//fmt.Println(s)
	var x, y, e int
	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			if s[i][j] == '.' {
				x, y = i, j
				e++
			}
		}
	}

	k = e - k
	var dfs func(x, y int)
	dfs = func(i, j int) {
		if k <= 0 { return }
		if i < 0 || j < 0 || i >= n || j >= m { return }
		if s[i][j] != '.' { return }
		s[i][j] = 'k'
		k--
		for _, d := range []dir{{0, 1}, {0, -1}, {1, 0}, {-1, 0}} {
			i1, j1 := i + d.x, j + d.y
			dfs(i1, j1)
		}
	}
	dfs(x, y)
	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			if s[i][j] == '.' {
				s[i][j] = 'X'
			} else if s[i][j] == 'k' {
				s[i][j] = '.'
			}
		}
	}
	for i := 0; i < n; i++ {
		fmt.Print(string(s[i]))
	}
}

package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
	"regexp"
)

func main() {
	var n, m, k int
	if _, err := fmt.Scanf("%d %d %d\n", &n, &m, &k); err != nil {
		return
	}

	reader := bufio.NewReader(os.Stdin)
	re := regexp.MustCompile(`\r?\n`)

	maze := make([][]rune, n+2)
	for i := 0; i < n+2; i++ {
		maze[i] = make([]rune, m+2)
	}

	has_start := false
	start := [2]int{0, 0}
	x_count := 0

	for i := 1; i <= n; i++ {
		if s, err := reader.ReadString('\n'); err != nil {
			if err == io.EOF {
				break
			} else {
				return
			}
		} else {
			s = re.ReplaceAllString(s, "")
			maze[i][0] = '#'
			for j := 1; j <= m; j++ {
				maze[i][j] = rune(s[j-1])
				if maze[i][j] == '.' {
					if !has_start {
						start[0] = i
						start[1] = j
						has_start = true
					} else {
						maze[i][j] = 'X'
						x_count++
					}
				}
			}
			maze[i][m+1] = '#'
		}
	}
	for i := 0; i < m+2; i++ {
		maze[0][i] = '#'
		maze[n+1][i] = '#'
	}

	starts := make(map[[2]int]bool)
	starts[start] = true

	remain := x_count - k
	for remain > 0 {
		new_starts := make(map[[2]int]bool)
		for s := range starts {
			i := s[0] - 1
			j := s[1]
			if remove_x(maze, i, j, new_starts) {
				remain--
				if remain == 0 {
					break
				}
			}

			i = s[0] + 1
			j = s[1]
			if remove_x(maze, i, j, new_starts) {
				remain--
				if remain == 0 {
					break
				}
			}

			i = s[0]
			j = s[1] - 1
			if remove_x(maze, i, j, new_starts) {
				remain--
				if remain == 0 {
					break
				}
			}

			i = s[0]
			j = s[1] + 1
			if remove_x(maze, i, j, new_starts) {
				remain--
				if remain == 0 {
					break
				}
			}
		}
		starts = new_starts
	}

	for i := 1; i <= n; i++ {
		fmt.Println(string(maze[i][1 : m+1]))
	}
}

func remove_x(maze [][]rune, i, j int, new_starts map[[2]int]bool) bool {
	if maze[i][j] == 'X' {
		maze[i][j] = '.'
		new_starts[[2]int{i, j}] = true
		return true
	}
	return false
}

package main

import (
    "fmt"
    "bufio"
    "os"
)

var n,m,k int
var f [505][505]bool
var r [505][505]rune
var di [4]int = [4]int{-1,0,1,0}
var dj [4]int = [4]int{0,1,0,-1}

func dfs(i,j int) {
    f[i][j] = true
    for in := 0; (in < 4); in++ {
        ni, nj := i+di[in], j+dj[in]
        if (ni >= 0) && (ni < n) && (nj >= 0) && (nj < m) && !f[ni][nj] {
            dfs(ni,nj)
        }
    }
    if (k > 0) {
        k--
        r[i][j] = 'X'
    }
}

func main() {
    scanner := bufio.NewScanner(os.Stdin)
    writer := bufio.NewWriter(os.Stdout)
    fmt.Scan(&n,&m,&k)
    for i := 0; i < n; i++ {
        for scanner.Scan() {
            if scanner.Text() != "" { break }
        }
        for j, v := range scanner.Text() {
            r[i][j] = v
            if v == '#' { f[i][j] = true }
        }
    }
    for i := 0; i < n; i++ {
        for j := 0; j < m; j++ {
            if r[i][j] == '.' {
                dfs(i,j)
                break
            }
        }
    }
    for i := 0; i < n; i++ {
        for j := 0; j < m; j++ {
            writer.WriteRune(r[i][j])
        }
        writer.WriteRune('\n')
    }
    writer.Flush()
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	reader := bufio.NewReader(os.Stdin)

	var n, m, k int
	fmt.Fscanf(reader, "%d %d %d\n", &n, &m, &k)
	s := make([][]byte, n)
	for i := 0; i < n; i++ {
		str, _ := reader.ReadString('\n')
		s[i] = []byte(str)
	}
//	fmt.Println(s)

	var x, y, e int
	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			if s[i][j] == '.' {
				x, y = i, j
				e++
			}
		}
	}
	type dir struct{
		x, y int
	}
	k = e - k
	var dfs func(x, y int)
	dfs = func(i, j int) {
		if k <= 0 { return }
		if i < 0 || j < 0 || i >= n || j >= m { return }
		if s[i][j] != '.' { return }
		s[i][j] = 'k'
		k--
		for _, d := range []dir{{0, 1}, {0, -1}, {1, 0}, {-1, 0}} {
			i1, j1 := i + d.x, j + d.y
			dfs(i1, j1)
		}
	}
	dfs(x, y)
	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			if s[i][j] == '.' {
				s[i][j] = 'X'
			} else if s[i][j] == 'k' {
				s[i][j] = '.'
			}
		}
	}
	for i := 0; i < n; i++ {
		fmt.Print(string(s[i]))
	}
}

// Author: sighduck
// URL: https://codeforces.com/problemset/problem/377/A

package main

import (
	"bufio"
	"fmt"
	"os"
	"strings"
)

type Cell struct {
	X int
	Y int
}

func Solve(n int, m int, k int, grid [][]string) [][]string {
	connectedLength := 0
	startCell := Cell{}
	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			if grid[i][j] == "." {
				connectedLength += 1
				startCell.X = i
				startCell.Y = j
			}
		}
	}
	pathLength := connectedLength - k
	queue := []Cell{startCell}
	dx := []int{-1, 0, 1, 0}
	dy := []int{0, 1, 0, -1}
	visited := make(map[Cell]bool)
	for pathLength > 0 {
		current := queue[0]
		queue = queue[1:]
		grid[current.X][current.Y] = "V"
		pathLength -= 1
		for i := 0; i < 4; i++ {
			newCell := Cell{current.X + dx[i], current.Y + dy[i]}
			if 0 <= newCell.X && newCell.X < n && 0 <= newCell.Y && newCell.Y < m {
				if grid[newCell.X][newCell.Y] == "." && !visited[newCell] {
					queue = append(queue, newCell)
					visited[newCell] = true
				}
			}
		}
	}
	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			if grid[i][j] == "." {
				grid[i][j] = "X"
			} else if grid[i][j] == "V" {
				grid[i][j] = "."
			}
		}
	}
	return grid
}

func main() {
	reader := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	var n, m, k int
	fmt.Fscanf(reader, "%d %d %d\n", &n, &m, &k)
	grid := make([][]string, n)
	for i := 0; i < n; i++ {
		grid[i] = make([]string, m)
		var s string
		fmt.Fscanf(reader, "%s\n", &s)
		for j := 0; j < m; j++ {
			grid[i][j] = string(s[j])
		}
	}

	answer := Solve(n, m, k, grid)
	for i := 0; i < n; i++ {
		fmt.Fprintf(writer, "%s\n", strings.Join(answer[i], ""))
	}
}

package main

import (
	"bufio"
	"bytes"
	"fmt"
	"os"
	"sort"
	"strconv"
	"strings"
)

var (
	__in = bufio.NewReader(os.Stdin)
)

func nthArg(n int) func(...interface{}) interface{} {
	return func(arg ...interface{}) interface{} {
		return arg[n]
	}
}

func unique(b *[]int) {
	a := *b
	n := len(a)
	if n > 0 {
		sort.Ints(a)
		i, j := 1, 0
		for ; i < n; i++ {
			if a[i] != a[j] {
				j++
				a[j] = a[i]
			}
		}
		*b = a[:j+1]
	}
}

func getInt() int {
	x, sign := 0, 1
	ch, _ := __in.ReadByte()
	for ch < '0' || ch > '9' {
		if ch == '-' {
			sign = -1
		}
		ch, _ = __in.ReadByte()
	}
	for ch >= '0' && ch <= '9' {
		x *= 10
		x += int(ch - '0')
		ch, _ = __in.ReadByte()
	}
	return x * sign
}

func getInts() []int {
	tmp := strings.Fields(nthArg(0)(__in.ReadString('\n')).(string))
	ret := make([]int, len(tmp))
	for i := range ret {
		ret[i], _ = strconv.Atoi(tmp[i])
	}
	return ret
}

func getInt64() int64 {
	var x, sign int64 = 0, 1
	ch, _ := __in.ReadByte()
	for ch < '0' || ch > '9' {
		if ch == '-' {
			sign = -1
		}
		ch, _ = __in.ReadByte()
	}
	for ch >= '0' && ch <= '9' {
		x *= 10
		x += int64(ch - '0')
		ch, _ = __in.ReadByte()
	}
	return x * sign
}

func getInts64() []int64 {
	tmp := strings.Fields(nthArg(0)(__in.ReadString('\n')).(string))
	ret := make([]int64, len(tmp))
	for i := range ret {
		ret[i], _ = strconv.ParseInt(tmp[i], 10, 64)
	}
	return ret
}

func getWord() string {
	var (
		buf bytes.Buffer
		ch  byte
	)
	for ch == 0 || ch == ' ' || ch == '\n' {
		ch, _ = __in.ReadByte()
	}
	for ch != 0 && ch != ' ' && ch != '\n' {
		buf.WriteByte(ch)
		ch, _ = __in.ReadByte()
	}
	return buf.String()
}

func getWords() []string {
	return strings.Fields(nthArg(0)(__in.ReadString('\n')).(string))
}

func getLine() string {
	return strings.TrimSpace(nthArg(0)(__in.ReadString('\n')).(string))
}

//-----------------------------------START-----------------------------------//

var (
	n, m, k int
	a       [][]byte
	found   [][]bool
)

func offLimit(r, c int) bool {
	return k == 0 || r < 0 || r >= n || c < 0 || c >= m || a[r][c] != '.' || found[r][c]
}

func dfs(r, c int) {
	if offLimit(r, c) {
		return
	}
	found[r][c] = true
	dfs(r+1, c)
	dfs(r, c+1)
	dfs(r-1, c)
	dfs(r, c-1)
	if k > 0 {
		a[r][c] = 'X'
		k--
	}
}

func main() {
	n, m, k = getInt(), getInt(), getInt()
	getLine()
	a = make([][]byte, n)
	found = make([][]bool, n)
	for i := range a {
		a[i] = []byte(getLine())
		found[i] = make([]bool, m)
	}
	for i := range a {
		for j := range a[i] {
			dfs(i, j)
		}
	}
	for i := 0; i < n; i++ {
		fmt.Printf("%s\n", string(a[i]))
	}
}

package main

import (
	"bufio"
	"bytes"
	"fmt"
	"os"
	"sort"
	"strconv"
	"strings"
)

var (
	__in = bufio.NewReader(os.Stdin)
)

func nthArg(n int) func(...interface{}) interface{} {
	return func(arg ...interface{}) interface{} {
		return arg[n]
	}
}

func unique(b *[]int) {
	a := *b
	n := len(a)
	if n > 0 {
		sort.Ints(a)
		i, j := 1, 0
		for ; i < n; i++ {
			if a[i] != a[j] {
				j++
				a[j] = a[i]
			}
		}
		*b = a[:j+1]
	}
}

func getInt() int {
	x, sign := 0, 1
	ch, _ := __in.ReadByte()
	for ch < '0' || ch > '9' {
		if ch == '-' {
			sign = -1
		}
		ch, _ = __in.ReadByte()
	}
	for ch >= '0' && ch <= '9' {
		x *= 10
		x += int(ch - '0')
		ch, _ = __in.ReadByte()
	}
	return x * sign
}

func getIntSlice() []int {
	tmp := strings.Fields(nthArg(0)(__in.ReadString('\n')).(string))
	ret := make([]int, len(tmp))
	for i := range ret {
		ret[i], _ = strconv.Atoi(tmp[i])
	}
	return ret
}

func scanInts(a ...*int) {
	b := getIntSlice()
	for i := range a {
		*a[i] = b[i]
	}
}

func getInt64() int64 {
	var x, sign int64 = 0, 1
	ch, _ := __in.ReadByte()
	for ch < '0' || ch > '9' {
		if ch == '-' {
			sign = -1
		}
		ch, _ = __in.ReadByte()
	}
	for ch >= '0' && ch <= '9' {
		x *= 10
		x += int64(ch - '0')
		ch, _ = __in.ReadByte()
	}
	return x * sign
}

func getInt64Slice() []int64 {
	tmp := strings.Fields(nthArg(0)(__in.ReadString('\n')).(string))
	ret := make([]int64, len(tmp))
	for i := range ret {
		ret[i], _ = strconv.ParseInt(tmp[i], 10, 64)
	}
	return ret
}

func scanInts64(a ...*int64) {
	b := getInt64Slice()
	for i := range a {
		*a[i] = b[i]
	}
}

func getWord() string {
	var (
		buf bytes.Buffer
		ch  byte
	)
	for ch == 0 || ch == ' ' || ch == '\n' {
		ch, _ = __in.ReadByte()
	}
	for ch != 0 && ch != ' ' && ch != '\n' {
		buf.WriteByte(ch)
		ch, _ = __in.ReadByte()
	}
	return buf.String()
}

func getWordSlice() []string {
	return strings.Fields(nthArg(0)(__in.ReadString('\n')).(string))
}

func scanWords(a ...*string) {
	b := getWordSlice()
	for i := range a {
		*a[i] = b[i]
	}
}

func getLine() string {
	return strings.TrimSpace(nthArg(0)(__in.ReadString('\n')).(string))
}

//-----------------------------------START-----------------------------------//

const (
	N = 507
	M = 507
)

var (
	n, m, k int
	a       [N][]byte
	found   [N][M]bool
)

func offLimit(r, c int) bool {
	return k == 0 || r < 0 || r >= n || c < 0 || c >= m || a[r][c] != '.' || found[r][c]
}

func dfs(r, c int) {
	if offLimit(r, c) {
		return
	}
	found[r][c] = true
	dfs(r+1, c)
	dfs(r, c+1)
	dfs(r-1, c)
	dfs(r, c-1)
	if k > 0 {
		a[r][c] = 'X'
		k--
	}
}

func main() {
	scanInts(&n, &m, &k)
	for i := 0; i < n; i++ {
		a[i] = []byte(getLine())
	}
	for i := range a {
		for j := range a[i] {
			dfs(i, j)
		}
	}
	for i := 0; i < n; i++ {
		fmt.Printf("%s\n", string(a[i]))
	}
}

package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
	"regexp"
)

func main() {
	var n, m, k int
	if _, err := fmt.Scanf("%d %d %d\n", &n, &m, &k); err != nil {
		return
	}

	reader := bufio.NewReader(os.Stdin)
	re := regexp.MustCompile(`\r?\n`)

	maze := make([][]rune, n+2)
	line_sharp_count := make(map[int]int)

	for i := 1; i <= n; i++ {
		if s, err := reader.ReadString('\n'); err != nil {
			if err == io.EOF {
				break
			} else {
				return
			}
		} else {
			s = re.ReplaceAllString(s, "")
			maze[i] = make([]rune, m+2)
			maze[i][0] = '#'
			for j := 1; j <= m; j++ {
				maze[i][j] = rune(s[j-1])
				if maze[i][j] == '#' {
					line_sharp_count[i] += 1
				}
			}
			maze[i][m+1] = '#'
		}
	}
	maze[0] = make([]rune, m+2)
	maze[n+1] = make([]rune, m+2)
	for i := 0; i < m+2; i++ {
		maze[0][i] = '#'
		maze[n+1][i] = '#'
	}

	for c := 0; c < k; c++ {
		ok := false
		for i := 1; i <= n; i++ {
			if line_sharp_count[i] >= n {
				continue
			}
			for j := 1; j <= m; j++ {
				if maze[i][j] == '.' && three_sharp(maze, i, j) {
					maze[i][j] = 'X'
					line_sharp_count[i] += 1
					ok = true
					break
				}
			}
			if ok {
				break
			}
		}
		if !ok {
			for i := 1; i <= n; i++ {
				if line_sharp_count[i] >= n {
					continue
				}
				for j := 1; j <= m; j++ {
					if maze[i][j] == '.' {
						maze[i][j] = 'X'
						line_sharp_count[i] += 1
						ok = true
						break
					}
				}
				if ok {
					break
				}
			}
		}
	}

	for i := 1; i <= n; i++ {
		fmt.Println(string(maze[i][1 : m+1]))
	}
}

func three_sharp(maze [][]rune, i, j int) bool {
	sharp_count := 0
	sharp_count += is_sharp(maze[i-1][j])
	sharp_count += is_sharp(maze[i+1][j])
	sharp_count += is_sharp(maze[i][j-1])
	sharp_count += is_sharp(maze[i][j+1])
	if sharp_count == 3 {
		return true
	}
	return false
}

func is_sharp(x rune) int {
	if x == '#' || x == 'X' {
		return 1
	}
	return 0
}

package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
	"regexp"
)

func main() {
	var n, m, k int
	if _, err := fmt.Scanf("%d %d %d\n", &n, &m, &k); err != nil {
		return
	}

	reader := bufio.NewReader(os.Stdin)
	re := regexp.MustCompile(`\r?\n`)

	maze := make([][]rune, n+2)

	for i := 1; i <= n; i++ {
		if s, err := reader.ReadString('\n'); err != nil {
			if err == io.EOF {
				break
			} else {
				return
			}
		} else {
			s = re.ReplaceAllString(s, "")
			maze[i] = make([]rune, m+2)
			maze[i][0] = '#'
			for j := 1; j <= m; j++ {
				maze[i][j] = rune(s[j-1])
			}
			maze[i][m+1] = '#'
		}
	}
	maze[0] = make([]rune, m+2)
	maze[n+1] = make([]rune, m+2)
	for i := 0; i < m+2; i++ {
		maze[0][i] = '#'
		maze[n+1][i] = '#'
	}

	for c := 0; c < k; c++ {
		ok := false
		for i := 1; i <= n; i++ {
			for j := 1; j <= m; j++ {
				if maze[i][j] == '.' && three_sharp(maze, i, j) {
					maze[i][j] = 'X'
					ok = true
					break
				}
			}
			if ok {
				break
			}
		}
	}

	for i := 1; i <= n; i++ {
		fmt.Println(string(maze[i][1 : m+1]))
	}
}

func three_sharp(maze [][]rune, i, j int) bool {
	sharp_count := 0
	sharp_count += is_sharp(maze[i-1][j])
	sharp_count += is_sharp(maze[i+1][j])
	sharp_count += is_sharp(maze[i][j-1])
	sharp_count += is_sharp(maze[i][j+1])
	if sharp_count == 3 {
		return true
	}
	return false
}

func is_sharp(x rune) int {
	if x == '#' || x == 'X' {
		return 1
	}
	return 0
}

package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
	"regexp"
)

func main() {
	var n, m, k int
	if _, err := fmt.Scanf("%d %d %d\n", &n, &m, &k); err != nil {
		return
	}

	reader := bufio.NewReader(os.Stdin)
	re := regexp.MustCompile(`\r?\n`)

	maze := make([][]rune, n+2)
	for i := 0; i < n+2; i++ {
		maze[i] = make([]rune, m+2)
	}

	for i := 1; i <= n; i++ {
		if s, err := reader.ReadString('\n'); err != nil {
			if err == io.EOF {
				break
			} else {
				return
			}
		} else {
			s = re.ReplaceAllString(s, "")
			maze[i][0] = '#'
			for j := 1; j <= m; j++ {
				maze[i][j] = rune(s[j-1])
			}
			maze[i][m+1] = '#'
		}
	}
	for i := 0; i < m+2; i++ {
		maze[0][i] = '#'
		maze[n+1][i] = '#'
	}

	three_map := make(map[[2]int]bool)
	corner_map := make(map[[2]int]bool)

	for i := 1; i <= n; i++ {
		for j := 1; j <= m; j++ {
			add_to_map(maze, three_map, corner_map, i, j)
		}
	}

	for c := 0; c < k; c++ {
		if len(three_map) > 0 {
			for v, _ := range three_map {
				i := v[0]
				j := v[1]

				maze[i][j] = 'X'
				delete(three_map, v)

				update_map(maze, three_map, corner_map, i, j)
				break
			}
		} else if len(corner_map) > 0 {
			for v, _ := range corner_map {
				i := v[0]
				j := v[1]

				if !check_corner(maze, i, j) {
					continue
				}

				maze[i][j] = 'X'
				delete(corner_map, v)

				update_map(maze, three_map, corner_map, i, j)
				break
			}
		} else {
			fmt.Println("Error!")
			return
		}
	}

	for i := 1; i <= n; i++ {
		fmt.Println(string(maze[i][1 : m+1]))
	}
}

func count_sharps(maze [][]rune, i, j int) int {
	sharp_count := 0
	sharp_count += is_sharp(maze[i-1][j])
	sharp_count += is_sharp(maze[i+1][j])
	sharp_count += is_sharp(maze[i][j-1])
	sharp_count += is_sharp(maze[i][j+1])
	return sharp_count
}

func is_sharp(x rune) int {
	if x == '#' || x == 'X' {
		return 1
	}
	return 0
}

func is_corner(maze [][]rune, i, j int) bool {
	if is_sharp(maze[i-1][j]) == 1 && is_sharp(maze[i][j-1]) == 1 {
		return true
	}
	if is_sharp(maze[i-1][j]) == 1 && is_sharp(maze[i][j+1]) == 1 {
		return true
	}
	if is_sharp(maze[i+1][j]) == 1 && is_sharp(maze[i][j-1]) == 1 {
		return true
	}
	if is_sharp(maze[i+1][j]) == 1 && is_sharp(maze[i][j+1]) == 1 {
		return true
	}
	return false
}

func update_map(maze [][]rune, three_map, corner_map map[[2]int]bool, i, j int) {
	add_to_map(maze, three_map, corner_map, i-1, j)
	add_to_map(maze, three_map, corner_map, i+1, j)
	add_to_map(maze, three_map, corner_map, i, j-1)
	add_to_map(maze, three_map, corner_map, i, j+1)
}

func add_to_map(maze [][]rune, three_map, corner_map map[[2]int]bool, i, j int) {
	if maze[i][j] == '.' {
		sc := count_sharps(maze, i, j)
		if sc == 3 {
			three_map[[2]int{i, j}] = true
			delete(corner_map, [2]int{i, j})
		} else if sc == 2 && is_corner(maze, i, j) {
			corner_map[[2]int{i, j}] = true
		}
	}
}

func check_corner(maze [][]rune, i, j int) bool {
	var ox, oy int
	if is_sharp(maze[i-1][j]) == 1 && is_sharp(maze[i][j-1]) == 1 {
		ox = i + 1
		oy = j + 1
	} else if is_sharp(maze[i-1][j]) == 1 && is_sharp(maze[i][j+1]) == 1 {
		ox = i + 1
		oy = j - 1
	} else if is_sharp(maze[i+1][j]) == 1 && is_sharp(maze[i][j-1]) == 1 {
		ox = i - 1
		oy = j + 1
	} else if is_sharp(maze[i+1][j]) == 1 && is_sharp(maze[i][j+1]) == 1 {
		ox = i - 1
		oy = j - 1
	}
	if is_sharp(maze[ox][oy]) != 1 {
		return true
	} else {
		history := make(map[[2]int]bool)
		start := [2]int{ox, j}
		target := [2]int{i, oy}
		taboo := [2]int{i, j}

		if find_neighbor(maze, history, start, target, taboo) {
			return true
		} else {
			return false
		}
	}
}

func find_neighbor(maze [][]rune, hist map[[2]int]bool, start, target, taboo [2]int) bool {
	hist[start] = true

	i := start[0]
	j := start[1]

	current := [2]int{i - 1, j}
	if maze[current[0]][current[1]] == '.' && !hist[current] && !is_equal(start, taboo) {
		if is_equal(current, target) {
			return true
		} else {
			return find_neighbor(maze, hist, current, target, taboo)
		}
	}
	current = [2]int{i + 1, j}
	if maze[current[0]][current[1]] == '.' && !hist[current] && !is_equal(start, taboo) {
		if is_equal(current, target) {
			return true
		} else {
			return find_neighbor(maze, hist, current, target, taboo)
		}
	}
	current = [2]int{i, j - 1}
	if maze[current[0]][current[1]] == '.' && !hist[current] && !is_equal(start, taboo) {
		if is_equal(current, target) {
			return true
		} else {
			return find_neighbor(maze, hist, current, target, taboo)
		}
	}
	current = [2]int{i, j + 1}
	if maze[current[0]][current[1]] == '.' && !hist[current] && !is_equal(start, taboo) {
		if is_equal(current, target) {
			return true
		} else {
			return find_neighbor(maze, hist, current, target, taboo)
		}
	}

	return false
}

func is_equal(a, b [2]int) bool {
	return a[0] == b[0] && a[1] == b[1]
}

package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
	"regexp"
)

func main() {
	var n, m, k int
	if _, err := fmt.Scanf("%d %d %d\n", &n, &m, &k); err != nil {
		return
	}

	reader := bufio.NewReader(os.Stdin)
	re := regexp.MustCompile(`\r?\n`)

	maze := make([][]rune, n+2)
	for i := 0; i < n+2; i++ {
		maze[i] = make([]rune, m+2)
	}

	for i := 1; i <= n; i++ {
		if s, err := reader.ReadString('\n'); err != nil {
			if err == io.EOF {
				break
			} else {
				return
			}
		} else {
			s = re.ReplaceAllString(s, "")
			maze[i][0] = '#'
			for j := 1; j <= m; j++ {
				maze[i][j] = rune(s[j-1])
			}
			maze[i][m+1] = '#'
		}
	}
	for i := 0; i < m+2; i++ {
		maze[0][i] = '#'
		maze[n+1][i] = '#'
	}

	three_map := make(map[[2]int]bool)
	corner_map := make(map[[2]int]bool)

	for i := 1; i <= n; i++ {
		for j := 1; j <= m; j++ {
			add_to_map(maze, three_map, corner_map, i, j)
		}
	}

	fill_maps := [2]map[[2]int]bool{three_map, corner_map}

	for c := 0; c < k; c++ {
		ok := false
		for _, mm := range fill_maps {
			if len(mm) > 0 {
				for v, _ := range mm {
					i := v[0]
					j := v[1]
					maze[i][j] = 'X'
					ok = true
					delete(mm, v)

					update_map(maze, fill_maps, i, j)
					break
				}
			}
			if ok {
				break
			}
		}
	}

	for i := 1; i <= n; i++ {
		fmt.Println(string(maze[i][1 : m+1]))
	}
}

func count_sharps(maze [][]rune, i, j int) int {
	sharp_count := 0
	sharp_count += is_sharp(maze[i-1][j])
	sharp_count += is_sharp(maze[i+1][j])
	sharp_count += is_sharp(maze[i][j-1])
	sharp_count += is_sharp(maze[i][j+1])
	return sharp_count
}

func is_sharp(x rune) int {
	if x == '#' || x == 'X' {
		return 1
	}
	return 0
}

func is_corner(maze [][]rune, i, j int) bool {
	if is_sharp(maze[i-1][j]) == 1 && is_sharp(maze[i][j-1]) == 1 && is_sharp(maze[i+1][j+1]) == 0 {
		return true
	}
	if is_sharp(maze[i-1][j]) == 1 && is_sharp(maze[i][j+1]) == 1 && is_sharp(maze[i+1][j-1]) == 0 {
		return true
	}
	if is_sharp(maze[i+1][j]) == 1 && is_sharp(maze[i][j-1]) == 1 && is_sharp(maze[i-1][j+1]) == 0 {
		return true
	}
	if is_sharp(maze[i+1][j]) == 1 && is_sharp(maze[i][j+1]) == 1 && is_sharp(maze[i-1][j-1]) == 0 {
		return true
	}
	return false
}

func update_map(maze [][]rune, fill_maps [2]map[[2]int]bool, i, j int) {
	add_to_map(maze, fill_maps[0], fill_maps[1], i-1, j)
	add_to_map(maze, fill_maps[0], fill_maps[1], i+1, j)
	add_to_map(maze, fill_maps[0], fill_maps[1], i, j-1)
	add_to_map(maze, fill_maps[0], fill_maps[1], i, j+1)
}

func add_to_map(maze [][]rune, three_map, corner_map map[[2]int]bool, i, j int) {
	if maze[i][j] == '.' {
		sc := count_sharps(maze, i, j)
		if sc == 3 {
			three_map[[2]int{i, j}] = true
			delete(corner_map, [2]int{i, j})
		} else if sc == 2 && is_corner(maze, i, j) {
			corner_map[[2]int{i, j}] = true
		}
	}
}

package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
	"regexp"
)

func main() {
	var n, m, k int
	if _, err := fmt.Scanf("%d %d %d\n", &n, &m, &k); err != nil {
		return
	}

	reader := bufio.NewReader(os.Stdin)
	re := regexp.MustCompile(`\r?\n`)

	maze := make([][]rune, n+2)
	sharp_counts := make([][]int, n+2)
	for i := 0; i < n+2; i++ {
		maze[i] = make([]rune, m+2)
		sharp_counts[i] = make([]int, m+2)
	}

	for i := 1; i <= n; i++ {
		if s, err := reader.ReadString('\n'); err != nil {
			if err == io.EOF {
				break
			} else {
				return
			}
		} else {
			s = re.ReplaceAllString(s, "")
			maze[i][0] = '#'
			sharp_counts[i][1] += 1
			for j := 1; j <= m; j++ {
				maze[i][j] = rune(s[j-1])
				if maze[i][j] == '#' {
					sharp_counts[i-1][j] += 1
					sharp_counts[i+1][j] += 1
					sharp_counts[i][j-1] += 1
					sharp_counts[i][j+1] += 1
				}
			}
			maze[i][m+1] = '#'
			sharp_counts[i][m] += 1
		}
	}
	for i := 0; i < m+2; i++ {
		maze[0][i] = '#'
		maze[n+1][i] = '#'
		sharp_counts[1][i] += 1
		sharp_counts[n][i] += 1
	}

	count_map := make(map[int]map[[2]int]bool)
	for i := 0; i <= 4; i++ {
		count_map[i] = make(map[[2]int]bool)
	}

	for i := 1; i <= n; i++ {
		for j := 1; j <= m; j++ {
			if maze[i][j] == '.' {
				sc := sharp_counts[i][j]
				count_map[sc][[2]int{i, j}] = true
			}
		}
	}

	for c := 0; c < k; c++ {
		ok := false
		for cc := 3; cc >= 2; cc-- {
			if len(count_map[cc]) > 0 {
				for v, _ := range count_map[cc] {
					if !count_map[cc][v] {
						continue
					}
					i := v[0]
					j := v[1]
					maze[i][j] = 'X'
					ok = true
					delete(count_map[cc], v)

					update_map(maze, sharp_counts, count_map, i-1, j)
					update_map(maze, sharp_counts, count_map, i+1, j)
					update_map(maze, sharp_counts, count_map, i, j-1)
					update_map(maze, sharp_counts, count_map, i, j+1)
					break
				}
			}
			if ok {
				break
			}
		}
	}

	for i := 1; i <= n; i++ {
		fmt.Println(string(maze[i][1 : m+1]))
	}
}

func update_map(maze [][]rune, sharp_counts [][]int, count_map map[int]map[[2]int]bool, i, j int) {
	if maze[i][j] == '.' {
		sharp_counts[i][j] += 1
		sct := sharp_counts[i][j]
		delete(count_map[sct-1], [2]int{i, j})
		count_map[sct][[2]int{i, j}] = true
	}
}

package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
	"regexp"
)

func main() {
	var n, m, k int
	if _, err := fmt.Scanf("%d %d %d\n", &n, &m, &k); err != nil {
		return
	}

	reader := bufio.NewReader(os.Stdin)
	re := regexp.MustCompile(`\r?\n`)

	maze := make([][]rune, n+2)
	sharp_counts := make([][]int, n+2)
	for i := 0; i < n+2; i++ {
		maze[i] = make([]rune, m+2)
		sharp_counts[i] = make([]int, m+2)
	}

	for i := 1; i <= n; i++ {
		if s, err := reader.ReadString('\n'); err != nil {
			if err == io.EOF {
				break
			} else {
				return
			}
		} else {
			s = re.ReplaceAllString(s, "")
			maze[i][0] = '#'
			sharp_counts[i][1] += 1
			for j := 1; j <= m; j++ {
				maze[i][j] = rune(s[j-1])
				if maze[i][j] == '#' {
					sharp_counts[i-1][j] += 1
					sharp_counts[i+1][j] += 1
					sharp_counts[i][j-1] += 1
					sharp_counts[i][j+1] += 1
				}
			}
			maze[i][m+1] = '#'
			sharp_counts[i][m] += 1
		}
	}
	for i := 0; i < m+2; i++ {
		maze[0][i] = '#'
		maze[n+1][i] = '#'
		sharp_counts[1][i] += 1
		sharp_counts[n][i] += 1
	}

	count_map := make(map[int]map[[2]int]bool)
	for i := 0; i <= 4; i++ {
		count_map[i] = make(map[[2]int]bool)
	}

	for i := 1; i <= n; i++ {
		for j := 1; j <= m; j++ {
			if maze[i][j] == '.' {
				sc := sharp_counts[i][j]
				count_map[sc][[2]int{i, j}] = true
			}
		}
	}

	for c := 0; c < k; c++ {
		ok := false
		for cc := 3; cc >= 2; cc-- {
			if len(count_map[cc]) > 0 {
				for v, _ := range count_map[cc] {
					i := v[0]
					j := v[1]
					maze[i][j] = 'X'
					ok = true
					delete(count_map[cc], v)

					update_map(maze, sharp_counts, count_map, i-1, j)
					update_map(maze, sharp_counts, count_map, i+1, j)
					update_map(maze, sharp_counts, count_map, i, j-1)
					update_map(maze, sharp_counts, count_map, i, j+1)

					break
				}
			}
			if ok {
				break
			}
		}
	}

	for i := 1; i <= n; i++ {
		fmt.Println(string(maze[i][1 : m+1]))
	}
}

func update_map(maze [][]rune, sharp_counts [][]int, count_map map[int]map[[2]int]bool, i, j int) {
	if maze[i][j] == '.' {
		sharp_counts[i][j] += 1
		sct := sharp_counts[i][j]
		delete(count_map[sct-1], [2]int{i, j})
		count_map[sct][[2]int{i, j}] = true
	}
}

package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
	"regexp"
)

func main() {
	var n, m, k int
	if _, err := fmt.Scanf("%d %d %d\n", &n, &m, &k); err != nil {
		return
	}

	reader := bufio.NewReader(os.Stdin)
	re := regexp.MustCompile(`\r?\n`)

	maze := make([][]rune, n+2)
	for i := 0; i < n+2; i++ {
		maze[i] = make([]rune, m+2)
	}

	for i := 1; i <= n; i++ {
		if s, err := reader.ReadString('\n'); err != nil {
			if err == io.EOF {
				break
			} else {
				return
			}
		} else {
			s = re.ReplaceAllString(s, "")
			maze[i][0] = '#'
			for j := 1; j <= m; j++ {
				maze[i][j] = rune(s[j-1])
			}
			maze[i][m+1] = '#'
		}
	}
	for i := 0; i < m+2; i++ {
		maze[0][i] = '#'
		maze[n+1][i] = '#'
	}

	three_map := make(map[[2]int]bool)
	corner_map := make(map[[2]int]bool)

	for i := 1; i <= n; i++ {
		for j := 1; j <= m; j++ {
			add_to_map(maze, three_map, corner_map, i, j)
		}
	}

	fill_maps := [2]map[[2]int]bool{three_map, corner_map}

	for c := 0; c < k; c++ {
		ok := false
		for _, mm := range fill_maps {
			if len(mm) > 0 {
				for v, _ := range mm {
					i := v[0]
					j := v[1]
					maze[i][j] = 'X'
					ok = true
					delete(mm, v)

					update_map(maze, fill_maps, i, j)
					break
				}
			}
			if ok {
				break
			}
		}
	}

	for i := 1; i <= n; i++ {
		fmt.Println(string(maze[i][1 : m+1]))
	}
}

func count_sharps(maze [][]rune, i, j int) int {
	sharp_count := 0
	sharp_count += is_sharp(maze[i-1][j])
	sharp_count += is_sharp(maze[i+1][j])
	sharp_count += is_sharp(maze[i][j-1])
	sharp_count += is_sharp(maze[i][j+1])
	return sharp_count
}

func is_sharp(x rune) int {
	if x == '#' || x == 'X' {
		return 1
	}
	return 0
}

func is_corner(maze [][]rune, i, j int) bool {
	if is_sharp(maze[i-1][j]) == 1 || is_sharp(maze[i+1][j]) == 1 {
		if is_sharp(maze[i][j-1]) == 1 || is_sharp(maze[i][j+1]) == 1 {
			return true
		}
	}
	return false
}

func update_map(maze [][]rune, fill_maps [2]map[[2]int]bool, i, j int) {
	add_to_map(maze, fill_maps[0], fill_maps[1], i-1, j)
	add_to_map(maze, fill_maps[0], fill_maps[1], i+1, j)
	add_to_map(maze, fill_maps[0], fill_maps[1], i, j-1)
	add_to_map(maze, fill_maps[0], fill_maps[1], i, j+1)
}

func add_to_map(maze [][]rune, three_map, corner_map map[[2]int]bool, i, j int) {
	if maze[i][j] == '.' {
		sc := count_sharps(maze, i, j)
		if sc == 3 {
			three_map[[2]int{i, j}] = true
			delete(corner_map, [2]int{i, j})
		} else if sc == 2 && is_corner(maze, i, j) {
			corner_map[[2]int{i, j}] = true
		}
	}
}

package main

import (
    "fmt"
    "bufio"
    "os"
)

var n,m,k int
var f [505][505]bool
var r [505][505]rune
var di [4]int = [4]int{-1,0,1,0}
var dj [4]int = [4]int{0,1,0,-1}

func dfs(i,j int) {
    f[i][j] = true

    for in := 0; (in < 4); in++ {
        ni, nj := i+di[in], j+dj[in]
        if (ni >= 0) && (ni < n) && (nj >= 0) && (nj < m) && !f[ni][nj] {
            dfs(ni,nj)
        }
    }
    if (k > 0) {
        k--
        r[i][j] = 'X'
    }
}

func main() {
    scanner := bufio.NewScanner(os.Stdin)
    writer := bufio.NewWriter(os.Stdout)
    fmt.Scan(&n,&m,&k)
    for i := 0; i < n; i++ {
        scanner.Scan()
        for j, v := range scanner.Text() {
            r[i][j] = v
            if v == '#' { f[i][j] = true }
        }
    }
    for i := 0; i < n; i++ {
        for j := 0; j < m; j++ {
            if r[i][j] == '.' {
                dfs(i,j)
                break
            }
        }
    }
    for i := 0; i < n; i++ {
        for j := 0; j < m; j++ {
            writer.WriteRune(r[i][j])
        }
        writer.WriteRune('\n')
    }
    writer.Flush()
}

package main

import (
	"bufio"
	"bytes"
	"fmt"
	"os"
	"sort"
	"strconv"
	"strings"
)

var (
	__in = bufio.NewReader(os.Stdin)
)

func nthArg(n int) func(...interface{}) interface{} {
	return func(arg ...interface{}) interface{} {
		return arg[n]
	}
}

func unique(b *[]int) {
	a := *b
	n := len(a)
	if n > 0 {
		sort.Ints(a)
		i, j := 1, 0
		for ; i < n; i++ {
			if a[i] != a[j] {
				j++
				a[j] = a[i]
			}
		}
		*b = a[:j+1]
	}
}

func getInt() int {
	x, sign := 0, 1
	ch, _ := __in.ReadByte()
	for ch < '0' || ch > '9' {
		if ch == '-' {
			sign = -1
		}
		ch, _ = __in.ReadByte()
	}
	for ch >= '0' && ch <= '9' {
		x *= 10
		x += int(ch - '0')
		ch, _ = __in.ReadByte()
	}
	return x * sign
}

func getInts() []int {
	tmp := strings.Fields(nthArg(0)(__in.ReadString('\n')).(string))
	ret := make([]int, len(tmp))
	for i := range ret {
		ret[i], _ = strconv.Atoi(tmp[i])
	}
	return ret
}

func getInt64() int64 {
	var x, sign int64 = 0, 1
	ch, _ := __in.ReadByte()
	for ch < '0' || ch > '9' {
		if ch == '-' {
			sign = -1
		}
		ch, _ = __in.ReadByte()
	}
	for ch >= '0' && ch <= '9' {
		x *= 10
		x += int64(ch - '0')
		ch, _ = __in.ReadByte()
	}
	return x * sign
}

func getInts64() []int64 {
	tmp := strings.Fields(nthArg(0)(__in.ReadString('\n')).(string))
	ret := make([]int64, len(tmp))
	for i := range ret {
		ret[i], _ = strconv.ParseInt(tmp[i], 10, 64)
	}
	return ret
}

func getWord() string {
	var (
		buf bytes.Buffer
		ch  byte
	)
	for ch == 0 || ch == ' ' || ch == '\n' {
		ch, _ = __in.ReadByte()
	}
	for ch != 0 && ch != ' ' && ch != '\n' {
		buf.WriteByte(ch)
		ch, _ = __in.ReadByte()
	}
	return buf.String()
}

func getWords() []string {
	return strings.Fields(nthArg(0)(__in.ReadString('\n')).(string))
}

func getLine() string {
	return strings.TrimSpace(nthArg(0)(__in.ReadString('\n')).(string))
}

//-----------------------------------START-----------------------------------//

var (
	n, m, k int
	a       [][]byte
	found   [][]bool
)

func offLimit(r, c int) bool {
	return k == 0 || r < 0 || r >= n || c < 0 || c >= m || a[r][c] != '.' || found[r][c]
}

func dfs(r, c int) {
	if offLimit(r, c) {
		return
	}
	found[r][c] = true
	dfs(r+1, c)
	dfs(r, c+1)
	dfs(r-1, c)
	dfs(r, c-1)
	if k > 0 {
		a[r][c] = 'X'
		k--
	}
}

func main() {
	n, m, k = getInt(), getInt(), getInt()
	a = make([][]byte, n)
	found = make([][]bool, n)
	for i := range a {
		a[i] = []byte(getLine())
		found[i] = make([]bool, m)
	}
	for i := range a {
		for j := range a[i] {
			dfs(i, j)
		}
	}
	for i := 0; i < n; i++ {
		fmt.Printf("%s\n", string(a[i]))
	}
}

package main

import (
	"bufio"
	"bytes"
	"fmt"
	"os"
	"sort"
	"strconv"
	"strings"
)

var (
	__in = bufio.NewReader(os.Stdin)
)

func nthArg(n int) func(...interface{}) interface{} {
	return func(arg ...interface{}) interface{} {
		return arg[n]
	}
}

func unique(b *[]int) {
	a := *b
	n := len(a)
	if n > 0 {
		sort.Ints(a)
		i, j := 1, 0
		for ; i < n; i++ {
			if a[i] != a[j] {
				j++
				a[j] = a[i]
			}
		}
		*b = a[:j+1]
	}
}

func getInt() int {
	x, sign := 0, 1
	ch, _ := __in.ReadByte()
	for ch < '0' || ch > '9' {
		if ch == '-' {
			sign = -1
		}
		ch, _ = __in.ReadByte()
	}
	for ch >= '0' && ch <= '9' {
		x *= 10
		x += int(ch - '0')
		ch, _ = __in.ReadByte()
	}
	return x * sign
}

func getIntSlice() []int {
	tmp := strings.Fields(nthArg(0)(__in.ReadString('\n')).(string))
	ret := make([]int, len(tmp))
	for i := range ret {
		ret[i], _ = strconv.Atoi(tmp[i])
	}
	return ret
}

func scanInts(a ...*int) {
	b := getIntSlice()
	for i := range a {
		*a[i] = b[i]
	}
}

func getInt64() int64 {
	var x, sign int64 = 0, 1
	ch, _ := __in.ReadByte()
	for ch < '0' || ch > '9' {
		if ch == '-' {
			sign = -1
		}
		ch, _ = __in.ReadByte()
	}
	for ch >= '0' && ch <= '9' {
		x *= 10
		x += int64(ch - '0')
		ch, _ = __in.ReadByte()
	}
	return x * sign
}

func getInt64Slice() []int64 {
	tmp := strings.Fields(nthArg(0)(__in.ReadString('\n')).(string))
	ret := make([]int64, len(tmp))
	for i := range ret {
		ret[i], _ = strconv.ParseInt(tmp[i], 10, 64)
	}
	return ret
}

func scanInts64(a ...*int64) {
	b := getInt64Slice()
	for i := range a {
		*a[i] = b[i]
	}
}

func getWord() string {
	var (
		buf bytes.Buffer
		ch  byte
	)
	for ch == 0 || ch == ' ' || ch == '\n' {
		ch, _ = __in.ReadByte()
	}
	for ch != 0 && ch != ' ' && ch != '\n' {
		buf.WriteByte(ch)
		ch, _ = __in.ReadByte()
	}
	return buf.String()
}

func getWordSlice() []string {
	return strings.Fields(nthArg(0)(__in.ReadString('\n')).(string))
}

func scanWords(a ...*string) {
	b := getWordSlice()
	for i := range a {
		*a[i] = b[i]
	}
}

func getLine() string {
	return strings.TrimSpace(nthArg(0)(__in.ReadString('\n')).(string))
}

//-----------------------------------START-----------------------------------//

const (
	N = 507
	M = 507
)

var (
	n, m, k int
	a       [N][]byte
	found   [N][M]bool
)

func offLimit(r, c int) bool {
	return k == 0 || r < 0 || r >= n || c < 0 || c >= m || a[r][c] != '.' || found[r][c]
}

func dfs(r, c int) {
	if offLimit(r, c) {
		return
	}
	found[r][c] = true
	dfs(r+1, c)
	dfs(r, c+1)
	dfs(r-1, c)
	dfs(r, c-1)
	if k > 0 {
		a[r][c] = 'X'
		k--
	}
}

func main() {
	scanInts(&n, &m, &k)
	fmt.Println(n, m, k)
	for i := 0; i < n; i++ {
		a[i] = []byte(getLine())
	}
	for i := range a {
		for j := range a[i] {
			dfs(i, j)
		}
	}
	for i := 0; i < n; i++ {
		fmt.Printf("%s\n", string(a[i]))
	}
}


William arrived at a conference dedicated to cryptocurrencies. Networking, meeting new people, and using friends' connections are essential to stay up to date with the latest news from the world of cryptocurrencies.The conference has $$$n$$$ participants, who are initially unfamiliar with each other. William can introduce any two people, $$$a$$$ and $$$b$$$, who were not familiar before, to each other. William has $$$d$$$ conditions, $$$i$$$'th of which requires person $$$x_i$$$ to have a connection to person $$$y_i$$$. Formally, two people $$$x$$$ and $$$y$$$ have a connection if there is such a chain $$$p_1=x, p_2, p_3, \dots, p_k=y$$$ for which for all $$$i$$$ from $$$1$$$ to $$$k - 1$$$ it's true that two people with numbers $$$p_i$$$ and $$$p_{i + 1}$$$ know each other.For every $$$i$$$ ($$$1 \le i \le d$$$) William wants you to calculate the maximal number of acquaintances one person can have, assuming that William satisfied all conditions from $$$1$$$ and up to and including $$$i$$$ and performed exactly $$$i$$$ introductions. The conditions are being checked after William performed $$$i$$$ introductions. The answer for each $$$i$$$ must be calculated independently. It means that when you compute an answer for $$$i$$$, you should assume that no two people have been introduced to each other yet.

The first line contains two integers $$$n$$$ and $$$d$$$ ($$$2 \le n \le 10^3, 1 \le d \le n - 1$$$), the number of people, and number of conditions, respectively. Each of the next $$$d$$$ lines each contain two integers $$$x_i$$$ and $$$y_i$$$ ($$$1 \le x_i, y_i \le n, x_i \neq y_i$$$), the numbers of people which must have a connection according to condition $$$i$$$.

Output $$$d$$$ integers. $$$i$$$th number must equal the number of acquaintances the person with the maximal possible acquaintances will have, if William performed $$$i$$$ introductions and satisfied the first $$$i$$$ conditions.

NoteThe explanation for the first test case:In this explanation, the circles and the numbers in them denote a person with the corresponding number. The line denotes that William introduced two connected people. The person marked with red has the most acquaintances. These are not the only correct ways to introduce people.

package main

import (
	"bufio"
	. "fmt"
	"io"
	"os"
	"sort"
)

// github.com/EndlessCheng/codeforces-go
func CF1609D(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

	var n, d, v, w, ex int
	Fscan(in, &n, &d)
	fa := make([]int, n+1)
	sz := make([]int, n+1)
	for i := range fa {
		fa[i] = i
		sz[i] = 1
	}
	var find func(int) int
	find = func(x int) int {
		if fa[x] != x {
			fa[x] = find(fa[x])
		}
		return fa[x]
	}

	for ; d > 0; d-- {
		Fscan(in, &v, &w)
		v, w = find(v), find(w)
		if v == w {
			ex++
		} else {
			if sz[v] > sz[w] {
				v, w = w, v
			}
			sz[w] += sz[v]
			sz[v] = 0
			fa[v] = w
		}
		a := append([]int(nil), sz...)
		sort.Ints(a) // or kth
		s := -1
		for i := n; i >= n-ex; i-- {
			s += a[i]
		}
		Fprintln(out, s)
	}
}

func main() { CF1609D(os.Stdin, os.Stdout) }

package main

import (
	"bufio"
	. "fmt"
	"io"
	"os"
	"sort"
)

// github.com/EndlessCheng/codeforces-go
func CF1609D(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

	var n, d, v, w, ex int
	Fscan(in, &n, &d)
	fa := make([]int, n+1)
	for i := range fa {
		fa[i] = i
	}
	var find func(int) int
	find = func(x int) int {
		if fa[x] != x {
			fa[x] = find(fa[x])
		}
		return fa[x]
	}

	for ; d > 0; d-- {
		Fscan(in, &v, &w)
		v, w = find(v), find(w)
		if v == w {
			ex++
		} else {
			fa[v] = w
		}
		cnt := map[int]int{}
		for i := 1; i <= n; i++ {
			cnt[find(i)]++
		}
		m := len(cnt)
		a := make([]int, 0, m)
		for _, v := range cnt {
			a = append(a, v)
		}
		sort.Ints(a)
		s := -1
		for i := m - 1; i >= m-1-ex && i >= 0; i-- {
			s += a[i]
		}
		Fprintln(out, s)
	}
}

func main() { CF1609D(os.Stdin, os.Stdout) }

package main

import (
	"fmt"
	"sort"
)

var res = 0

func main() {
	var n, d int
	fmt.Scan(&n, &d)
	pr := make([]int, 1001, 1001)
	sz := make([]int, 1001, 1001)
	for i := 1; i <= n; i++ {
		makeSet(i, pr, sz)
	}
	for i := 1; i <= d; i++ {
		var u, v int
		fmt.Scan(&u)
		fmt.Scan(&v)
		unionSet(u, v, pr, sz)
		a := append(make([]int, 0, len(sz)), sz...)

		sort.Ints(a)

		var ans = 0
		for j := 0; j <= res; j++ {
			ans += a[1000-j] - 1
		}
		fmt.Println(ans + res)
	}
}

func makeSet(v int, pr []int, sz []int) {
	pr[v] = v
	sz[v] = 1
}

func findSet(v int, pr []int) int {
	if pr[v] == v {
		return v
	}
	return findSet(pr[v], pr)
}

func unionSet(u int, v int, pr []int, sz []int) {
	u = findSet(u, pr)
	v = findSet(v, pr)
	if sz[u] < sz[v] {
		u, v = v, u
	}
	if u != v {
		sz[u] += sz[v]
		pr[v] = u
		sz[v] = 0
	} else {
		res++
	}
}

package main

import (
	"bufio"
	. "fmt"
	"os"
	"sort"
)

func pa(a int) int{
	if arr[a] == a{
		return a
	}
	pa := pa(arr[a])
	arr[a] = pa
	return pa
}

func un(u,v int) bool{
	pu := pa(u)
	pv := pa(v)
	if pu == pv{
		return false
	}
	arr[pv] = pu
	cnt[pu] += cnt[pv]
	cnt[pv] = 0
	return true
}
var arr []int
var cnt []int

func solve(in *bufio.Reader, out *bufio.Writer){
	defer out.Flush()
	var n,q int
	Fscan(in,&n,&q)
	for i := 0;i <= n;i++{
		arr = append(arr,i)
		cnt = append(cnt,1)
	}
	c := 1
	for i := 0;i < q;i++{
		var u,v int
		Fscan(in,&u,&v)
		if !un(u,v) && c < n{
			c++
		}
		tmp := make([]int,n + 1)
		copy(tmp,cnt)
		sort.Ints(tmp)
		sum := 0
		for j := n - c + 1;j <= n;j++{
			sum += tmp[j]
		}
		Fprintln(out,sum - 1)
	}
}

func main(){
	stdin := os.Stdin
	stdout := os.Stdout
	in := bufio.NewReader(stdin)
	out := bufio.NewWriter(stdout)
	n := 1
	//Fscan(in,&n)
	for i := 0;i < n;i++{
		solve(in,out)
	}
}

package main

import (
	"bufio"
	. "fmt"
	"io"
	"os"
	"sort"
)

// github.com/EndlessCheng/codeforces-go
func CF1609D(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

	var n, d, v, w, ex int
	Fscan(in, &n, &d)
	fa := make([]int, n+1)
	sz := make([]int, n+1)
	for i := range fa {
		fa[i] = i
		sz[i] = 1
	}
	var find func(int) int
	find = func(x int) int {
		if fa[x] != x {
			fa[x] = find(fa[x])
		}
		return fa[x]
	}

	for ; d > 0; d-- {
		Fscan(in, &v, &w)
		v, w = find(v), find(w)
		if v == w {
			ex++
		} else {
			if sz[v] > sz[w] {
				v, w = w, v
			}
			sz[w] += sz[v]
			fa[v] = w
		}
		a := append([]int(nil), sz...)
		sort.Ints(a) // or kth
		s := -1
		for i := n; i >= n-ex; i-- {
			s += a[i]
		}
		Fprintln(out, s)
	}
}

func main() { CF1609D(os.Stdin, os.Stdout) }

package main

import (
	"fmt"
	"sort"
)

var res = 0

func main() {
	var n, d int
	fmt.Scan(&n, &d)
	pr := make([]int, 1001, 1001)
	sz := make([]int, 1001, 1001)
	for i := 1; i <= n; i++ {
		makeSet(i, pr, sz)
	}
	for i := 1; i <= n; i++ {
		var u, v int
		fmt.Scan(&u)
		fmt.Scan(&v)
		unionSet(u, v, pr, sz)
		a := append(make([]int, 0, len(sz)), sz...)

		sort.Ints(a)

		var ans = 0
		for j := 0; j <= res; j++ {
			ans += a[1000-j] - 1
		}
		fmt.Println(ans + res)
	}
}

func makeSet(v int, pr []int, sz []int) {
	pr[v] = v
	sz[v] = 1
}

func findSet(v int, pr []int) int {
	if pr[v] == v {
		return v
	}
	return findSet(pr[v], pr)
}

func unionSet(u int, v int, pr []int, sz []int) {
	u = findSet(u, pr)
	v = findSet(v, pr)
	if sz[u] < sz[v] {
		u, v = v, u
	}
	if u != v {
		sz[u] += sz[v]
		pr[v] = u
		sz[v] = 0
	} else {
		res++
	}
}


Pupils decided to go to amusement park. Some of them were with parents. In total, n people came to the park and they all want to get to the most extreme attraction and roll on it exactly once.Tickets for group of x people are sold on the attraction, there should be at least one adult in each group (it is possible that the group consists of one adult). The ticket price for such group is c1 + c2·(x - 1)2 (in particular, if the group consists of one person, then the price is c1). All pupils who came to the park and their parents decided to split into groups in such a way that each visitor join exactly one group, and the total price of visiting the most extreme attraction is as low as possible. You are to determine this minimum possible total price. There should be at least one adult in each group.

The first line contains three integers n, c1 and c2 (1 ≤ n ≤ 200 000, 1 ≤ c1, c2 ≤ 107) — the number of visitors and parameters for determining the ticket prices for a group. The second line contains the string of length n, which consists of zeros and ones. If the i-th symbol of the string is zero, then the i-th visitor is a pupil, otherwise the i-th person is an adult. It is guaranteed that there is at least one adult. It is possible that there are no pupils.

Print the minimum price of visiting the most extreme attraction for all pupils and their parents. Each of them should roll on the attraction exactly once.

NoteIn the first test one group of three people should go to the attraction. Then they have to pay 4 + 1 * (3 - 1)2 = 8.In the second test it is better to go to the attraction in two groups. The first group should consist of two adults (for example, the first and the second person), the second should consist of one pupil and one adult (the third and the fourth person). Then each group will have a size of two and for each the price of ticket is 7 + 2 * (2 - 1)2 = 9. Thus, the total price for two groups is 18.

package main

import "fmt"

func main() {
	var n int
	var c1 uint64;
	var c2 uint64;
	fmt.Scan(&n)
	fmt.Scan(&c1)
	fmt.Scan(&c2)
    var s string
    fmt.Scan(&s)
    var adult int;
    adult = 0;
    var pupil int;
    pupil = 0;
	for i := 0; i < n; i++ {
		if s[i] == '0' {
			pupil++;
		} else {
			adult++;
		}
	}

    var ans uint64;
    ans = 1;
for i := 1; i <= 63; i++ {
ans = ans * 2;
}
	for  i := 1; i <= adult; i++ {
var lft uint64;
var g uint64;
g = uint64(i);

lft = uint64(n) - g;

var md uint64 = lft % g;
var p uint64;
p = md * (lft / g + 1) * (lft / g + 1) + (g - md) * (lft / g) * (lft / g);
p = c2 * p + g * c1;
        if p < ans {
ans = p;
}
    }
    fmt.Println(ans);
}

package main

import (
    "fmt"
    "bufio"
    "os"
)

func Readln(r *bufio.Reader) (string) {
  var (isPrefix bool = true
       err error = nil
       line, ln []byte
      )
  for isPrefix && err == nil {
      line, isPrefix, err = r.ReadLine()
      ln = append(ln, line...)
  }
  return string(ln)
}

func main() {
	var n, c1, c2 int
	fmt.Scan(&n, &c1, &c2)
	reader := bufio.NewReader(os.Stdin)
    s := Readln(reader)
    s = Readln(reader)
    n0, n1 := 0, 0
    for i:=0; i<n; i++ {
        if s[i]=='0' {
            n0++;
        } else {
            n1++;
        }
    }
    var ret int64
    for i:=1; i<=n1; i++ {
        var p, q int
        p = n / i;
        q = n % i;
        var cur int64
        cur = int64(i-q)*(int64(c1)+int64(c2)*int64(p-1)*int64(p-1)) + int64(q)*(int64(c1)+int64(c2)*int64(p)*int64(p));
        if i==1 || cur<ret { ret=cur }
    }
    fmt.Println(ret)
}

package main

import "fmt"

func main() {
	var n int
	var c1 int64
	var c2 int64
	var s string
	fmt.Scan(&n)
	fmt.Scan(&c1)
	fmt.Scan(&c2)
	fmt.Scan(&s)
	a := 0
	for i := 0; i < n; i++ {
	    if s[i] == '1' {
	        a++
	    }
	}
	ans := int64(-1);
	for i := 1; i <= a; i++ {
	    u := int64((n - i) / i);
	    v := int64((n - i) % i);
	    loc_ans := int64(i) * c1 + (v * (u + 1) * (u + 1) + (int64(i) - v) * u * u) * c2
	    if ans == -1 || loc_ans < ans {
	        ans = loc_ans
	    }
	}
    fmt.Println(ans)
}


Andrew has $$$n$$$ piles with stones. The $$$i$$$-th pile contains $$$a_i$$$ stones. He wants to make his table clean so he decided to put every stone either to the $$$1$$$-st or the $$$n$$$-th pile.Andrew can perform the following operation any number of times: choose $$$3$$$ indices $$$1 \le i < j < k \le n$$$, such that the $$$j$$$-th pile contains at least $$$2$$$ stones, then he takes $$$2$$$ stones from the pile $$$j$$$ and puts one stone into pile $$$i$$$ and one stone into pile $$$k$$$. Tell Andrew what is the minimum number of operations needed to move all the stones to piles $$$1$$$ and $$$n$$$, or determine if it's impossible.

The input contains several test cases. The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. The first line for each test case contains one integer $$$n$$$ ($$$3 \leq n \leq 10^5$$$) — the length of the array. The second line contains a sequence of integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 10^9$$$) — the array elements. It is guaranteed that the sum of the values $$$n$$$ over all test cases does not exceed $$$10^5$$$.

For each test case print the minimum number of operations needed to move stones to piles $$$1$$$ and $$$n$$$, or print $$$-1$$$ if it's impossible.

NoteIn the first test case, it is optimal to do the following:   Select $$$(i, j, k) = (1, 2, 5)$$$. The array becomes equal to $$$[2, 0, 2, 3, 7]$$$.  Select $$$(i, j, k) = (1, 3, 4)$$$. The array becomes equal to $$$[3, 0, 0, 4, 7]$$$.  Twice select $$$(i, j, k) = (1, 4, 5)$$$. The array becomes equal to $$$[5, 0, 0, 0, 9]$$$. This array satisfy the statement, because every stone is moved to piles $$$1$$$ and $$$5$$$.  There are $$$4$$$ operations in total.In the second test case, it's impossible to put all stones into piles with numbers $$$1$$$ and $$$3$$$:   At the beginning there's only one possible operation with $$$(i, j, k) = (1, 2, 3)$$$. The array becomes equal to $$$[2, 1, 2]$$$.  Now there is no possible operation and the array doesn't satisfy the statement, so the answer is $$$-1$$$. In the third test case, it's optimal to do the following:   Select $$$(i, j, k) = (1, 2, 3)$$$. The array becomes equal to $$$[2, 0, 2]$$$. This array satisfies the statement, because every stone is moved to piles $$$1$$$ and $$$3$$$.  The is $$$1$$$ operation in total.In the fourth test case, it's impossible to do any operation, and the array doesn't satisfy the statement, so the answer is $$$-1$$$.

package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()

	var T int
	fmt.Fscan(in, &T)
	for t := 0; t < T; t++ {
		var n int
		fmt.Fscan(in, &n)

		arr := make([]int64, n)
		var o, e, max int64
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &arr[i])
			if i > 0 && i < n-1 {
				if arr[i] > max {
					max = arr[i]
				}

				if arr[i]%2 == 0 {
					e++
				} else {
					o++
				}
			}
		}

		if (e == 0 && o == 1) || max <= 1 {
			fmt.Fprintln(out, -1)
		} else {
			var c int64

			for i := 1; i < n-1; i++ {
				c += arr[i] / 2
				if arr[i]%2 != 0 {
					c++
				}
			}

			fmt.Fprintln(out, c)
		}
	}
}

//nolint
package main

import (
	"bufio"
	"fmt"
	"os"
)

func Main() {
	var t, n, tp int
	scan(&t)
	for ; t > 0; t-- {
		scan(&n)
		numOdd := 0
		sum := int64(0)
		for i := 0; i < n; i++ {
			scan(&tp)
			if i == 0 || i == n-1 {
				continue
			}
			sum += int64(tp)
			if tp%2 == 1 {
				numOdd++
			}
		}
		if sum == int64(n-2) || (n == 3 && sum%2 == 1) {
			println("-1")
		} else {
			println((sum + int64(numOdd)) / 2)
		}
	}
}

//{
var reader = bufio.NewReader(os.Stdin)
var writer = bufio.NewWriter(os.Stdout)

func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }
func scanf(f string, a ...interface{})  { fmt.Fscanf(reader, f, a...) }
func print(a ...interface{})            { fmt.Fprint(writer, a...) }
func println(a ...interface{})          { fmt.Fprintln(writer, a...) }
func scan(a ...interface{})             { fmt.Fscan(reader, a...) }

func main() {
	defer writer.Flush()

	Main()
}

//}

package main

import (
	"bufio"
	"fmt"
	"os"
)

var x int

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t int
	fmt.Fscan(in, &t)
	for t > 0 {
		var n int
		fmt.Fscan(in, &n)
		nums := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &nums[i])
		}
		res := int64(0)
		sum := int64(0)
		evens := 0
		ones := 0
		for i := 1; i < n-1; i++ {
			if nums[i]%2 == 0 {
				evens++
			}
			if nums[i] == 1 {
				ones++
			}
			sum += int64(nums[i])
		}
		if evens == 0 && n == 3 {
			fmt.Fprintln(out, -1)
		} else if ones == n-2 {
			fmt.Fprintln(out, -1)
		} else {
			res += int64(n - 2 - evens)
			res += int64(sum-int64(n-2-evens)) / int64(2)
			fmt.Fprintln(out, res)
		}
		t--
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

var (
	in  = bufio.NewReader(os.Stdin)
	out = bufio.NewWriter(os.Stdout)
)

func flush()                            { out.Flush() }
func scan(a ...interface{})             { fmt.Fscan(in, a...) }
func printf(f string, a ...interface{}) { fmt.Fprintf(out, f, a...) }
func assert(f bool) {
	if !f {
		panic("gg")
	}
}

// ----------------------------- TEMPLATE END ----------------------------------

func main() {
	defer flush()

	var ntc int
	scan(&ntc)
	for t := 0; t < ntc; t++ {
		var n int
		scan(&n)
		a := make([]int, n)
		var ans int64 = 0
		can := false
		for i := 0; i < n; i++ {
			scan(&a[i])
			if i == 0 || i == n-1 {
				continue
			}
			if a[i] > 1 {
				can = true
			}
			ans += int64((a[i] + 1) / 2)
		}
		if n == 3 && a[1]%2 == 1 {
			can = false
		}
		can = can
		if !can {
			ans = -1
		}
		printf("%d\n", ans)
	}
}

/*
https://codeforces.com/contest/1637/problem/C
*/

package main

import (
	"bufio"
	"fmt"
	//"math"
	"os"
	"strconv"
	"strings"
)

func main() {
	reader := bufio.NewReader(os.Stdin)
	line1, _ := reader.ReadString('\n')
	line1_arr := strings.Fields(line1)
	//fmt.Println(line1_arr)
	T, _ := strconv.Atoi(line1_arr[0])
	// fmt.Println("T:", T)
	for t := 0; t < T; t++ {
		line2, _ := reader.ReadString('\n')
		line2_arr := strings.Fields(line2)
		N, _ := strconv.Atoi(line2_arr[0])
		// fmt.Println("N: ", N)
		line3, _ := reader.ReadString('\n')
		//fmt.Println("line3: ", line3)
		line3_arr := strings.Fields(line3)
		A := make([]int64, N)
		for n := 0; n < N; n++ {
			v, _ := strconv.Atoi(line3_arr[n])
			A[n] = int64(v)
		}
		// fmt.Println("A:", A)

		res := int64(-1)
		if N == 3 && A[1] %2 != 0 {
			fmt.Println(res)
			continue
		}

		sum := int64(0)
		atleastone_even := false
		atleastoneodd_morethan_2 := false
		for i:=1; i<N-1; i++{
			sum+=A[i]
			if A[i] % 2 != 0 {
				sum +=1
				if A[i] > 2 {
					atleastoneodd_morethan_2 = true
				}
			} else {
				atleastone_even = true
			}
		}

		if atleastone_even || atleastoneodd_morethan_2 {
			res = sum/2
		}
		fmt.Println(res)
	}

}

package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
)

/*
7
7
2 4 1 3 5 1 4
4
1 1 1 1
4
1 2 3 0
5
0 1 2 3 0
5
0 1 1 2 0
5
0 1 2 1 0
6
0 1 1 1 6 0
---
9 = 2 + 1 + 2 + 1 + 3
-1
3
4
3
3
6
========
2
4
0 0 0 0
7
0 5 1 1 1 4 0
---
0
8

0 5 1 1 1 4 0
0 3 2 1 1 4 0
0 1 2 2 1 4 0
0 2 0 2 1 4 0
0 0 0 2 1 4 0
0 0 0 0 1 4 0
0 0 0 0 2 2 0
0 0 0 0 0 2 0
0 0 0 0 0 0 0

*/

//goland:noinspection GoUnhandledErrorResult
func main() {
	defer writer.Flush()

	t := 1
	scanf("%d\n", &t)

	for ; t > 0; t-- {
		var n int
		scanf("%d\n", &n)

		a := make([]int, n)

		for i := range a {
			scanf("%d", &a[i])
		}
		scanf("\n")

		if (n == 3 && a[1]%2 != 0) || (max(a[1:n-1]...) == 1) {
			fmt.Fprintln(writer, -1)
			continue
		}

		minCost := int64(0)

		for i := 1; i < n-1; i++ {
			num := a[i]
			minCost += int64(divCeil(num, 2))
		}

		fmt.Fprintln(writer, minCost)
	}
}

const maxUint = ^uint(0)

//goland:noinspection GoUnusedConst
const minUint = 0
const maxInt = int(maxUint >> 1)

//goland:noinspection GoUnusedConst
const minInt = -maxInt - 1

var reader = bufio.NewReader(os.Stdin)
var writer = bufio.NewWriter(os.Stdout)

func scanf(f string, a ...interface{}) {
	if _, err := fmt.Fscanf(reader, f, a...); err != nil {
		panic(err)
	}
}

//goland:noinspection GoUnusedFunction
func reversed(s string) string {
	input := []rune(s)
	li := len(input)
	ret := make([]rune, li)
	for i, r := range input {
		ret[li-i-1] = r
	}
	return string(ret)
}

//goland:noinspection GoUnusedFunction
func divCeil(num, div int) int {
	quot := num / div
	if num%div != 0 {
		quot++
	}
	return quot
}

//goland:noinspection GoUnusedFunction
func min(nums ...int) int {
	minimum := nums[0]
	for _, num := range nums {
		if num < minimum {
			minimum = num
		}
	}
	return minimum
}

//goland:noinspection GoUnusedFunction
func max(nums ...int) int {
	maximum := nums[0]
	for _, num := range nums {
		if num > maximum {
			maximum = num
		}
	}
	return maximum
}

//goland:noinspection GoUnusedFunction
func abs(num int) int {
	if num < 0 {
		return -num
	}
	return num
}

//goland:noinspection GoUnusedFunction
func isPrime(num int) bool {
	if num == 2 || num == 3 {
		return true
	}

	if num <= 1 || num%2 == 0 || num%3 == 0 {
		return false
	}

	for i := 5; i*i <= num; i += 6 {
		if num%i == 0 || num%(i+2) == 0 {
			return false
		}
	}

	return true
}

//goland:noinspection GoUnusedFunction
func gcd(a, b int) int {
	for b != 0 {
		a, b = b, a%b
	}
	return a
}

//goland:noinspection GoUnusedFunction
func lcm(a, b int) int {
	return abs(a) / gcd(a, b) * abs(b)
}

// Examples:
//	digitAt(1234, -1) -> panic!
//	digitAt(1234, 0) -> 4
//	digitAt(1234, 1) -> 3
//	digitAt(1234, 2) -> 2
//	digitAt(1234, 3) -> 1
//	digitAt(1234, 4) -> panic!
//goland:noinspection GoUnusedFunction
func digitAt(num, pos int) int {
	if num == 0 {
		return 0
	}

	num = abs(num)

	lenNum := int(math.Log10(float64(num))) + 1
	if pos < 0 || pos >= lenNum {
		err := fmt.Sprintf("Out of bound access for digit. Position passed %d, number %d", pos, num)
		panic(err)
	}

	rem := num % int(math.Pow10(pos+1))
	return rem / int(math.Pow10(pos))
}

package main

import (
	"bufio"
	"bytes"
	"fmt"
	"os"
)

const (
	MOD int64 = 1e9+7
	NL byte = byte('\n')
	SP byte = byte(' ')
)

var (
	in *bufio.Scanner
	out *bufio.Writer

	n int
	a [100000]int64
	res int64
)

func main() {
	initBuffer()
	var t int = 1
	t = getInt()
	for i := 0; i < t; i++ {
		solve()
	}
	// test()
	out.Flush()
}

func solve() {
	initialize()
	input()
	count()
	output()
}

func initialize() {

}

func input() {
	n = getInt()
	for i := 0; i < n; i++ {
		a[i] = getInt64()
	}
}

func count() {
	res = 0
	var numE, numB bool
	for i := 1; i < n - 1; i++ {
		res += a[i] / 2  + a[i] % 2
		numE = numE || (a[i] % 2 == 1)
		numB = numB || (a[i] >= 2)
	}
	if numE && (!numB || n == 3) {
		res = -1
	}
}

func output() {
	out.Write(int64ToBytes(res))
	out.WriteByte(NL)
}

func test() {
	fmt.Print()
}

func initBuffer() {
	in = bufio.NewScanner(os.Stdin)
	in.Buffer([]byte{}, 4 * 1024 * 1024)
	in.Split(scanWords)
	out = bufio.NewWriter(os.Stdout)
}

func scanWords(data []byte, atEOF bool) (advance int, token []byte, err error) {
	if atEOF && len(data) == 0 {
		return 0, nil, nil
	}
	i := bytes.IndexByte(data, '\n')
	j := bytes.IndexByte(data, ' ')
	ind := min(i, j)
	if ind < 0 {
		ind = max(i, j)
	}
	if ind >= 0 {
		return ind + 1, dropCR(data[0:ind]), nil
	}
	if atEOF {
		return len(data), dropCR(data), nil
	}
	return 0, nil, nil
}

func dropCR(data []byte) []byte {
	if len(data) > 0 && data[len(data)-1] == '\r' {
		return data[:len(data)-1]
	}
	return data
}

func getFloat() float64 {
	in.Scan()
	b := in.Bytes()
	var isNeg bool
	if b[0] == '-' {
		isNeg = true
		b = b[1:]
	}
	var res1 int64
	for i := 0; i < len(b); i++ {
		if b[i] == '.' {
			b = b[i+1:]
			break
		}
		res1 *= 10
		res1 += int64(b[i]) - 48
		if i == len(b) - 1 {
			b = b[:0]
		}
	}
	var res2 float64
	for i := len(b); i > 0; i-- {
		res2 += float64(b[i-1]) - 48
		res2 /= 10
	}
	if isNeg {
		res1 *= -1
		res2 *= -1
	}
	return float64(res1) + res2
}

func getInt() int {
	in.Scan()
	b := in.Bytes()
	var isNeg bool
	if b[0] == '-' {
		isNeg = true
		b = b[1:]
	}
	var res int
	for i := 0; i < len(b); i++ {
		res *= 10
		res += int(b[i]) - 48
	}
	if isNeg {
		res *= -1
	}
	return res
}

func getInt64() int64 {
	in.Scan()
	b := in.Bytes()
	var isNeg bool
	if b[0] == '-' {
		isNeg = true
		b = b[1:]
	}
	var res int64
	for i := 0; i < len(b); i++ {
		res *= 10
		res += int64(b[i]) - 48
	}
	if isNeg {
		res *= -1
	}
	return res
}

func getString() string {
	in.Scan()
	return in.Text()
}

func intToBytes(n int) []byte {
	var res [33]byte
	var isNeg bool
	if n < 0 {
		isNeg = true
		n = -n
	}
	var l int = 33
	for n > 0 {
		l--
		res[l] = byte(n % 10 + 48)
		n /= 10
	}
	if l == 33 {
		l--
		res[l] = byte(48)
	}
	if isNeg {
		l--
		res[l] = byte(45)
	}
	return res[l:]
}

func int64ToBytes(n int64) []byte {
	var res [65]byte
	var isNeg bool
	if n < 0 {
		isNeg = true
		n = -n
	}
	var l int = 65
	for n > 0 {
		l--
		res[l] = byte(n % 10 + 48)
		n /= 10
	}
	if l == 65 {
		l--
		res[l] = byte(48)
	}
	if isNeg {
		l--
		res[l] = byte(45)
	}
	return res[l:]
}

func bsLower(arr []int, n int) int {
	var minN, midN, maxN int
	maxN = len(arr)
	for maxN - 1 > minN {
		midN = (minN + maxN) / 2
		if arr[midN] < n {
			minN = midN
		} else {
			maxN = midN
		}
	}
	if arr[minN] >= n {
		return minN
	}
	return maxN
}

func bsLower64(arr []int64, n int64) int {
	var minN, midN, maxN int
	maxN = len(arr)
	for maxN - 1 > minN {
		midN = (minN + maxN) / 2
		if arr[midN] < n {
			minN = midN
		} else {
			maxN = midN
		}
	}
	if arr[minN] >= n {
		return minN
	}
	return maxN
}

func bsUpper(arr []int, n int) int {
	var minN, midN, maxN int
	maxN = len(arr)
	for maxN - 1 > minN {
		midN = (minN + maxN) / 2
		if arr[midN] <= n {
			minN = midN
		} else {
			maxN = midN
		}
	}
	if arr[minN] > n {
		return minN
	}
	return maxN
}

func bsUpper64(arr []int64, n int64) int {
	var minN, midN, maxN int
	maxN = len(arr)
	for maxN - 1 > minN {
		midN = (minN + maxN) / 2
		if arr[midN] <= n {
			minN = midN
		} else {
			maxN = midN
		}
	}
	if arr[minN] > n {
		return minN
	}
	return maxN
}

func max(n1, n2 int) int {
	if n1 > n2 {
		return n1
	}
	return n2
}

func max64(n1, n2 int64) int64 {
	if n1 > n2 {
		return n1
	}
	return n2
}

func min(n1, n2 int) int {
	if n1 < n2 {
		return n1
	}
	return n2
}

func min64(n1, n2 int64) int64 {
	if n1 < n2 {
		return n1
	}
	return n2
}

func yes(res, isUpperCase bool) bool {
	var arr [4]string = [4]string{"No", "Yes", "NO", "YES"}
	var ind int
	if res == true {
		ind = 1
	}
	if isUpperCase == true {
		ind += 2
	}
	out.WriteString(arr[ind])
	out.WriteByte(NL)
	return res
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()

	var T int
	fmt.Fscan(in, &T)
	for t := 0; t < T; t++ {
		var n int
		fmt.Fscan(in, &n)

		arr := make([]int64, n)
		var ones, sum int64
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &arr[i])
			if i > 0 && i < n-1 {
				if arr[i] == 1 {
					ones++
				} else {
					sum += arr[i]
				}
			}
		}

		if (n == 3 && arr[1]%2 != 0) || sum < ones {
			fmt.Fprintln(out, -1)
		} else {
			var c int64
			for i := 1; i < n-1; i++ {
				c += arr[i] / 2
				if arr[i]%2 != 0 {
					c++
				}
			}

			fmt.Fprintln(out, c)
		}
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()

	var T int
	fmt.Fscan(in, &T)
	for t := 0; t < T; t++ {
		var n int
		fmt.Fscan(in, &n)

		arr := make([]int64, n)
		var ones, sum, max int64
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &arr[i])
			if i > 0 && i < n-1 {
				if arr[i] > max {
					max = arr[i]
				}

				if arr[i] == 1 {
					ones++
				} else {
					sum += arr[i]
				}
			}
		}

		if sum%2 != 0 {
			sum--
		}

		if (n == 3 && arr[1]%2 != 0) || sum < ones || max <= 1 {
			fmt.Fprintln(out, -1)
		} else {
			var c int64
			for i := 1; i < n-1; i++ {
				c += arr[i] / 2
				if arr[i]%2 != 0 {
					c++
				}
			}

			fmt.Fprintln(out, c)
		}
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()

	var T int
	fmt.Fscan(in, &T)
	for t := 0; t < T; t++ {
		var n int
		fmt.Fscan(in, &n)

		arr := make([]int64, n)
		var ones, sum int64
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &arr[i])
			if i > 0 && i < n-1 {
				if arr[i] == 1 {
					ones++
				} else {
					sum += arr[i]
				}
			}
		}

		if sum%2 != 0 {
			sum--
		}

		if (n == 3 && arr[1]%2 != 0) || sum < ones {
			fmt.Fprintln(out, -1)
		} else {
			var c int64
			for i := 1; i < n-1; i++ {
				c += arr[i] / 2
				if arr[i]%2 != 0 {
					c++
				}
			}

			fmt.Fprintln(out, c)
		}
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()

	var T int
	fmt.Fscan(in, &T)
	for t := 0; t < T; t++ {
		var n int
		fmt.Fscan(in, &n)

		arr := make([]int, n)
		var ones, sum int
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &arr[i])
			if i > 0 && i < n-1 {
				if arr[i] == 1 {
					ones++
				} else {
					sum += arr[i]
				}
			}
		}

		if sum%2 != 0 {
			sum--
		}

		if (n == 3 && arr[1]%2 != 0) || sum < ones {
			fmt.Fprintln(out, -1)
		} else {
			var c int
			for i := 1; i < n-1; i++ {
				c += arr[i] / 2
				if arr[i]%2 != 0 {
					c++
				}
			}

			fmt.Fprintln(out, c)
		}
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()

	var T int
	fmt.Fscan(in, &T)
	for t := 0; t < T; t++ {
		var n int
		fmt.Fscan(in, &n)

		arr := make([]int, n)
		var o, e, max int
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &arr[i])
			if i > 0 && i < n-1 {
				if arr[i] > max {
					max = arr[i]
				}

				if arr[i]%2 == 0 {
					e += arr[i]
				} else {
					o++
				}
			}
		}

		if (e < o) || max <= 1 {
			fmt.Fprintln(out, -1)
		} else {
			var c int

			for i := 1; i < n-1; i++ {
				c += arr[i] / 2
				if arr[i]%2 != 0 {
					c++
				}
			}

			fmt.Fprintln(out, c)
		}
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()

	var T int
	fmt.Fscan(in, &T)
	for t := 0; t < T; t++ {
		var n int
		fmt.Fscan(in, &n)

		arr := make([]int, n)
		var o, e, max int
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &arr[i])
			if i > 0 && i < n-1 {
				if arr[i] > max {
					max = arr[i]
				}

				if arr[i]%2 == 0 {
					e++
				} else {
					o++
				}
			}
		}

		if (e == 0 && o == 1) || max <= 1 {
			fmt.Fprintln(out, -1)
		} else {
			var c int

			for i := 1; i < n-1; i++ {
				c += arr[i] / 2
				if arr[i]%2 != 0 {
					c++
				}
			}

			fmt.Fprintln(out, c)
		}
	}
}

//nolint
package main

import (
	"bufio"
	"fmt"
	"os"
)

func Main() {
	var t, n, tp int
	scan(&t)
	for ; t > 0; t-- {
		scan(&n)
		numOdd := 0
		sum := 0
		for i := 0; i < n; i++ {
			scan(&tp)
			if i == 0 || i == n-1 {
				continue
			}
			sum += tp
			if tp%2 == 1 {
				numOdd++
			}
		}
		if sum == n-2 || (n == 3 && sum%2 == 1) {
			println("-1")
		} else {
			println((sum + numOdd) / 2)
		}
	}
}

//{
var reader = bufio.NewReader(os.Stdin)
var writer = bufio.NewWriter(os.Stdout)

func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }
func scanf(f string, a ...interface{})  { fmt.Fscanf(reader, f, a...) }
func print(a ...interface{})            { fmt.Fprint(writer, a...) }
func println(a ...interface{})          { fmt.Fprintln(writer, a...) }
func scan(a ...interface{})             { fmt.Fscan(reader, a...) }

func main() {
	defer writer.Flush()

	Main()
}

//}

//nolint
package main

import (
	"bufio"
	"fmt"
	"os"
)

func Main() {
	var t, n, tp int
	scan(&t)
	for ; t > 0; t-- {
		scan(&n)
		sum := 0
		for i := 0; i < n; i++ {
			scan(&tp)
			if i != 0 && i != n-1 {
				sum += tp
			}
		}
		if sum <= n-2 {
			println("-1")
			continue
		}
		if sum%2 != 0 {
			if n == 3 {
				println("-1")
			} else {
				println(sum/2 + 1)
			}
		} else {
			println(sum / 2)
		}
	}
}

//{
var reader = bufio.NewReader(os.Stdin)
var writer = bufio.NewWriter(os.Stdout)

func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }
func scanf(f string, a ...interface{})  { fmt.Fscanf(reader, f, a...) }
func print(a ...interface{})            { fmt.Fprint(writer, a...) }
func println(a ...interface{})          { fmt.Fprintln(writer, a...) }
func scan(a ...interface{})             { fmt.Fscan(reader, a...) }

func main() {
	defer writer.Flush()

	Main()
}

//}

package main

import (
	"bufio"
	"fmt"
	"os"
)

var x int

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t int
	fmt.Fscan(in, &t)
	for t > 0 {
		var n int
		fmt.Fscan(in, &n)
		nums := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &nums[i])
		}
		res := int64(0)
		sum := int64(0)
		evens := 0
		for i := 1; i < n-1; i++ {
			if nums[i]%2 == 0 {
				evens++
			}
			sum += int64(nums[i])
		}
		if evens == 0 && n == 3 {
			fmt.Fprintln(out, -1)
		} else {
			res += int64(n - 2 - evens)
			res += int64(sum-int64(n-2-evens)) / int64(2)
			fmt.Fprintln(out, res)
		}
		t--
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

var x int

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t int
	fmt.Fscan(in, &t)
	for t > 0 {
		var n int
		fmt.Fscan(in, &n)
		nums := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &nums[i])
		}
		res := int64(0)
		sum := int64(0)
		evens := 0
		for i := 1; i < n-1; i++ {
			if nums[i]%2 == 0 {
				evens++
			}
			sum += int64(nums[i])
		}
		if evens == 0 {
			fmt.Fprintln(out, -1)
		} else {
			res += int64(n - 2 - evens)
			res += int64(sum-int64(n-2-evens)) / int64(2)
			fmt.Fprintln(out, res)
		}
		t--
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

var (
	in  = bufio.NewReader(os.Stdin)
	out = bufio.NewWriter(os.Stdout)
)

func flush()                            { out.Flush() }
func scan(a ...interface{})             { fmt.Fscan(in, a...) }
func printf(f string, a ...interface{}) { fmt.Fprintf(out, f, a...) }
func assert(f bool) {
	if !f {
		panic("gg")
	}
}

// ----------------------------- TEMPLATE END ----------------------------------

func main() {
	defer flush()

	var ntc int
	scan(&ntc)
	for t := 0; t < ntc; t++ {
		var n int
		scan(&n)
		a := make([]int, n)
		var ans int64 = 0
		can := false
		for i := 0; i < n; i++ {
			scan(&a[i])
			if i == 0 || i == n-1 {
				continue
			}
			if a[i]%2 == 0 {
				can = true
			}
			ans += int64((a[i] + 1) / 2)

		}
		if !can {
			ans = -1
		}
		printf("%d\n", ans)
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

var (
	in  = bufio.NewReader(os.Stdin)
	out = bufio.NewWriter(os.Stdout)
)

func flush()                            { out.Flush() }
func scan(a ...interface{})             { fmt.Fscan(in, a...) }
func printf(f string, a ...interface{}) { fmt.Fprintf(out, f, a...) }
func assert(f bool) {
	if !f {
		panic("gg")
	}
}

// ----------------------------- TEMPLATE END ----------------------------------

func main() {
	defer flush()

	var ntc int
	scan(&ntc)
	for t := 0; t < ntc; t++ {
		var n int
		scan(&n)
		a := make([]int, n)
		ans := 0
		can := false
		for i := 0; i < n; i++ {
			scan(&a[i])
			if i == 0 || i == n-1 {
				continue
			}
			if a[i]%2 == 0 {
				can = true
			}
			ans += (a[i] + 1) / 2

		}
		if !can {
			ans = -1
		}
		printf("%d\n", ans)
	}
}

/*
https://codeforces.com/contest/1637/problem/C
*/

package main

import (
	"bufio"
	"fmt"
	//"math"
	"os"
	"strconv"
	"strings"
)

func main() {
	reader := bufio.NewReader(os.Stdin)
	line1, _ := reader.ReadString('\n')
	line1_arr := strings.Fields(line1)
	//fmt.Println(line1_arr)
	T, _ := strconv.Atoi(line1_arr[0])
	// fmt.Println("T:", T)
	for t := 0; t < T; t++ {
		line2, _ := reader.ReadString('\n')
		line2_arr := strings.Fields(line2)
		N, _ := strconv.Atoi(line2_arr[0])
		// fmt.Println("N: ", N)
		line3, _ := reader.ReadString('\n')
		//fmt.Println("line3: ", line3)
		line3_arr := strings.Fields(line3)
		A := make([]int64, N)
		for n := 0; n < N; n++ {
			v, _ := strconv.Atoi(line3_arr[n])
			A[n] = int64(v)
		}
		// fmt.Println("A:", A)

		sum := int64(0)
		atleastone_even := false
		for i:=1; i<N-1; i++{
			sum+=A[i]
			if A[i] % 2 != 0 {
				sum +=1
			} else {
				atleastone_even = true
			}
		}

		res := int64(-1)
		if atleastone_even {
			res = sum/2
		}
		fmt.Println(res)
	}

}


Vasya has the square chessboard of size n × n and m rooks. Initially the chessboard is empty. Vasya will consequently put the rooks on the board one after another.The cell of the field is under rook's attack, if there is at least one rook located in the same row or in the same column with this cell. If there is a rook located in the cell, this cell is also under attack.You are given the positions of the board where Vasya will put rooks. For each rook you have to determine the number of cells which are not under attack after Vasya puts it on the board.

The first line of the input contains two integers n and m (1 ≤ n ≤ 100 000, 1 ≤ m ≤ min(100 000, n2)) — the size of the board and the number of rooks.  Each of the next m lines contains integers xi and yi (1 ≤ xi, yi ≤ n) — the number of the row and the number of the column where Vasya will put the i-th rook. Vasya puts rooks on the board in the order they appear in the input. It is guaranteed that any cell will contain no more than one rook.

Print m integer, the i-th of them should be equal to the number of cells that are not under attack after first i rooks are put.

NoteOn the picture below show the state of the board after put each of the three rooks. The cells which painted with grey color is not under the attack.

package main

import (
	"bufio"
	"os"
	"strconv"
)

func main() {
	cin := bufio.NewScanner(os.Stdin)
	cin.Split(bufio.ScanWords)
	cout := bufio.NewWriter(os.Stdout)
	defer cout.Flush()

	cin.Scan()
	n, _ := strconv.Atoi(cin.Text())
	cin.Scan()
	m, _ := strconv.Atoi(cin.Text())

	ans := make([]int64, m)
	xx := make([]bool, n)
	yy := make([]bool, n)
	xs := 0
	ys := 0
	for i := 0; i < m; i++ {
		cin.Scan()
		x, _ := strconv.Atoi(cin.Text())
		x--
		cin.Scan()
		y, _ := strconv.Atoi(cin.Text())
		y--

		if i == 0 {
			ans[i] = int64(n)*int64(n) - (2*int64(n) - 1)
			xs, ys = xs+1, ys+1
			xx[x], yy[y] = true, true
		} else if xx[x] && yy[y] {
			ans[i] = ans[i-1]
		} else if xx[x] {
			ans[i] = ans[i-1] - (int64(n) - int64(xs))
			ys++
			yy[y] = true
		} else if yy[y] {
			ans[i] = ans[i-1] - (int64(n) - int64(ys))
			xs++
			xx[x] = true
		} else {
			ans[i] = ans[i-1] - (int64(n) - int64(xs)) - (int64(n) - int64(ys)) + 1
			xs, ys = xs+1, ys+1
			xx[x], yy[y] = true, true
		}
	}
	for _, a := range ans {
		cout.WriteString(strconv.FormatInt(a, 10))
		cout.WriteByte(' ')
	}

}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

var in = bufio.NewScanner(os.Stdin)

func readInt64() int64 {
	in.Scan()
	n, _ := strconv.ParseInt(in.Text(), 10, 64)
	return n
}

func readInt() int {
	in.Scan()
	n, _ := strconv.Atoi(in.Text())
	return n
}

func main() {
	var out = bufio.NewWriter(os.Stdout)
	defer out.Flush()

	buffer := bufio.NewReaderSize(os.Stdin, 64*1024)
	var n, m, r, c int
	fmt.Fscan(buffer, &n, &m)

	nrow := int64(n)
	ncol := int64(n)

	frow := make([]bool, n)
	fcol := make([]bool, n)
	for i := 0; i < n; i++ {
		frow[i] = true
		fcol[i] = true
	}

	results := make([]int64, m)
	total := int64(n) * int64(n)

	for i := 0; i < m; i++ {
		fmt.Fscan(buffer, &r, &c)
		r--
		c--

		if frow[r] && fcol[c] {
			total -= nrow + ncol - 1
			nrow--
			ncol--
		} else if frow[r] {
			total -= ncol
			nrow--
		} else if fcol[c] {
			total -= nrow
			ncol--
		}

		results[i] = total
		frow[r] = false
		fcol[c] = false
	}

	for _, r := range results {
		out.WriteString(strconv.FormatInt(r, 10))
		out.WriteByte(' ')
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

var n, m, r, c int
var res []int64

func main() {
	bufin := bufio.NewReader(os.Stdin)
	bufout := bufio.NewWriter(os.Stdout)
	defer bufout.Flush()

	fmt.Fscan(bufin, &n, &m)

	nrow := int64(n)
	ncol := int64(n)

	frow := make([]bool, n)
	fcol := make([]bool, n)
	for i := 0; i < n; i++ {
		frow[i] = true
		fcol[i] = true
	}

	res = make([]int64, m)
	total := int64(n) * int64(n)

	for i := 0; i < m; i++ {
		fmt.Fscan(bufin, &r, &c)
		r--
		c--

		if frow[r] && fcol[c] {
			total -= nrow + ncol - 1
			nrow--
			ncol--
		} else if frow[r] {
			total -= ncol
			nrow--
		} else if fcol[c] {
			total -= nrow
			ncol--
		}

		res[i] = total
		frow[r] = false
		fcol[c] = false
	}

	for _, r := range res {
		fmt.Fprint(bufout, r, " ")
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

var in = bufio.NewScanner(os.Stdin)

func readInt64() int64 {
	in.Scan()
	n, _ := strconv.ParseInt(in.Text(), 10, 64)
	return n
}

func readInt() int {
	in.Scan()
	n, _ := strconv.Atoi(in.Text())
	return n
}

func main() {
	var out = bufio.NewWriter(os.Stdout)
	defer out.Flush()

	buffer := bufio.NewReaderSize(os.Stdin, 64*1024*1024)
	var n, m, r, c int
	fmt.Fscan(buffer, &n, &m)

	nrow := int64(n)
	ncol := int64(n)

	frow := make([]bool, n)
	fcol := make([]bool, n)
	for i := 0; i < n; i++ {
		frow[i] = true
		fcol[i] = true
	}

	results := make([]int64, m)
	total := int64(n) * int64(n)

	for i := 0; i < m; i++ {
		fmt.Fscan(buffer, &r, &c)
		r--
		c--

		if frow[r] && fcol[c] {
			total -= nrow + ncol - 1
			nrow--
			ncol--
		} else if frow[r] {
			total -= ncol
			nrow--
		} else if fcol[c] {
			total -= nrow
			ncol--
		}

		results[i] = total
		frow[r] = false
		fcol[c] = false
	}

	for _, r := range results {
		out.WriteString(strconv.FormatInt(r, 10))
		out.WriteByte(' ')
	}
}

package main

import (
	"bufio"
	"os"
	"strconv"
)

func main() {
	in := bufio.NewScanner(os.Stdin)
	out := bufio.NewWriter(os.Stdout)

	in.Split(bufio.ScanWords)
	defer out.Flush()

	in.Scan()
	n, _ := strconv.Atoi(in.Text())

	in.Scan()
	m, _ := strconv.Atoi(in.Text())

	nrow := int64(n)
	ncol := int64(n)

	frow := make([]bool, n)
	fcol := make([]bool, n)
	for i := 0; i < n; i++ {
		frow[i] = true
		fcol[i] = true
	}

	results := make([]int64, m)
	total := int64(n) * int64(n)

	for i := 0; i < m; i++ {
		in.Scan()
		r, _ := strconv.Atoi(in.Text())
		in.Scan()
		c, _ := strconv.Atoi(in.Text())
		r--
		c--

		if frow[r] && fcol[c] {
			total -= nrow + ncol - 1
			nrow--
			ncol--
		} else if frow[r] {
			total -= ncol
			nrow--
		} else if fcol[c] {
			total -= nrow
			ncol--
		}

		results[i] = total
		frow[r] = false
		fcol[c] = false
	}

	for _, r := range results {
		out.WriteString(strconv.FormatInt(r, 10))
		out.WriteByte(' ')
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

var n, m, r, c int
var res []int64

func main() {
	bufin := bufio.NewReaderSize(os.Stdin, 64*1024)
	bufout := bufio.NewWriterSize(os.Stdout, 64*1024)
	defer bufout.Flush()

	fmt.Fscan(bufin, &n, &m)

	nrow := int64(n)
	ncol := int64(n)

	frow := make([]bool, n)
	fcol := make([]bool, n)
	for i := 0; i < n; i++ {
		frow[i] = true
		fcol[i] = true
	}

	res = make([]int64, m)
	total := int64(n) * int64(n)

	for i := 0; i < m; i++ {
		fmt.Fscan(bufin, &r, &c)
		r--
		c--

		if frow[r] && fcol[c] {
			total -= nrow + ncol - 1
			nrow--
			ncol--
		} else if frow[r] {
			total -= ncol
			nrow--
		} else if fcol[c] {
			total -= nrow
			ncol--
		}

		res[i] = total
		frow[r] = false
		fcol[c] = false
	}

	for _, r := range res {
		fmt.Fprint(bufout, r, " ")
	}
}

package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
	"regexp"
	"strconv"
	"strings"
)

func main() {
	var n, m int64
	if _, err := fmt.Scanf("%d %d\n", &n, &m); err != nil {
		return
	}

	reader := bufio.NewReader(os.Stdin)
	re := regexp.MustCompile(`\r?\n`)

	xs := make([]int, n)
	ys := make([]int, n)
	xs_len := 0
	ys_len := 0

	var remain int64 = n * n

	for i := int64(0); i < m; i++ {
		if _in, err := reader.ReadString('\n'); err != nil {
			if err == io.EOF {
				break
			} else {
				return
			}
		} else {
			xy := strings.Split(re.ReplaceAllString(_in, ""), " ")
			if len(xy) != 2 {
				return
			}
			x, err := strconv.Atoi(xy[0])
			if err != nil {
				return
			}
			y, err := strconv.Atoi(xy[1])
			if err != nil {
				return
			}

			on_x := false
			on_y := false

			if xs[x-1] == 1 {
				on_x = true
			}
			if ys[y-1] == 1 {
				on_y = true
			}

			if !on_x {
				remain -= (n - int64(ys_len))
			}
			if !on_y {
				remain -= (n - int64(xs_len))
			}
			if !on_x && !on_y {
				remain += 1
			}

			if !on_x {
				xs[x-1] = 1
				xs_len++
			}
			if !on_y {
				ys[y-1] = 1
				ys_len++
			}

			fmt.Printf("%d ", remain)
		}
	}
	fmt.Printf("\n")
}

package main

import (
	"bufio"

	"os"
	"strconv"
)

func main() {
	in := bufio.NewScanner(os.Stdin)
	out := bufio.NewWriter(os.Stdout)

	in.Split(bufio.ScanWords)
	defer out.Flush()

	in.Scan()
	n, _ := strconv.Atoi(in.Text())

	in.Scan()
	m, _ := strconv.Atoi(in.Text())

	nrow := int64(n)
	ncol := int64(n)

	frow := make([]bool, n)
	fcol := make([]bool, n)
	for i := 0; i < n; i++ {
		frow[i] = true
		fcol[i] = true
	}

	ans := make([]int64, m)
	total := int64(n) * int64(n)

	for i := 0; i < m; i++ {
		in.Scan()
		r, _ := strconv.Atoi(in.Text())
		in.Scan()
		c, _ := strconv.Atoi(in.Text())
		r--
		c--

		if frow[r] && fcol[c] {
			total -= nrow + ncol - 1
			nrow--
			ncol--
		} else if frow[r] {
			total -= ncol
			nrow--
		} else if fcol[c] {
			total -= nrow
			ncol--
		}

		ans[i] = total
		frow[r] = false
		fcol[c] = false
	}

	for _, r := range ans {
		out.WriteString(strconv.FormatInt(r, 10))
		out.WriteByte(' ')
	}
}

package main

import (
	"bufio"
	. "fmt"
	"io"
	"os"
)

// github.com/EndlessCheng/codeforces-go
func CF701B(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

	var n, m, x, y int
	Fscan(in, &n, &m)
	rows, cols := map[int]bool{}, map[int]bool{}
	for ; m > 0; m-- {
		Fscan(in, &x, &y)
		rows[x] = true
		cols[y] = true
		Fprint(out, int64(n)*int64(n-len(rows)-len(cols))+int64(len(rows))*int64(len(cols)), " ")
	}
}

func main() { CF701B(os.Stdin, os.Stdout) }

package main

import (
	"bufio"
	"fmt"
	"os"
	"unicode"
)

var stdReader *bufio.Reader = bufio.NewReader(os.Stdin)

func readInt() (result int) {
	var negative, started bool
READER_LOOP:
	for {
		char, err := stdReader.ReadByte()
		switch {
		case err != nil:
			break READER_LOOP
		case char == '-':
			negative = true
		case unicode.IsDigit(rune(char)):
			started = true
			result = result*10 + int(char&0xf)
		case started:
			break READER_LOOP
		}
	}
	if negative {
		result = -result
	}
	return
}

func main() {
	n := int64(readInt())
	r := readInt()
	hor, ver := make([]bool, n), make([]bool, n)
	var horc, verc, occupied int64
	for i := 0; i < r; i++ {
		x, y := readInt()-1, readInt()-1
		if !hor[x] {
			occupied += n - verc
		}
		if !ver[y] {
			occupied += n - horc
		}
		if !hor[x] && !ver[y] {
			occupied--
		}

		if !hor[x] {
			hor[x] = true
			horc++
		}
		if !ver[y] {
			ver[y] = true
			verc++
		}
		fmt.Printf("%d ", n*n-occupied)
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

import "strings"

func main() {
	reader := bufio.NewReader(os.Stdin)
	text, _ := reader.ReadString('\n')
	text = strings.TrimRight(text, "\r\n")
	s := strings.Split(text, " ")

	size, _ := strconv.ParseInt(s[0], 10, 64)
	rooks, _ := strconv.ParseInt(s[1], 10, 64)

	columns := map[int]bool{}
	rows := map[int]bool{}

	cc := int64(0)
	cr := int64(0)

	for i := int64(0); i < rooks; i++ {
		text, _ := reader.ReadString('\n')
		text = strings.TrimRight(text, "\r\n")
		s := strings.Split(text, " ")

		r, _ := strconv.Atoi(s[0])
		c, _ := strconv.Atoi(s[1])
		if rows[r] == false {
			rows[r] = true
			cr += 1
		}
		if columns[c] == false {
			columns[c] = true
			cc += 1
		}
		fmt.Printf("%d ", (size-cc)*(size-cr))
	}

}

// Author: sighduck
// URL: https://codeforces.com/problemset/problem/701/B

package main

import (
	"bufio"
	"fmt"
	"os"
)

type Rook struct {
	X int
	Y int
}

func Solve(n int, m int, rooks []Rook) []int64 {
	rows := make([]int, n)
	columns := make([]int, n)
	queries := make([]int64, m)
	rowCount := int64(n)
	columnCount := int64(n)

	for i := 0; i < m; i++ {
		rook := rooks[i]
		if rows[rook.X-1] == 0 {
			rows[rook.X-1] = 1
			rowCount -= 1
		}
		if columns[rook.Y-1] == 0 {
			columns[rook.Y-1] = 1
			columnCount -= 1
		}
		queries[i] = rowCount * columnCount
	}
	return queries
}

func main() {
	reader := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	var n int
	var m int
	fmt.Fscanf(reader, "%d %d\n", &n, &m)
	rooks := make([]Rook, m)

	for i := 0; i < m; i++ {
		fmt.Fscanf(reader, "%d %d\n", &rooks[i].X, &rooks[i].Y)
	}
	queries := Solve(n, m, rooks)
	for i := 0; i < m; i++ {
		fmt.Fprintf(writer, "%d ", queries[i])
	}
}

package main

import (
	"bufio"
	"os"
	"strconv"
)

func main() {
	cin := bufio.NewScanner(os.Stdin)
	cin.Split(bufio.ScanWords)
	cout := bufio.NewWriter(os.Stdout)
	defer cout.Flush()

	cin.Scan()
	n, _ := strconv.Atoi(cin.Text())
	cin.Scan()
	m, _ := strconv.Atoi(cin.Text())

	ans := make([]int64, n)
	xx := make([]bool, n)
	yy := make([]bool, n)
	xs := 0
	ys := 0
	for i := 0; i < m; i++ {
		cin.Scan()
		x, _ := strconv.Atoi(cin.Text())
		x--
		cin.Scan()
		y, _ := strconv.Atoi(cin.Text())
		y--

		if i == 0 {
			ans[i] = int64(n)*int64(n) - (2*int64(n) - 1)
			xs, ys = xs+1, ys+1
			xx[x], yy[y] = true, true
		} else if xx[x] && yy[y] {
			ans[i] = ans[i-1]
		} else if xx[x] {
			ans[i] = ans[i-1] - (int64(n) - int64(xs))
			ys++
			yy[y] = true
		} else if yy[y] {
			ans[i] = ans[i-1] - (int64(n) - int64(ys))
			xs++
			xx[x] = true
		} else {
			ans[i] = ans[i-1] - (int64(n) - int64(xs)) - (int64(n) - int64(ys)) + 1
			xs, ys = xs+1, ys+1
			xx[x], yy[y] = true, true
		}
	}
	for _, a := range ans {
		cout.WriteString(strconv.FormatInt(a, 10))
		cout.WriteByte(' ')
	}

}

package main

import "fmt"

func main() {
	var n, m, i, r, c int32
	fmt.Scan(&n, &m)

	nrow := int64(n)
	ncol := int64(n)

	frow := make([]bool, n)
	fcol := make([]bool, n)
	for i = 0; i < n; i++ {
		frow[i] = true
		fcol[i] = true
	}

	results := make([]int64, m)
	total := int64(n) * int64(n)

	for i = 0; i < m; i++ {
		fmt.Scan(&r, &c)
		r--
		c--
		if frow[r] && fcol[c] {
			total -= nrow + ncol - 1
			nrow--
			ncol--
		} else if frow[r] {
			total -= ncol
			nrow--
		} else {
			total -= nrow
			ncol--
		}

		results[i] = total
		frow[r] = false
		fcol[c] = false
	}

	for i = 0; i < m-1; i++ {
		fmt.Print(results[i], " ")
	}
	fmt.Println(results[i])
}