[inmethod-grid] Issue #308 - Simplify the cod to find the closest TD cell

(cherry picked from commit c0efb10869bd3a7b919f3244625d9b5480533df3)