h,w=gets.split.map &:to_i m=$<.map{|e|e.chomp} r=Float::INFINITY (-1..h).each{|y|(-1..w).each{|x| next if 0<=y&&y