package bj757x.bj7576;

import java.io.PrintWriter;
import java.util.LinkedList;
import java.util.Scanner;

/**
 * Created by jsong on 09/04/2017.
 *
 * @hackerrank https://www.hackerrank.com/jsong00505
 * @backjoon https://www.acmicpc.net/user/jsong00505
 * @github https://github.com/jsong00505
 * @linkedin https://www.linkedin.com/in/junesongskorea/
 * @email jsong00505@gmail.com
 * @challenge Main
 */
class Point {
  private int x;
  private int y;

  Point(int x, int y) {
    this.x = x;
    this.y = y;
  }

  public int getX() {
    return x;
  }

  public int getY() {
    return y;
  }
}

public class Main {
  static int RIPED = 1;
  static int UNRIPED = 0;
  static int EMPTY = -1;

  static void print(int n, int m, int[][] box) {
    System.out.println("=========START=========");
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        System.out.print(box[i][j] + " ");
      }
      System.out.println();
    }
    System.out.println("==========END=========");
  }

  static int[][] depthFirstSearch(int n, int m, int[][] box, LinkedList<Point> queue) {
    for (Point p : queue) {
      int x = p.getX();
      int y = p.getY();

      // up
      if (x > 0) {
        if (box[x - 1][y] == UNRIPED) {
          box[x - 1][y] = RIPED;
        }
      }
      // down
      if (x < n - 1) {
        if (box[x + 1][y] == UNRIPED) {
          box[x + 1][y] = RIPED;
        }
      }
      // right
      if (y < m - 1) {
        if (box[x][y + 1] == UNRIPED) {
          box[x][y + 1] = RIPED;
        }
      }
      // left
      if (y > 0) {
        if (box[x][y - 1] == UNRIPED) {
          box[x][y - 1] = RIPED;
        }
      }
    }

    //print(n, m, box);
    return box;
  }

  static int getMinDaysRipedAll(int n, int m, int[][] box, int emptySpots) {
    int days = 1;
    int beforeSpots = 0;
    while (true) {
      LinkedList<Point> queue = new LinkedList<>();
      int ripedSpots = 0;

      for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
          if (box[i][j] == RIPED) {
            queue.add(new Point(i, j));
          }
        }
      }

      // update the box by depth-first search
      box = depthFirstSearch(n, m, box, queue);
      //print(n, m, box);
      for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
          if (box[i][j] == RIPED) {
            ripedSpots++;
          }
        }
      }

      if (n * m == emptySpots + ripedSpots) {
        break;
      } else if (beforeSpots == ripedSpots) {
        days = -1;
        break;
      } else {
        days++;
        beforeSpots = ripedSpots;
      } // if-else
    } // while

    return days;
  }

  public static void main(String[] args) {
    try (Scanner in = new Scanner(System.in);
        PrintWriter out = new PrintWriter(System.out); ) {
      // horizontal
      int m = in.nextInt();
      // vertical
      int n = in.nextInt();

      int emptySpots = 0;
      assert (m >= 2 && m <= 1000 && n >= 2 && n <= 1000);

      int[][] box = new int[n][m];
      for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
          box[i][j] = in.nextInt();

          if (box[i][j] == EMPTY) {
            emptySpots++;
          }
        }
      }

      out.println(getMinDaysRipedAll(n, m, box, emptySpots));
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

@@ -691,16 +691,46 @@
  = -1;%0A%0A
+  static int ripedSpots = 0;%0A%0A
   static
@@ -766,24 +766,24 @@
 %5B%5D%5B%5D box) %7B%0A
-
     System.o
@@ -1292,32 +1292,56 @@
  1%5D%5By%5D = RIPED;%0A
+          ripedSpots++;%0A
         %7D%0A      
@@ -1444,32 +1444,56 @@
  1%5D%5By%5D = RIPED;%0A
+          ripedSpots++;%0A
         %7D%0A      
@@ -1597,32 +1597,56 @@
 y + 1%5D = RIPED;%0A
+          ripedSpots++;%0A
         %7D%0A      
@@ -1745,32 +1745,56 @@
 y - 1%5D = RIPED;%0A
+          ripedSpots++;%0A
         %7D%0A      
@@ -2041,34 +2041,8 @@
 %3E();
-%0A      int ripedSpots = 0;
 %0A%0A  
@@ -2317,200 +2317,8 @@
 ue);
-%0A      //print(n, m, box);%0A      for (int i = 0; i %3C n; i++) %7B%0A        for (int j = 0; j %3C m; j++) %7B%0A          if (box%5Bi%5D%5Bj%5D == RIPED) %7B%0A            ripedSpots++;%0A          %7D%0A        %7D%0A      %7D
 %0A%0A  
@@ -3085,24 +3085,24 @@
 == EMPTY) %7B%0A
-
             
@@ -3107,32 +3107,100 @@
   emptySpots++;%0A
+          %7D else if(box%5Bi%5D%5Bj%5D == RIPED) %7B%0A            ripedSpots++;%0A
           %7D%0A    

fix duplicated counting riped spots, but still over time limit