In this paper, we propose a fast SFF method, which also provides accurate shape estimation for objects with complex geometry. Dynamic programming is a mathematical tool to determine the optical solution to an n-variable problem efficiently. In the new SF method, dynamic programming is modified and used to find the optimal path that gives maximum focus measure at each pixel with certain constraints. Then the shape of object can be estimated over the 3D FIS. An automated CCD camera system has been developed to implement the proposed method.