Maximal acyclic subgraphs and closest stable matrices