computing Optimal no of threads given an MxN matrix dynamically

Hi.

 I would like to know how do we calculate the optimal number of threads within a block for an M X N matrix taking into consideration the warpsize,MAXSMP's and maximum number of threads possible in an SMP...My input keeps changing so i cant go and find from the Occupancy calculator as such...