                                     eval(%w'dx=[-
                      1,0,1        ,-1,1,-1,0,  1];d
                  y=[-1,-1,-1,    0,0, 1,1,1];   m=[[]
                 ];de       fine_ met   hod(:r)   {|t,u
                ,v,     g   |m[t]  [u]   =0;8.ti   mes{|
          i|if(t+dx.at (     i)>=0&&t+d   x.at(i   )<v&&
       u+dy.  at(i)>     =0   &&u+dy.at   (i)<g&&   m[t+
     dx.at(    i)][u+dy    .a  t(  i)]==1);r(t+     dx.
    at(i),u+        dy   .      a    t (i),        v,g
   );end}};          lo     op{         w     ,h=gets
  .c   h         o    mp.spl  i      t   .map(&:to
  _i    )    ;    i    f(w==   0  &   &   h==0);b
  rea    k   ;e   nd   ;m=Arr      a      y.new(w
  ).ma   p    {A        rray.n   ew(h   ,0)};h.ti
   mes{      |y|    s=gets.chomp.spli   t.map(&:
    to_i);w.times   {|x|m[x][y]=s[x]} };c=0;h.ti
     mes{|y|w.ti     mes{|x| if(m[x][y]==1);c+
       =1;r(x,y,   w,h);e        nd}};puts
          (c)}'.join)
