https://www.acmicpc.net/problem/17086
import java.util.*;
var arr=Array(0, {Array(0, {0})})
var check=Array(0, {Array(0, {false})})
var n: Int=0
var m: Int=0
var dx=arrayOf(0,0,1,-1,1,1,-1,-1)
var dy=arrayOf(1,-1,0,0,1,-1,-1,1)
fun bfs(a:Int,b:Int):Int{
check=Array(n+1, {Array(m+1, {false})})
var q=LinkedList<Int>()
var w=LinkedList<Int>()
var e=LinkedList<Int>()
q.add(a)
w.add(b)
e.add(0)
check[a][b]=true
while(!q.isEmpty()){
val v0=q.poll()
val v1=w.poll()
val v2=e.poll()
for(i in 0 until 8){
var x=v0+dx[i]
var y=v1+dy[i]
var z=v2+1
if(x<0 || x>=n || y<0 || y>=m || check[x][y]==true) continue
if(arr[x][y]==1) return z
q.add(x)
w.add(y)
e.add(z)
check[x][y]=true
}
}
return 0
}
fun main(args: Array<String>) {
val s=Scanner(System.`in`)
n=s.nextInt()
m=s.nextInt()
arr=Array(n+1, {Array(m+1, {0})})
for(i in 0 until n){
for(j in 0 until m){
arr[i][j]=s.nextInt()
}
}
var count=0
for(i in 0 until n){
for(j in 0 until m){
if(arr[i][j]!=1){
var num=bfs(i,j)
count=if(num>count) {num} else {count}
}
}
}
print(count)
}