S Matrix for Yang-Mills and Gravitational Fields