티스토리 뷰
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
class Solution {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
int T = Integer.parseInt(br.readLine());
for (int t = 0; t < T; t++) {
String[] inputs = br.readLine().split(" ");
int n = Integer.parseInt(inputs[0]);
int k = Integer.parseInt(inputs[1]);
int[][] arr = new int[n][n];
for (int i = 0; i < n; i++) {
inputs = br.readLine().split(" ");
for (int j = 0; j < n; j++) {
arr[i][j] = Integer.parseInt(inputs[j]);
}
}
int result = 0;
for (int y = 0; y < n; y++) {
result += inputWordCntFromX(arr[y], n, k);
}
for (int x = 0; x < n; x++) {
result += inputWordCntFromY(arr, x, n, k);
}
sb.append(String.format("#%d %d\n", t + 1, result));
}
System.out.println(sb);
}
private static int inputWordCntFromX(int[] arr, int n, int k) {
int count = 0;
int result = 0;
for (int x = 0; x < n; x++) {
if (arr[x] == 1) {
count++;
} else {
if (count == k) {
result++;
}
count = 0;
}
}
if (count == k) {
result++;
}
return result;
}
private static int inputWordCntFromY(int[][] arr, int x, int n, int k) {
int count = 0;
int result = 0;
for (int y = 0; y < n; y++) {
if (arr[y][x] == 1) {
count++;
} else {
if (count == k) {
result++;
}
count = 0;
}
}
if (count == k) {
result++;
}
return result;
}
}