using System;
class MatrixTool
{
    static void Main()
    {
        int rows = ReadPositiveInt("Ievadi rindu skaitu: ");
        int cols = ReadPositiveInt("Ievadi kolonnu skaitu: ");
        int[,] matrix = GenerateMatrix(rows, cols);
        while (true)
        {
            Console.WriteLine("\n--- Izvēlne ---");
            Console.WriteLine("1 – Izdrukāt matricu");
            Console.WriteLine("2 – Atrast lielāko un mazāko elementu");
            Console.WriteLine("3 – Aprēķināt katras kolonnas summu");
            Console.WriteLine("4 – Atrast galvenās diagonāles summu");
            Console.WriteLine("5 – Atrast blakusdiagonāles summu");
            Console.WriteLine("6 – Transponēt matricu");
            Console.WriteLine("7 – Meklēt skaitli matricā");
            Console.WriteLine("0 – Iziet");
            int choice = ReadInt("Tava izvēle: ");
            switch (choice)
            {
                case 1:
                    PrintMatrix(matrix);
                    break;
                case 2:
                    Console.WriteLine($"Max: {FindMax(matrix)}");
                    Console.WriteLine($"Min: {FindMin(matrix)}");
                    break;
                case 3:
                    int[] sums = ColumnSums(matrix);
                    Console.WriteLine("Kolonnu summas: " + string.Join(", ", sums));
                    break;
                case 4:
                    Console.WriteLine($"Galvenās diagonāles summa: {MainDiagonalSum(matrix)}");
                    break;
                case 5:
                    Console.WriteLine($"Blakusdiagonāles summa: {SecondaryDiagonalSum(matrix)}");
                    break;
                case 6:
                    matrix = Transpose(matrix);
                    Console.WriteLine("Matrica transponēta.");
                    break;
                case 7:
                    int number = ReadInt("Ievadi meklējamo skaitli: ");
                    var result = FindNumber(matrix, number);
                    if (result.found)
                        Console.WriteLine($"Atrasts ({result.row}, {result.col})");
                    else
                        Console.WriteLine("Skaitlis nav atrasts.");
                    break;
                case 0:
                    return;
                default:
                    Console.WriteLine("Nepareiza izvēle.");
                    break;
            }
        }
    }
    static int ReadPositiveInt(string msg)
    {
        int value;
        do
        {
            Console.Write(msg);
        } while (!int.TryParse(Console.ReadLine(), out value) || value <= 0);
        return value;
    }
    static int ReadInt(string msg)
    {
        int value;
        while (true)
        {
            Console.Write(msg);
            if (int.TryParse(Console.ReadLine(), out value)) return value;
        }
    }
    static int[,] GenerateMatrix(int rows, int cols)
    {
        Random rnd = new Random();
        int[,] m = new int[rows, cols];
        for (int i = 0; i < rows; i++)
            for (int j = 0; j < cols; j++)
                m[i, j] = rnd.Next(-100, 101);
        return m;
    }
    static void PrintMatrix(int[,] matrix)
    {
        int rows = matrix.GetLength(0);
        int cols = matrix.GetLength(1);
        for (int i = 0; i < rows; i++)
        {
            for (int j = 0; j < cols; j++)
                Console.Write($"{matrix[i, j],5}");
            Console.WriteLine();
        }
    }
    static int FindMax(int[,] matrix)
    {
        int max = matrix[0, 0];
        foreach (int v in matrix) if (v > max) max = v;
        return max;
    }
    static int FindMin(int[,] matrix)
    {
        int min = matrix[0, 0];
        foreach (int v in matrix) if (v < min) min = v;
        return min;
    }
    static int[] ColumnSums(int[,] matrix)
    {
        int rows = matrix.GetLength(0);
        int cols = matrix.GetLength(1);
        int[] sums = new int[cols];
        for (int j = 0; j < cols; j++)
            for (int i = 0; i < rows; i++)
                sums[j] += matrix[i, j];
        return sums;
    }
    static int MainDiagonalSum(int[,] matrix)
    {
        int rows = matrix.GetLength(0);
        int cols = matrix.GetLength(1);
        int size = Math.Min(rows, cols);
        int sum = 0;
        for (int i = 0; i < size; i++) sum += matrix[i, i];
        return sum;
    }
    static int SecondaryDiagonalSum(int[,] matrix)
    {
        int rows = matrix.GetLength(0);
        int cols = matrix.GetLength(1);
        int size = Math.Min(rows, cols);
        int sum = 0;
        for (int i = 0; i < size; i++) sum += matrix[i, cols - 1 - i];
        return sum;
    }
    static int[,] Transpose(int[,] matrix)
    {
        int rows = matrix.GetLength(0);
        int cols = matrix.GetLength(1);
        int[,] t = new int[cols, rows];

        for (int i = 0; i < rows; i++)
            for (int j = 0; j < cols; j++)
                t[j, i] = matrix[i, j];
        return t;
    }
    static (bool found, int row, int col) FindNumber(int[,] matrix, int number)
    {
        int rows = matrix.GetLength(0);
        int cols = matrix.GetLength(1);

        for (int i = 0; i < rows; i++)
            for (int j = 0; j < cols; j++)
                if (matrix[i, j] == number)
                    return (true, i, j);
        return (false, -1, -1);
    }
}