Time: O(m*n), Space: (m*n)