import java.util.*;

class Solution {
	static int n, m, k;
	static int[][] arr;
	static List<Pair> list;

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int test = in.nextInt();
		list = new ArrayList<>();
		for (int i = 1; i <= test; i++) {
			list.clear();

			n = in.nextInt();
			m = in.nextInt();
			k = in.nextInt();
			arr = new int[n + k][m + k];
			for (int j = (k / 2); j < (k / 2) + n; j++) {
				for (int z = (k / 2); z < (k / 2) + m; z++) {
					arr[j][z] = in.nextInt();
					if (arr[j][z] != 0)
						list.add(new Pair(j, z, arr[j][z], arr[j][z], k, 0));
				}
			}
			solve();
			
			int result = 0;
			for (int j = 0; j < arr.length; j++) {
				for (int z = 0; z < arr[0].length; z++) {
					if (arr[j][z] != 0 && arr[j][z] != -1)
						result++;
				}
			}
			System.out.println("#" + i + " " + result + "\n");
		}
	}

	static int[][] dir = { { 1, 0 }, { 0, 1 }, { -1, 0 }, { 0, -1 } };

	private static void solve() {
		PriorityQueue<Pair> queue = new PriorityQueue<Pair>(list);

		while (!queue.isEmpty()) {
			Pair t = queue.poll();
			if (t.state == 0 && t.flag == 1) {
				arr[t.x][t.y] = -1;
				continue;
			}
			if (t.time == 0) {
				continue;
			}
			if (t.state == 0) {
				queue.add(new Pair(t.x, t.y, t.k, t.k, t.time, 1));
			} else {
				queue.add(new Pair(t.x, t.y, t.k, t.state - 1, t.time - 1, t.flag));
				continue;
			}
			for (int i = 0; i < 4; i++) {
				int tx = t.x + dir[i][0];
				int ty = t.y + dir[i][1];
				if (tx < 0 || ty < 0 || tx >= n + k || ty >= m + k) {
					continue;
				}
				if (arr[tx][ty] != 0) {
					continue;
				}
				arr[tx][ty] = t.k;
				queue.add(new Pair(tx, ty, t.k, t.k, t.time - 1, 0));
			}
		}
	}
}

class Pair implements Comparable<Pair> {
	public int x;
	public int y;
	public int k;
	public int state; // 변하는 생명력
	public int time;
	public int flag; // 0이 되었을 때, 번식 했나 유무

	public Pair(int x, int y, int k, int state, int time, int flag) {
		this.x = x;
		this.y = y;
		this.k = k;
		this.state = state;
		this.time = time;
		this.flag = flag;
	}

	@Override
	public int compareTo(Pair o) {
		if (this.time > o.time) {
			return -1;
		} else if (this.time == o.time) {
			if (this.k > o.k) {
				return -1;
			}
			return 1;
		}
		return 1;
	}
}

'알고리즘 공부' 카테고리의 다른 글

SWEA 3462: 선표의 축구 경기 예측  (0) 2021.01.10
SWEA 1949: 등산로 조성  (0) 2021.01.07
SWEA 1267: 작업순서  (0) 2021.01.03
SWEA 5521: 상원이의 생일파티  (0) 2020.12.27
SWEA: 5215 햄버거 다이어트  (0) 2020.12.15

+ Recent posts