https://www.swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV5PuPq6AaQDFAUq
import java.util.Scanner;
public class Solution {
static int T, N, W;
static int [][] map;
public static void main(String[] args) {
Scanner scanner = new Scanner(src);
T = scanner.nextInt();
for(int t=1; t<=T; t++) {
int answer = 0;
N = scanner.nextInt();
map = new int[N][N];
W = scanner.nextInt();
for(int row=0; row<N; row++) {
for(int col=0; col<N; col++) {
map[row][col]=scanner.nextInt();
}
}
// 입력 완료-------------------------------------------
for(int row=0; row<N; row++) {
for(int col=0; col<N; col++) {
if(isStartCol(row, col)) {
if(getColLen(row, col)==W) {
answer++;
}
}
if(isStartRow(row, col)) {
if(getRowLen(row, col)==W) {
answer++;
}
}
}
}
// 처리 완료-------------------------------------------
System.out.printf("#%d %d%n",t, answer);
}
}
static boolean isStartRow(int row, int col) {
return row==0 || map[row-1][col]==0;
}
static boolean isStartCol(int row, int col) {
return col==0 || map[row][col-1]==0;
}
static int getRowLen(int row, int col) {
int len = 0;
for(int r=row; r<N; r++) {
if(map[r][col]==1) {
len++;
}else {
break;
}
}
return len;
}
static int getColLen(int row, int col) {
int len = 0;
for(int c=col; c<N; c++) {
if(map[row][c]==1) {
len++;
}else {
break;
}
}
return len;
}
static String src = "4\r\n" +
"2 2 2\r\n" +
"3 4\r\n" +
"2 2 2\r\n" +
"1 2\r\n" +
"2 2 1\r\n" +
"4 2\r\n" +
"2 2 1\r\n" +
"3 2";
}