https://www.acmicpc.net/problem/4963
import java.util.*;
import java.io.*;
var arr=Array(0, {Array(0, {0})})
var check=Array(0, {Array(0,{false})})
var dxy = arrayOf(arrayOf(0,1),arrayOf(0,-1),arrayOf(1,0),arrayOf(-1,0),arrayOf(1,1),arrayOf(-1,-1),arrayOf(1,-1),arrayOf(-1,1))
var w: Int=0
var h: Int=0
fun find(i:Int,j:Int){
check[i][j]=true
for(k in 0 until 8){
var x=i+dxy[k][0]
var y=j+dxy[k][1]
if(x>=0 && x<h && y>=0 && y<w){
if(arr[x][y]==1 && check[x][y]==false) find(x,y)
}
}
}
fun main(args: Array<String>) {
val s=Scanner(System.`in`)
while(true){
w=s.nextInt()
h=s.nextInt()
if(w==0 && h==0) break
arr=Array(h, {Array(w, {0})})
for(i in 0 until h){
for(j in 0 until w){
arr[i][j]=s.nextInt()
}
}
check=Array(h, {Array(w, {false})})
var count=0
for(i in 0 until h){
for(j in 0 until w){
if(arr[i][j]==1 && check[i][j]==false){
find(i,j)
count++
}
}
}
println(count)
}
}