private static int[][] makeTransformMatrix(int rows, int cols) {
    int length = rows * cols;
    int[][] transform = new int[length][length];
    for (int i = 0; i < rows; i++) {
      for (int j = 0; j < cols; j++) {
        int diag = cols * i + j;
        transform[diag][diag] = 1;
        if (i > 0) {
          transform[diag][diag - cols] = 1;
        }
        if (i < rows - 1) {
          transform[diag][diag + cols] = 1;
        }
        if (j > 0) {
          transform[diag][diag - 1] = 1;
        }
        if (j < cols - 1) {
          transform[diag][diag + 1] = 1;
        }
      }
    }
    return transform;
  }