When to say I’m sorry or thank you

When to say I’m sorry or thank you